简体   繁体   中英

Amazon S3 force MP3 file download bypassing web server

I've been trying to find a way to force downloads of MP3 files from Amazon S3 but all of the implementations I found would pass the file through the web server example:

S3 --> My Web Server --> Client.

How could I link to a file on Amazon S3 which will force a download from Amazon s3 directly without having the file go through my web server?

your answer is HTML 5

<a href="$download_link" download="$file_name">Click Me</a>

This force browser to save the file like "save "

You need to set these metadata of the S3 object:

Content-Type: "application/octet-stream"
Content-Disposition: "attachment"

Sometime it is enough setting Content-Disposition only. You can do this from the S3 console too or through AWS Api.

You can directly reference any object you upload to your Amazon S3 bucket, as though it were a web server itself.

For example if your buckets name was xyz100 and you had uploaded an mp3 named music.mp3, you could download it directly from S3 using the following link format:

https://s3.amazonaws.com/xyz100/music.mp3

You know that you can upload objects/files to S3 either through the web based Amazon Web Services console, or use any free software tool.

When the objects are uploaded to S3, you can specify the ACL or security policy for the object. By default objects are private, and if you tried to access the file as shown above you would get an access denied error message.

The first way to solve this is to make the object public, which eliminates the need for any special signatures in the url itself. However making an object public can become a risk if you think the file could be abused with high-bandwidth downloads, or deep linked on other people's websites.

The second way is to leave the object private, and create a direct url that includes a signature. This requires a little more trickery, but there are PHP toolkits that can easily create signed links for you, including a toolkit that Amazon provides themselves. Otherwise the Amazon S3 web console allows you to create signed links.

The most important thing about signed links is that you can specify a time in which the link will expire. This can be a lifesaver, in that you can create links on the fly, that will expire in 30 minutes for example. This helps prevent abuse, and deep linking, but of course is the most complicated manner of publicly using private S3 objects, and of course a server is required.

Though technically possible to have some type of client side JavaScript sign the links for you, the client code would need to include your access key and secret key, which would be a major security hole, no matter how much you obfuscate it. Rule #1, never create any item that goes into the wild, that includes your amazon keys, no matter how scrambled they are.

Here then would be an example of what the above object's direct url would look like if it was private and a signature was required:

https://s3.amazonaws.com/xyz100/music.mp3?Expires=1687832834&Signature=GTje51Mo47BfkGqS1gO0Ns%2FrHUk%3D

You do not need to pass the objects actual data through your web server, you can issue any user a direct link to S3 using the methods detailed above. Note to make sure to set the Content-Type correctly when uploading, most tools do this automatically. If it is not set correctly, the file will not open correctly. This is what controls for example if a picture is downloaded and saved (incorrect Content-Type or default binary type), or simply shown in the browser (correct Content-Type).

Good luck, hope this helps.

made the files public, you shouldn't need any sort of AWS client library to access it. You can just grab the file using your file retrieval mechanism of choice.

You need to change the Content-Type. From the S3 console, right click on the object and select Properties then it's under Metadata. You can also do it programmatically: http://docs.amazonwebservices.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/change_content_type

<?php
$file = $_GET['file'];
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$file.";");
header("Content-Length: ".filesize($file));
readfile($file);
exit;
?>

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