Xojo Plugin AWS S3: Copy S3 File to a Different Bucket

Back to Index

Demonstrates how to copy an S3 file from one bucket to another. The file can also be renamed if desired. (The file already exists in S3 in one bucket, and is copied without downloading/uploading to another S3 bucket.) This example copies from /chilkat.qa/starfish.jpg to /chilkat.ocean/starfishCopy.jpg

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html

CURL Command

curl -X PUT https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg \
    -H "x-amz-copy-source: /chilkat.qa/starfish.jpg"

Xojo Plugin Example

Dim rest As New Chilkat.Rest
Dim success As Boolean

Dim authAws As New Chilkat.AuthAws
authAws.AccessKey = "AWS_ACCESS_KEY"
authAws.SecretKey = "AWS_SECRET_KEY"
authAws.Region = "us-west-2"
authAws.ServiceName = "s3"
success = rest.SetAuthAws(authAws)

//  URL: https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg
Dim bTls As Boolean
bTls = True
Dim port As Int32
port = 443
Dim bAutoReconnect As Boolean
bAutoReconnect = True
success = rest.Connect("chilkat.ocean.s3.us-west-2.amazonaws.com",port,bTls,bAutoReconnect)
If (success <> True) Then
    System.DebugLog("ConnectFailReason: " + Str(rest.ConnectFailReason))
End If

success = rest.AddHeader("x-amz-copy-source","/chilkat.qa/starfish.jpg")

Dim sbResponseBody As New Chilkat.StringBuilder
success = rest.FullRequestNoBodySb("PUT","/starfishCopy.jpg",sbResponseBody)
If (success <> True) Then
End If

Dim respStatusCode As Int32
respStatusCode = rest.ResponseStatusCode
If (respStatusCode >= 400) Then
    System.DebugLog("Response Status Code = " + Str(respStatusCode))
    System.DebugLog("Response Header:")
    System.DebugLog("Response Body:")
End If

Dim xmlResponse As New Chilkat.Xml
success = xmlResponse.LoadSb(sbResponseBody,True)

//  See the Online Tool for Generating XML Parse Code
Dim CopyObjectResult_xmlns As String
Dim tagPath As String
Dim LastModified As String
Dim ETag As String

CopyObjectResult_xmlns = xmlResponse.GetAttrValue("xmlns")
LastModified = xmlResponse.GetChildContent("LastModified")
ETag = xmlResponse.GetChildContent("ETag")

Sample XML Response Body

<?xml version="1.0" encoding="UTF-8"?>
<CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">