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

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



Solution 1:[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:[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.

Solution 3:[3]

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 4:[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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kgiannakakis
Solution 2 Jorge
Solution 3
Solution 4 j0k