PowerShell AWS: PUT - Copy Object

Back to Index

This implementation of the PUT operation creates a copy of an object that is already stored in Amazon S3. A PUT copy operation is the same as performing a GET and then a PUT. Adding the request header, x-amz-copy-source, makes the PUT operation copy the source object into the destination bucket. (This example copies starfish.jpg from the chilkat100 bucket to the chilkatdestbucket.)

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


[Reflection.Assembly]::LoadFile("C:\myAssemblies\ChilkatDotNet2.dll")

$rest = New-Object Chilkat.Rest

#   Provide AWS credentials for the REST call.
$authAws = New-Object Chilkat.AuthAws
$authAws.AccessKey = "AWS_ACCESS_KEY"
$authAws.SecretKey = "AWS_SECRET_KEY"
$authAws.ServiceName = "s3"
$authAws.Region = "us-east-1"
$rest.SetAuthAws($authAws)

$success = $rest.Connect("s3.us-east-1.amazonaws.com",443,$true,$true)
if ($success -ne $true) {
    $($rest.LastErrorText)
    exit
}

$rest.Host = "chilkatdestbucket.s3.us-east-1.amazonaws.com"

$rest.AddHeader("x-amz-copy-source","/chilkat100/starfish.jpg")

$sbReq = New-Object Chilkat.StringBuilder

$sbXml = New-Object Chilkat.StringBuilder
$success = $rest.FullRequestSb("PUT","/starfish.jpg",$sbReq,$sbXml)
if ($success -ne $true) {
    $($rest.LastErrorText)
    exit
}

if ($rest.ResponseStatusCode -ne 200) {
    $("Received error response code: " + $rest.ResponseStatusCode)
    $("Response body:")
    $($sbXml.GetAsString())
    exit
}

$xml = New-Object Chilkat.Xml
$xml.LoadSb($sbXml,$true)

#  The following code parses the XML response.
#  A sample XML response is shown below the sample code.

$LastModified = $xml.GetChildContent("LastModified")
$ETag = $xml.GetChildContent("ETag")

$("Example Completed.")

Sample XML Response Body

<CopyObjectResult>
   <LastModified>2009-10-28T22:32:00</LastModified>
   <ETag>"9b2cf535f27731c974343645a3985328"</ETag>
 </CopyObjectResult>