Unicode C
Unicode C
Create S3 Bucket in a Region
See more Amazon S3 Examples
Demonstrates how to create an S3 bucket in a specified region. This example will create a bucket in the eu-west-2 region.Chilkat Unicode C Downloads
#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkXmlW.h>
void ChilkatSample(void)
{
BOOL success;
HCkRestW rest;
BOOL bTls;
int port;
BOOL bAutoReconnect;
HCkAuthAwsW authAws;
const wchar_t *bucketRegion;
HCkStringBuilderW sbBucketRegion;
HCkXmlW xml;
const wchar_t *responseStr;
HCkXmlW responseXml;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest = CkRestW_Create();
// Connect to the Amazon AWS REST server.
bTls = TRUE;
port = 443;
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"s3.amazonaws.com",port,bTls,bAutoReconnect);
// Provide AWS credentials for the REST call.
authAws = CkAuthAwsW_Create();
CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
CkAuthAwsW_putServiceName(authAws,L"s3");
success = CkRestW_SetAuthAws(rest,authAws);
// We'll send a PUT request having an XML body such as this:
// <CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
// <LocationConstraint>BucketRegion</LocationConstraint>
// </CreateBucketConfiguration>
// Create the XML body specifying the region as "eu-west-2"
bucketRegion = L"eu-west-2";
sbBucketRegion = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbBucketRegion,bucketRegion);
// We only need to specify the LocationConstraint if the bucket's region is NOT us-east-1
xml = CkXmlW_Create();
if (!CkStringBuilderW_ContentsEqual(sbBucketRegion,L"us-east-1")) {
CkXmlW_putTag(xml,L"CreateBucketConfiguration");
CkXmlW_AddAttribute(xml,L"xmlns",L"http://s3.amazonaws.com/doc/2006-03-01/");
CkXmlW_UpdateChildContent(xml,L"LocationConstraint",L"eu-west-2");
}
// --------------------------------------------------------------
// IMPORTANT: To create a bucket in the default us-east-1 region,
// do not add the LocationConstraint. Adding a LocationConstraint of "us-east-1"
// causes an error "The specified location-constraint is not valid."
// By default, the bucket is created in us-east-1 by sending a PUT with an empty body.
// --------------------------------------------------------------
// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkateuwest2".
CkRestW_putHost(rest,L"chilkateuwest2.s3.amazonaws.com");
// Make the call to create the bucket.
if (!CkStringBuilderW_ContentsEqual(sbBucketRegion,L"us-east-1")) {
responseStr = CkRestW_fullRequestString(rest,L"PUT",L"/",CkXmlW_getXml(xml));
}
else {
// If the bucket is to be created in the us-east-1 region (the default region)
// just send a PUT with no body.
responseStr = CkRestW_fullRequestNoBody(rest,L"PUT",L"/");
}
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
CkStringBuilderW_Dispose(sbBucketRegion);
CkXmlW_Dispose(xml);
return;
}
if (CkRestW_getResponseStatusCode(rest) != 200) {
wprintf(L"status code = %d\n",CkRestW_getResponseStatusCode(rest));
responseXml = CkXmlW_Create();
CkXmlW_LoadXml(responseXml,responseStr);
wprintf(L"%s\n",CkXmlW_getXml(responseXml));
wprintf(L"Failed.\n");
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
CkStringBuilderW_Dispose(sbBucketRegion);
CkXmlW_Dispose(xml);
CkXmlW_Dispose(responseXml);
return;
}
wprintf(L"Bucket in the eu-west-2 region created.\n");
CkRestW_Dispose(rest);
CkAuthAwsW_Dispose(authAws);
CkStringBuilderW_Dispose(sbBucketRegion);
CkXmlW_Dispose(xml);
CkXmlW_Dispose(responseXml);
}