Xbase++
Xbase++
S3 - Upload to Bucket in a Region
See more Amazon S3 (new) Examples
Demonstrates how to upload a file to an S3 bucket that was created in a particular region.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL oFileData
LOCAL oResponseBody
LOCAL nStatusCode
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// When usig the REST class to access an S3 bucket that has been created in a particular region,
// there are three important things to remember:
//
// 1) Connect to the domain that includes the region. For example, instead of connecting to
// "s3.amazonaws.com", connect to "s3.eu-central-1.amazonaws.com" if your bucket is in the EU Frankfurt region.
// See http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region for a list of S3 region domains (endpoints)
//
// 2) Set the AuthAws.Region property equal to the region, such as "eu-central-1".
//
// 3) Include the region in request's Host header.
//
// This example will upload to a bucket in the eu-central-1 region.
//
oRest := CreateObject("Chilkat.Rest")
// Connect to the Amazon AWS REST server.
nBTls := 1
nPort := 443
nBAutoReconnect := 1
nSuccess := oRest:Connect("s3.eu-central-1.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"
oAuthAws:Region := "eu-central-1"
nSuccess := oRest:SetAuthAws(oAuthAws)
// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkateufrankfurt" (which was created in the eu-central-1 region)
oRest:Host := "chilkateufrankfurt.s3.eu-central-1.amazonaws.com"
oFileData := CreateObject("Chilkat.BinData")
nSuccess := oFileData:LoadFile("qa_data/jpg/penguins.jpg")
// Upload the file to Amazon S3.
oResponseBody := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestBd("PUT", "/penguins.jpg", oFileData, oResponseBody)
IF (nSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oAuthAws:destroy()
oFileData:destroy()
oResponseBody:destroy()
RETURN
ENDIF
// Did we get a 200 response indicating success?
nStatusCode := oRest:ResponseStatusCode
IF (nStatusCode != 200)
? "Error response: " + oResponseBody:GetAsString()
? "Status code: " + Str(nStatusCode) + ", Status text: " + oRest:ResponseStatusText
oRest:destroy()
oAuthAws:destroy()
oFileData:destroy()
oResponseBody:destroy()
RETURN
ENDIF
? "File successfully uploaded."
oRest:destroy()
oAuthAws:destroy()
oFileData:destroy()
oResponseBody:destroy()