Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create download link for an Amazon S3 bucket's object?

I am learning Amazon S3 using S3 PHP Class. I have uploaded my all files to my S3 bucket, now I want to create links for each available file in my bucket.

Will the following function work for me?

public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false)
{

}

    $s3 = new S3('access-key', 'secret-key');
    $s3->getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false);

Or another function like get_object_url, but get_object_url() not in my S3 class.

I am using Undesigned's Amazon S3 PHP class.

like image 793
Frank Avatar asked May 19 '12 07:05

Frank


People also ask

How do I create a URL for Amazon S3?

To generate a presigned URL using the AWS Management ConsoleSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for.

How do I download data from aws S3?

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 can you download an S3 bucket including all folders and files?

aws s3 sync s3://mybucket . will download all the objects in mybucket to the current directory. This will download all of your files using a one-way sync. It will not delete any existing files in your current directory unless you specify --delete , and it won't change or delete any files on S3.


2 Answers

The following patterns are valid for constructing S3 URLs:

http(s)://<bucket>.s3.amazonaws.com/<object>
http(s)://s3.amazonaws.com/<bucket>/<object>
like image 145
hari maliya Avatar answered Oct 24 '22 16:10

hari maliya


If you want the public to access the bucket, it is as simple as

http://[YourBucketName].s3.amazonaws.com/[YourFileName]

So long as you set permissions correctly.

If you're worried about download abuse, you'll want an authenticated URL (which I guess you want from your code sammple). In which case, I suggest you use the Amazon SDK: http://aws.amazon.com/sdkforphp/ as it contains examples of what you need.

$s3->getObjectUrl($bucket, $filename, '5 minutes');

Docs: http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl

like image 29
Robbie Avatar answered Oct 24 '22 14:10

Robbie