Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL cBucketRegion
LOCAL oSbBucketRegion
LOCAL oXml
LOCAL cResponseStr
LOCAL oResponseXml

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oRest := CreateObject("Chilkat.Rest")

//  Connect to the Amazon AWS REST server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("s3.amazonaws.com", nPort, nBTls, nBAutoReconnect)

//  Provide AWS credentials for the REST call.
oAuthAws := CreateObject("Chilkat.AuthAws")
oAuthAws:AccessKey := "AWS_ACCESS_KEY"
oAuthAws:SecretKey := "AWS_SECRET_KEY"
oAuthAws:ServiceName := "s3"
nSuccess := oRest:SetAuthAws(oAuthAws)

//  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"
cBucketRegion := "eu-west-2"
oSbBucketRegion := CreateObject("Chilkat.StringBuilder")
oSbBucketRegion:Append(cBucketRegion)

//  We only need to specify the LocationConstraint if the bucket's region is NOT us-east-1
oXml := CreateObject("Chilkat.Xml")
IF (.NOT. oSbBucketRegion:ContentsEqual("us-east-1"))
    oXml:Tag := "CreateBucketConfiguration"
    oXml:AddAttribute("xmlns", "http://s3.amazonaws.com/doc/2006-03-01/")
    oXml:UpdateChildContent("LocationConstraint", "eu-west-2")
ENDIF

//  --------------------------------------------------------------
//  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".
oRest:Host := "chilkateuwest2.s3.amazonaws.com"

//  Make the call to create the bucket.

IF (.NOT. oSbBucketRegion:ContentsEqual("us-east-1"))
    cResponseStr := oRest:FullRequestString("PUT", "/", oXml:GetXml())
ELSE
    //  If the bucket is to be created in the us-east-1 region (the default region)
    //  just send a PUT with no body.
    cResponseStr := oRest:FullRequestNoBody("PUT", "/")
ENDIF

IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oAuthAws:destroy()
    oSbBucketRegion:destroy()
    oXml:destroy()
    RETURN
ENDIF

IF (oRest:ResponseStatusCode != 200)
    ? "status code = " + Str(oRest:ResponseStatusCode)
    oResponseXml := CreateObject("Chilkat.Xml")
    oResponseXml:LoadXml(cResponseStr)
    ? oResponseXml:GetXml()
    ? "Failed."
    oRest:destroy()
    oAuthAws:destroy()
    oSbBucketRegion:destroy()
    oXml:destroy()
    oResponseXml:destroy()
    RETURN
ENDIF

? "Bucket in the eu-west-2 region created."

oRest:destroy()
oAuthAws:destroy()
oSbBucketRegion:destroy()
oXml:destroy()
oResponseXml:destroy()