Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing Download from s3 amazon servers

Tags:

I've been developing a new web application which relies on Amazon S3 servers as storage system, and Codeiginter as the PHP framework.

I need to force the file to download when the link is clicked. The original URL looks like this:

http://www.our-web.com/download/do/1.jpg

which generates a temporary signed URL to the actual file on the Amazon S3 servers like this:

http://main_bucket.s3.amazonaws.com/post/1/1.jpg?AWSAccessKeyId=AKIAJEOQKYPKC3CCU5RA&Expires=1305395426&Signature=iuzCdA22gImLK192%2BMAhk8OkAY8%3D

I need to make the file start downloading from the real Amazon URL it soon as the user clicks the link.

I have two ways now to do so:

  1. Use redirect() which will open the file not download it; or
  2. Alter headers as this code:

    header('Content-type: application/force-download');
    header('Content-Disposition: attachment; filename=' . $file_name);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 4000');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($generated_file));
    
    readfile($generated_file);
    

Unfortunately, both ways don't help me. The second method causes the download to come from my website and not from directly from Amazon.

How can I force the file to download directly from the Amazon S3 servers, and not from my website?

like image 859
Khaled Avatar asked May 14 '11 17:05

Khaled


People also ask

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.

Does downloading from S3 cost money?

The data transfer between two EC2 in different AZ costs $0.02/GB, but S3 is free to download from any AZ. Consider the scenario where 1 GB data is transferred 20 times from one EC2 server to another in different availability zone. It will cost $0.20/GB (20 * 0.01).


3 Answers

You just need to set the correct headers on your files in S3 in order to force the browser to download rather than opening the file. Set these:

Content-Disposition: attachment; filename=FILENAME.EXT Content-Type: application/octet-stream 

You will need to set them when uploading the files to S3. With the php SDK you'd use create_object.

Or you can set these after uploading using 'change_content_type' or by copying the file to itself in S3 and setting the correct headers.

like image 133
Geoff Appleford Avatar answered Nov 03 '22 10:11

Geoff Appleford


Bit late to the party, but often times with a file you don't want to have to decide at storage time how it will be used. You want to be able to store the file once, then in one area possibly embed the file or display in browser, and in another area enable the user to download the same file.

Fortunately you can do that by providing override parameters in the request url. It only works with signed requests, but thankfully you're already doing that.

If you add a parameter like &request-content-type="application/force-download" that should do the trick.

Check out the Request Parameters section of the S3 GET Object documentation: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html

like image 44
Tom McQuarrie Avatar answered Nov 03 '22 09:11

Tom McQuarrie


The question is about setting this programmatically, but to do so manually through the AWS console:

  • Select a file in S3.
  • Properties > Metadata > Add more metadata
  • Key: Content-Disposition Value: Attachment
  • Save
like image 42
some ideas Avatar answered Nov 03 '22 10:11

some ideas