简体   繁体   中英

S3 Force Download

I have a website here http://www.voclr.it/acapellas/ my files are hosting on my Amazon S3 Account, but when a visitor goes to download an MP3 from my website it forces them to stream it but what I actually want it to do is download it to there desktop.

I have disabled S3 on the website for now, so the downloads are working fine. but really I want S3 to search the MP3s

Basically, you have to tell S3 to override the content-disposition header of the response. You can do that by appending the response-content-disposition query string parameter to the S3 file url and setting it to the desired content-disposition value. To force download try:

<url>&response-content-disposition="attachment; filename=somefilename"

You can find this in the S3 docs . For information on the values that the content-disposition header can assume you can look here .

As an additional information this also works with Google Cloud Storage.

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());

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

I created a solution for doing this via CloudFront functions (no php required since it all runs at AWS by linking to the.mp3 file on Cloudfront with a?title=TitleGoesHere querystring to force a downloaded file with that filename). This is a fairly recent way of doing things (as of August 2022). I documented my function and how I set up my "S3 bucket behind a CloudFront Distribution" here: https://stackoverflow.com/a/73451456/19823883

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM