Last active
October 18, 2021 19:19
-
-
Save Rugby-Ball/e3b2c8c4eab3f4b281fdeda468b336b4 to your computer and use it in GitHub Desktop.
Download a single file from an S3 bucket to your local computer. #S3 #Public #AWS #Utility #File_System #Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Download a single file from an S3 bucket to your local computer. | |
| # AWS accessKey and secretKey can be removed below and from Copy-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 Copy-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 the S3 file to. | |
| $filepath = "c:\temp\temp" #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-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. | |
| #This is used to download the file. This sets up the variables, and also does a check if the destination folder exists. | |
| #Check and Create Local Folder. | |
| ##Check if $filepath exists, if it doesnt create it. | |
| If (-not(Test-Path -Path $filepath)) | |
| { New-Item -ItemType Directory -Force -Path $filepath } | |
| # There are a few options downloading a file from an S3 bucket. I Commented out the two options, if you want to try this you will need to uncomment out one of the lines. | |
| # Copy-S3Object -BucketName $s3Bucket -Key $s3prefixedfilename -LocalFolder $filepath -Region $region -AccessKey $accessKey -SecretKey $secretKey # Uses -localfolder to tell the CMDLet where to download the file on you local computer. | |
| # NOTE, if the file you getting is in subfolders on the S3 bucket, it will create those folder in the local computers folder, using example above, the file is located in the S3 bucket at: "For_DBA/Backup_SQLdev/backupfilexxx.bak" | |
| # And the Copy-S3Object CMDlet is using -LocalFolder $filepath, which the $filepath variable is set to C:\temp\temp, so the file will be written at C:\temp\temp\For_DBA\Backup_SQLdev\backupfilexxx.bak | |
| # | |
| # If you want to download the file only and not recreate the folder structure the file is on in S3, you would use -localfile but you must also include the name of the file. | |
| # | |
| #Copy-S3Object -BucketName $s3Bucket -Key $s3prefixedfilename -LocalFile $fullpath -Region $region -AccessKey $accessKey -SecretKey $secretKey # Uses -localfile. If you use this it will not create the subfolders that the file is on | |
| # in S3 Bucket. But you do need to provide the name to give the file to copy locally. It can be the same file name thats on the S3 Bucket, or any name you want to change it to. | |
| #This would be the one-liners if you didnt want to make it modular. | |
| # Copy-S3Object -BucketName "Your-S3-Bucket-Here" -Key "For_DBA/Backup_SQLdev/backupfilexxx.bak" -LocalFolder "c:\temp\temp" -Region us-east-1 -AccessKey $accessKey -SecretKey $secretKey # Uses -localfolder to tell the CMDLet where to download the file on you local computer. | |
| # | |
| # Copy-S3Object -BucketName "Your-S3-Bucket-Here" -Key "For_DBA/Backup_SQLdev/backupfilexxx.bak" -LocalFile "c:\temp\temp\backupfilexxx.bak" -Region us-east-1 -AccessKey $accessKey -SecretKey $secretKey # Uses -localfile. If you use this it will not create the subfolders that the file is on | |
| # in S3 Bucket. But you do need to provide the name to give the file to copy locally. It can be the same file name thats on the S3 Bucket, or any name you want to change it to. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment