Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I copy a "folder" in s3 in one command using powershell?

I have an s3 bucket with prefixes for stage and production. After QA, I want to copy all objects from stage to production. I'm trying to understand how I can do this with one command.

My googling only brings up the following, which is for copying a single object:

http://docs.aws.amazon.com/powershell/latest/reference/items/Copy-S3Object.html

I was able to upload a folder at one time in the following way

Write-S3Object -BucketName $bucketName -Folder $myfolder -Keyprefix $Keyprefix

But the Copy-S3Object command it doesn't seem to accept a prefix and a destination prefix, for example.

What's the best way to do this?

like image 281
Eric Wilson Avatar asked Aug 31 '25 22:08

Eric Wilson


2 Answers

OK, looks like the AWS CLI is a good solution in many situations, but I'm actually constrained to use Powershell here.

Here is my solution:

$objects = Get-S3Object -BucketName $bucketName -KeyPrefix $stageKeyPrefix

foreach($object in $objects) {
    $prodKey = $object.Key -replace $stageKeyPrefix, $prodKeyPrefix
    Copy-S3Object -BucketName $bucket -Key $object.Key -DestinationKey $prodKey
}
like image 153
Eric Wilson Avatar answered Sep 03 '25 11:09

Eric Wilson


Believe you can only do this through the AWS Command Line Interface, see below snippet as example.

$folder = "c:\temp\MyDirectory\"
$bucket = "AWS BUCKET"
$folder = "FOLDER PREFIX"

aws s3 cp $folder s3://$bucket/$version/ --recursive
like image 33
jarnohenneman Avatar answered Sep 03 '25 11:09

jarnohenneman