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:
- Use
redirect()which will open the file not download it; or -
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?
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:
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.