Sample code for 30+ languages & platforms
Xbase++

S3 Find Bucket Region

See more Amazon S3 (new) Examples

Demonstrates how to find out the region of an S3 bucket.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL cStrResult
LOCAL nResponseStatusCode
LOCAL oXml

nSuccess := 0

//  This 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 using the correct region (in this example, "us-west-2")
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)

//  The bucket name we want to know about is set in the HOST header.
//  In this example, we want to find the region for the "chilkat.qa" bucket.
oRest:Host := "chilkat.qa.s3.amazonaws.com"

//  Send the GET request to query the bucket location.
cStrResult := oRest:FullRequestNoBody("GET", "/?location")
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oAuthAws:destroy()
    RETURN
ENDIF

nResponseStatusCode := oRest:ResponseStatusCode
? "Response status code = " + Str(nResponseStatusCode)
IF (nResponseStatusCode != 200)
    ? oRest:ResponseHeader
    ? cStrResult
    ? "Failed."
    oRest:destroy()
    oAuthAws:destroy()
    RETURN
ENDIF

? cStrResult

//  The result contains XML like this:
//  <?xml version="1.0" encoding="UTF-8"?>
//  <LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">us-west-2</LocationConstraint>

//  Get the "us-west-2" from the XML
oXml := CreateObject("Chilkat.Xml")
nSuccess := oXml:LoadXml(cStrResult)
? "region = " + oXml:Content

oRest:destroy()
oAuthAws:destroy()
oXml:destroy()