Demonstrates how to copy an S3 file from one bucket to another. The file can also be renamed if desired. (The file already exists in S3 in one bucket, and is copied without downloading/uploading to another S3 bucket.) This example copies from /chilkat.qa/starfish.jpg to /chilkat.ocean/starfishCopy.jpg
curl -X PUT https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg \
-H "x-amz-copy-source: /chilkat.qa/starfish.jpg"
#include <CkRestW.h>
#include <CkAuthAwsW.h>
#include <CkStringBuilderW.h>
#include <CkXmlW.h>
void ChilkatSample(void)
{
CkRestW rest;
bool success;
CkAuthAwsW authAws;
authAws.put_AccessKey(L"AWS_ACCESS_KEY");
authAws.put_SecretKey(L"AWS_SECRET_KEY");
authAws.put_Region(L"us-west-2");
authAws.put_ServiceName(L"s3");
rest.SetAuthAws(authAws);
// URL: https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
success = rest.Connect(L"chilkat.ocean.s3.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
if (success != true) {
wprintf(L"ConnectFailReason: %d\n",rest.get_ConnectFailReason());
wprintf(L"%s\n",rest.lastErrorText());
return;
}
rest.AddHeader(L"x-amz-copy-source",L"/chilkat.qa/starfish.jpg");
CkStringBuilderW sbResponseBody;
success = rest.FullRequestNoBodySb(L"PUT",L"/starfishCopy.jpg",sbResponseBody);
if (success != true) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
int respStatusCode = rest.get_ResponseStatusCode();
if (respStatusCode >= 400) {
wprintf(L"Response Status Code = %d\n",respStatusCode);
wprintf(L"Response Header:\n");
wprintf(L"%s\n",rest.responseHeader());
wprintf(L"Response Body:\n");
wprintf(L"%s\n",sbResponseBody.getAsString());
return;
}
CkXmlW xmlResponse;
xmlResponse.LoadSb(sbResponseBody,true);
// See the Online Tool for Generating XML Parse Code
const wchar_t *CopyObjectResult_xmlns = 0;
const wchar_t *tagPath = 0;
const wchar_t *LastModified = 0;
const wchar_t *ETag = 0;
CopyObjectResult_xmlns = xmlResponse.getAttrValue(L"xmlns");
LastModified = xmlResponse.getChildContent(L"LastModified");
ETag = xmlResponse.getChildContent(L"ETag");
}
<?xml version="1.0" encoding="UTF-8"?>
<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<LastModified>2018-12-18T15:53:32.000Z</LastModified>
<ETag>"2e9c59dbf2662367dc97dfdda85da048"</ETag>
</CopyObjectResult>