PowerShell Script for Uploading a Local Directory to an S3 Bucket on AWS

For those needing to upload several files to S3 that are on a local directory, I’ve put together a simple PowerShell snippet for sending all files in a directory to a specified S3 bucket.

PowerShell S3 Upload

If you have sub-directories within a directory, those will also be copied up as long as there are files within them.

This script makes use of the AWS CLI:

1
2
3
4
$bucketName = "Your Bucket Name"; #For example, "s3://my-bucket-name"
$relativePath = "Your Relative Path"; #For example, "objects" or "myfolder/objects"
aws s3 sync $relativePath $bucketName;
Write-Host "Done";

You can also download the file here.

Don’t forget to swap in the name of your bucket name and the relative path of the directory from where you execute the script in the appropriate variables. And keep in mind that you can also choose to use an absolute path to the source directory instead of relative.

If instead you want to download objects from an S3 bucket to your local directory, just swap the order of the variables in the command like this:

1
aws s3 sync $bucketName $relativePath;

This uses the ‘sync’ command within the S3 library of the AWS CLI. You can find the reference for that here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s