Amazon AWS S3 to Force Download Mp3 File instead of Stream It

13,464

Solution 1

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.

Solution 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 [email protected] 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();
    }
  }

Solution 3

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.

Solution 4

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.

Here's an image http://i.imgur.com/2PWK3xF.jpg

Share:
13,464

Related videos on Youtube

Calua
Author by

Calua

Updated on April 24, 2022

Comments

  • Calua
    Calua about 2 years

    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
    Jonik almost 14 years
    Hmm, what kind of script would that be? Could you do that with e.g. s3cmd (s3tools.org/s3cmd)?
  • NikolaDjokic
    NikolaDjokic almost 14 years
    I am not sure about s3cmd. See this forum post developer.amazonwebservices.com/connect/…. 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.
  • Calua
    Calua almost 14 years
    thanks for your response, I'm using CloudBerry(freeware) to set the content-header. It works fine! thanks a lot
  • davidjbullock
    davidjbullock almost 11 years
    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.
  • Michael J. Calkins
    Michael J. Calkins almost 11 years
    @davidjbullock Not sure if it was or not but that was a poor example. I updated my example to reflect what I do better.
  • Rowe Morehouse
    Rowe Morehouse about 10 years
    Thanks @kgiannakakis, good solution. I set the header with Cloudberry Explorer for S3.