Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Last active October 18, 2021 19:21
Show Gist options
  • Select an option

  • Save Rugby-Ball/8e5149d0fa90a2b6aac0e39e529e0da5 to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/8e5149d0fa90a2b6aac0e39e529e0da5 to your computer and use it in GitHub Desktop.
Uploading a single file to an S3 Bucket using PowerShell. #S3 #Public #AWS #Utility #File_System #Powershell
#Uploading a single file to an S3 Bucket using PowerShell.
# AWS accessKey and secretKey can be removed below and from Write-S3Object if running on an EC2 instance and using IAM roles for security.
$accessKey = "Access Key Here"
$secretKey = "Secret Key Here"
# I broke it out like this to make it more modular, but you could just add this information directly to the write-S3object cmdlet if you wanted to make
# a one liner to do this.
#This section is setting up the variables for the local file you want to move to S3.
$filepath = "c:\temp\backupFiles" #This is the path where the file you want to move from Windows machine to S3 resides.
$filename = "backupfilexxx.bak" #This is the file you want to move, it should be found in the path in
$fullpath = Join-Path $filepath $filename # This will join the File path and file name into a full path.
#This section sets up the variables for the S3 part of the script.
$region = "us-east-1" #This is the region in AWS the S3 Bucket resides.
$s3Bucket = "Your-S3-Bucket-Name-Here" #This is the S3 Bucket you will be working with.
$s3keyprefix = "For_DBA/Backup_SQLdev/" #This would be the Sub-Folders inside the S3 Bucket to put the file.
$s3prefixedfilename = $s3keyprefix+$filename # This joins the KeyPrefix and the filename. Its used to tell the write-s3object cmdlet where to write the file.
#
#Note: Quick lesson on S3: So S3 is not actually a file system, it is actually a Database that is made to look like your working with a Files System.
# The Database is the Bucketname, and all the files in that bucket go in with the folder structure appended to the front of the file name.
# So if you pushed up a file "file1.txt" to the root of the S3 Bucket, AWS would rename the file in the background to
# /file1.txt And in that S3 Bucket if you had a sub-folder called For_DBA it would rename that file to For_DBA/file1.txt
# S3 will parse the file on the / to define the subfolders and build the illusion of a File System. You will not see the files names with the /
# in the file name, it hides that from the user.
# But again, its not a file system. It's a Key/Value Database.
Write-S3Object -BucketName $s3Bucket -file $fullpath -Key $s3prefixedfilename -Region $region -AccessKey $accessKey -SecretKey $secretKey
## If you wanted to make this a one liner instead of modular like above, here is what it would look like (I commented the line so it wont run, but this one line does what all the lines above do, just in one line.)
#Write-S3Object -BucketName Your-S3-Bucket-Name-Here -file "c:\temp\backupFiles\backupfilexxx.bak" -Key "For_DBA/Backup_SQLdev/backupfilexxx.bak" -Region "us-east-1" -AccessKey "Access Key Here" -SecretKey "Secret key here"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment