Reputation: 6805
I have a bunch of videos stored on my Amazon S3 storage. I'm working on creating a PHP script, very similar to the one here, where users can download the videos to their hard drive.
I'd like to use something like this:
<?php
$file_name = $_GET['file'];
$file_url = 'http://www.myamazons3.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
However, I am under the impression that this increases the bandwidth because the video will be coming through my server.
Any ideas on how I might be able to force the download of these videos, while avoiding reading it first through my own server?
Many thanks!
Upvotes: 2
Views: 7294
Reputation: 10360
Take a look at the S3 API Docs, and note the header values that you can set. Amazon will send these when the file is requested: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html (the same parameters can be sent with a POST to update an existing object)
Upvotes: 2
Reputation: 6935
I know this is an old thread, but within the past year, the S3 team has added support for request parameters, which allow you to override certain HTTP headers upon request.
You could, for example, upload an SVG image as image/svg+xml
, but then override the headers when retrieving as application/octet-stream
to force a download.
Using the AWS SDK for PHP, you can use the Aws\S3\S3Client::createPresignedUrl() method to achieve this.
Upvotes: 0
Reputation: 142
The php script you mentioned will work ok, but the main downside is that every time a visitor on your site requests a file, your own servers will load it from the S3 and then relay that data to the browser. For low traffic sites, it's probably not a big deal, but for high traffic ones, you definitely want to avoid running everything through your own servers.
Luckily, there's a fairly straight-forward way to set your files to be forced to download from the S3. You just want to set the content-type and content-disposition (just setting content-disposition will work in some browsers, but setting both should work in all browsers).
This code is assuming that you're using the Amazon S3 PHP class from Undesigned:
"application/octet-stream", "Content-Disposition" => "attachment")); ?>Now all your files will be forced to download. You may need to clear your cache to see the change. And obviously, don't do that on any file that you actually do want to be loaded "inline" in the browser.
The nice part with this solution is that applications that load media files directly (like let's say an mp3 player in Flash) don't care about the content-type or content-disposition, so you can still play your files in the browser and then link to download that same file. If the user already finished loading the file in flash, they'll most likely still have it in their cache, which means their download will be super quick and it won't even cost you any extra bandwidth charges from the S3.
Upvotes: 1