5

I'm using Amazon S3 to put the mp3 file then allow our site visitor to download the mp3 from Amazon AWS. I use S3Fox to manage the file, everything seems working fine until recently we got many complaints from visitor that the mp3 was streamed via the browser instead of displaying browser save dialog. I try for some mp3 and notice that for some mp3, the save dialog box is appear, and for some others they're streamed via browser. What can I do to force that the mp3 file will be downloaded instead of streamed via web browser....

Any help would be much appreciated. Thanks

Jonik
  • 80,077
  • 70
  • 264
  • 372
Calua
  • 495
  • 4
  • 11
  • 18

4 Answers4

9

In order to do so you need to set the Content-Disposition header:

Content-disposition: attachment; filename=song.mp3

I don't think this is possible with S3Fox. You could use Bucket Explorer (not free) or write a script to upload the files.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Hmm, what kind of script would that be? Could you do that with e.g. s3cmd (http://s3tools.org/s3cmd)? – Jonik May 19 '10 at 16:54
  • I am not sure about s3cmd. See this forum post http://developer.amazonwebservices.com/connect/thread.jspa?messageID=67395. You can use the REST API to set the headers. Amazon provides libraries for many languages (Ruby, python, C#). You can use the language of your choice to write the script. – kgiannakakis May 20 '10 at 07:39
  • thanks for your response, I'm using CloudBerry(freeware) to set the content-header. It works fine! thanks a lot – Calua May 21 '10 at 10:35
  • Thanks @kgiannakakis, good solution. I set the header with Cloudberry Explorer for S3. – Rowe Morehouse Feb 12 '14 at 18:32
2

This ended up being my solution for force downloading files from AWS S3.

In safari the files were downloading as .html files until I stopped returning the readfile and just ran the function alone.

  public function get_download($upload_id)
  {
    try {
      $upload = Upload::find($upload_id);
      if ($upload->deleted)
        throw new Exception("This resource has been deleted.");

      if ($upload->filename == '')
        throw new Exception("No downloadable file found.  Please email info@clouddueling.com for support.");

      header("Content-Description: File Transfer");
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename={$upload->uploaded_filename};");

      readfile("https://s3.amazonaws.com/stackoverflow/uploads/" . $upload->filename);
      exit;
    } catch(Exception $e) {
      return $e->getMessage();
    }
  }
Michael J. Calkins
  • 32,082
  • 15
  • 62
  • 91
  • 6
    This doesn't appear to be forcing the download from S3, instead you're pushing the file back from S3 through your webserver, tying up your webserver resources to force the download. – davidjbullock Jun 19 '13 at 16:11
  • @davidjbullock Not sure if it was or not but that was a poor example. I updated my example to reflect what I do better. – Michael J. Calkins Jun 20 '13 at 00:22
2

Ok, it's been a long time since you ask this, but I had the same problem and I'd like to share my solution with the community, just in case someone else need to solve this thing. Of course, you can change Content-Type and Content-Disposition from the Amazon S3 Console, but the interesting thing is to do it programmatically.

The following code works fine for me:

require_once '../sdk-1.4.2.1/sdk.class.php';

// Instantiate the class
$s3 = new AmazonS3();

// Copy object over itself and modify headers
$response = $s3->copy_object(
    array( // Source
        'bucket' => 'your_bucket',
        'filename' => 'Key/To/YourFile'
    ),
    array( // Destination
        'bucket' => 'your_bucket',
        'filename' => 'Key/To/YourFile'
    ),
    array( // Optional parameters
        'headers' => array(
            'Content-Type' => 'application/octet-stream',
            'Content-Disposition' => 'attachment'
        )
    )
);

// Success?
var_dump($response->isOK()); 

Hope it can helps other struggling with the same trouble.

Jorge
  • 259
  • 1
  • 7
  • 15
1

In s3 management console window, right click and chose properties.

Click on metadata.

Click on add more metadata

Key: content-disposition Value: attachment

Save. That's all.

j0k
  • 22,600
  • 28
  • 79
  • 90
vezycash
  • 11
  • 1