Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS CLI get download S3 URL for private bucket from AWS CLI

I could upload a file to a private S3 bucket successfully using following command:

aws s3 cp "myfile.txt" "s3://myfolder/myfile.txt" --region=us-east-1 --output=json

I would like to issue a AWS CLI command to return me a temporary URL download for myfile.txt and does anyone know how to?

I googled and look like I have to do some signing to get temporary URL such as: http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html

like image 760
Nam Nguyen Avatar asked Jan 27 '14 22:01

Nam Nguyen


People also ask

How do I download from S3 bucket to AWS CLI?

You can use cp to copy the files from an s3 bucket to your local system. Use the following command: $ aws s3 cp s3://bucket/folder/file.txt .

How do I download S3 bucket from aws?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.

How do I access private S3 bucket files?

Restrict access to your S3 resources. By default, all S3 buckets are private and can be accessed only by users who are explicitly granted access. Restrict access to your S3 buckets or objects by doing the following: Writing IAM user policies that specify the users that can access specific buckets and objects.


2 Answers

aws cli now supports presign command. You can run

$ aws s3 presign s3://test-bucket/test-file.txt https://test-bucket/test-file.txt?Expires=1499152189&Signature=some-sha 

This will generate an url which you can share it with anyone to download that file in 3600 seconds.

You can change time period with --expires-in

$ aws s3 presign s3://test-bucket/test-file.txt --expires-in 600  

The generated url will expire in 10 minutes.

You can read more about presign in aws cli docs.

like image 60
Pandikunta Anand Reddy Avatar answered Oct 11 '22 07:10

Pandikunta Anand Reddy


You can use the following URL format:

https://<bucket-name>.s3.amazonaws.com/<object or key name> 

or old style:

https://s3.amazonaws.com/<bucket-name>/<object or key name> 

To have it accessible you need to allow public access to your object or attach an appropriate bucket policy.

For example the following bucket policy shows public access to bucket zzzyyy object 'yyyeee'

$ aws s3 get-object-acl --bucket zzzyyy --key yyyeee {     "Owner": {         "DisplayName": "owner",         "ID": "Some hash of owner"     },     "Grants": [         {             "Grantee": {                 "DisplayName": "owner",                 "ID": "Some hash of owner"             },             "Permission": "READ"         },         {             "Grantee": {                 "DisplayName": "owner",                 "ID": "Some hash of owner"             },             "Permission": "WRITE"         },         {             "Grantee": {                 "DisplayName": "owner",                 "ID": "Some hash of owner"             },         "Permission": "READ_ACP"         },         {             "Grantee": {                 "DisplayName": "owner",                 "ID": "Some hash of owner"             },             "Permission": "WRITE_ACP"         },         {             "Grantee": {                 "URI": "http://acs.amazonaws.com/groups/global/AllUsers"             },             "Permission": "READ"         },         {             "Grantee": {                 "URI": "http://acs.amazonaws.com/groups/global/AllUsers"             },             "Permission": "READ_ACP"         }     ] } 

You can see examples of bucket policies here:

http://docs.aws.amazon.com/AmazonS3/latest/dev/AccessPolicyLanguage_UseCases_s3_a.html

You can also use the S3 Console as seen here:

S3 Console

like image 25
Rico Avatar answered Oct 11 '22 08:10

Rico