Sample code for 30+ languages & platforms
Xbase++

S3 Upload Binary File from BinData

See more Amazon S3 (new) Examples

Upload a binary file contained in a BinData object to Amazon S3.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nBAutoReconnect
LOCAL oAuthAws
LOCAL oPngData
LOCAL oSbResponse
LOCAL nStatusCode

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
//  Make sure to connect to the region where the bucket is located..
nSuccess := oRest:Connect("s3-us-west-2.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"
//  Use the correct region..
oAuthAws:Region := "us-west-2"
oAuthAws:ServiceName := "s3"

nSuccess := oRest:SetAuthAws(oAuthAws)

//  Set the bucket name via the HOST header.
//  In this case, the bucket name is "chilkat.qa".
//  (Also make sure to use the correct region.)
oRest:Host := "chilkat.qa.s3-us-west-2.amazonaws.com"

//  Load a text file into memory.
oPngData := CreateObject("Chilkat.BinData")
nSuccess := oPngData:LoadFile("qa_data/png/anemone.png")
IF (nSuccess != 1)
    ? "Failed to load file from local filesystem."
    oRest:destroy()
    oAuthAws:destroy()
    oPngData:destroy()
    RETURN
ENDIF

//  Indicate the Content-Type of our upload.  (This is optional)
oRest:AddHeader("Content-Type", "image/png")

//  Upload the file to Amazon S3.
oSbResponse := CreateObject("Chilkat.StringBuilder")
nSuccess := oRest:FullRequestBd("PUT", "/images/sea_creatures/anemone.png", oPngData, oSbResponse)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oRest:destroy()
    oAuthAws:destroy()
    oPngData:destroy()
    oSbResponse:destroy()
    RETURN
ENDIF

//  Did we get a 200 response indicating success?
nStatusCode := oRest:ResponseStatusCode
IF (nStatusCode != 200)
    ? "Error response: " + oSbResponse:GetAsString()
    ? "Status code: " + Str(nStatusCode) + ", Status text: " + oRest:ResponseStatusText
    oRest:destroy()
    oAuthAws:destroy()
    oPngData:destroy()
    oSbResponse:destroy()
    RETURN
ENDIF

? "File successfully uploaded."

oRest:destroy()
oAuthAws:destroy()
oPngData:destroy()
oSbResponse:destroy()