AWSPowerShell

How to Find and Download Files from Amazon S3 Using PowerShell

205 VIEWS

·

Before you can create a script to download files from an Amazon S3 bucket, you need to:

  •  Install the AWS Tools module using ‘Install-Module -Name AWSPowerShell’
  •  Know the name of the bucket you want to connect.
  •  Define the name of the bucket in your script.

Having this info beforehand allows you to store the information as a variable to use later in the script. In the below example, we will use “snip-videos” as our S3 bucket name variable.

$BucketName = ‘snip-videos’

Using the Get-S3Object cmdlet that is part of the AWS Tools for Windows PowerShell module to discover all the contents of our bucket:

Get-S3Object -BucketName $BucketName

If you have several files, the results will easily scroll off the screen! Filtering will greatly reduce the number of returned objects. We can set some parameters based on properties we are looking for. The example below will trim the results down to just one file:

$Params = @{

BucketName = $BucketName

Key = ‘how to assign specific services to users in office 365 using powershell/final.mp4’

}

Now, let’s run the cmdlet again, but this time with the above $Params set:

Get-S3Object -BucketName $BucketName

The results:

As you can see, we are looking in $BucketName for files matching the Key parameter value of ‘how to assign specific services to users in office 365 using powershell/final.mp4’. Alternatively, you can also filter certain prefixes by using the KeyPrefix parameter value, which will allow you to greatly shorten the value to just ‘how to’. Furthermore, you can add the parameter of MaxKey, which allows you to limit the number of results returned. For more information on available filtering parameters, visit the AWS documentation.

In order to download a file from the bucket, you will need to use the Read-S3Object cmdlet.

$Params = @{

BucketName = $BucketName

Key = ‘how to assign specific services to users in office 365 using powershell/final.mp4’

File = ‘D:\TechSnips\tmp\final.mp4’

}

Read-S3Object @Params

From the above example, we’ll once again create an array of parameters. This time we are using the File parameter with a value of ‘D:\TechSnips\tmp\final.mp4’. This is the location where we are storing the file that we want to download and the filename we wish to use. Once the command is run, it may take a moment or two depending on the size of the file being downloaded.

Here are the results of our example:


And that is how you can use PowerShell to not only find but also download files from an Amazon S3 bucket. For more examples of downloading files using advanced parameters and filtering, be sure to watch the companion snip located here: https://www.youtube.com/watch?v=39Kceosmcck


Once upon a time I was a good little sysadmin that dutifully automated everything I could get my hands on. My independent spirit served me well in those days. It taught me to get stuff done and guided me to higher paying employment opportunities. That is until the day that it ganged up with my entrepreneurial spirit and they convinced me that I could do much cooler things on my own. I gleefully drank their koolaid and have since started my own automation consultancy, Howell IT. Besides my career's fairy-tale happily so-far after, somehow I also found a woman that not only wanted to marry me, but decided I was also sane enough to be a father.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu