Being able to download, not just stream files, from Amazon S3 - amazon-web-services

I have Amazon S3 where all of my files are stored. Currently my users can go to a link where they can stream, but not download, audio and video files. How can I set up a link through either Amazon S3 or perhaps Amazon CloudFront that will allow someone to download an MP3 file or something of that nature?
Thanks for any advice!

You must set the file's content header to something other than the media type the browser understands. For example:
Content-Disposition: attachment; filename=FILENAME.EXT
Content-Type: application/octet-stream
This used to be a big issue if you wanted to have both features (ability to display/view and ability to download) and you used to have to proxy the file download through your EC2 or other annoying ways. Now S3 has it built in:
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html
You can override values for a set of response headers using the query
parameters listed in the following table. These response header values
are only sent on a successful request, that is, when status code 200
OK is returned. The set of headers you can override using these
parameters is a subset of the headers that Amazon S3 accepts when you
create an object. The response headers that you can override for the
GET response are Content-Type, Content-Language, Expires,
Cache-Control, Content-Disposition, and Content-Encoding. To override
these header values in the GET response, you use the request
parameters described in the following table. (linke above)

Related

How to limit Content-Type header in Amazon s3 Pre-Signed Url

We have a client incorrectly setting the Content-Type to application/json when uploading a pdf file to s3. This results in the client library we use to download the file to give it the wrong extension, also happens when you download the file from the s3 console, even though it has .pdf as the suffix, it downloads to .json.
Is there anyway to prevent clients from doing this? The Content type should always be application/x-www-form-urlencoded from my understanding.

In S3 bucket how to properly configure files in json format to be downloaded or to be read?

My question came out when I experienced two different behaviors in object URL from json files stored in a s3 bucket.
Consider a json file: mydata.json
If I upload this file using s3 dashboard from AWS website, I am able to see data in browser: //s3-us-west-2.amazonaws.com/bucket/folder/mydata.json. I am also able to read this data from a different application if I create a specific configuration in s3 bucket.
For the other hand, if I use boto3 library for python and upload the same file in the same bucket (making file public in the process), when I click object URL it downloads the file, but it doesn't open data in browser.
This is the code I used:
# upload json file
bucket.upload_file(path, jsonkey)
object_acl = s3.ObjectAcl('bucket_name', jsonkey)
bucket_response = object_acl.put(ACL='public-read')
I explored file properties such as metadata. When I upload file via dashboard, the metadata assigned is Content-Type: application/json, and via boto3 is Content-Type: binary/octet-stream. I don't really know if metadata affects the object URL behavior.
In this context, how can I properly configure files in json format to be downloaded or to be read? I mean, what is the main configuration that affects object URL behavior?
I couldn't find a significant difference between both methods (dashboard and boto3) in properties or permissions, besides Content-Type in metadata. However, when I tried to change Content-Type, behavior was the same.
Any other information I can provide to clarify this question, be free to ask. Thanks in advance.
The documentation for the S3 bucket resource's upload_file() method is not ideal as it simply refers you to the equivalent S3Transfer docs for how extra arguments can be used.
Try the following:
bucket.upload_file(path, jsonkey, ExtraArgs={'ContentType': "application/json"})

Render image from S3 in IE11/Edge with Content-Type: application/octet-stream

I have a set of images stored on S3 that should be displayed on the browser. These images have a content type of application/octet-stream and can be viewed in Chrome and Firefox.
My understanding is that Internet Explorer cannot view application/octet-stream content, or it is unable to realize that the S3 object is actually an image.
I've tried to upload new versions of my images onto S3 and manually adding a Metadata Header/Value pair of Content-Type and image/png (using the S3 console, not CLI). However, I still see the same application/octet-stream on IE.
Is it possible to configure my application (Angular4/SpringBoot/Tomcat) to tell IE to look at that type of content, or am I looking at the wrong place in S3?
From my backend, where I set my AWS S3 api I was setting the response headers to 'application/octet-stream'.

Is it possible to change Amazon S3 response header `Server`?

From AWS S3, I can see that Server is set to AmazonS3.
I'm wondering if it's possible to change that to other name?
Thanks.
Yes it is, please see these blogposts 1, 2.
https://nvisium.com/resources/blog/2017/08/10/lambda-edge-cloudfront-custom-headers.html
http://lukaszielinski.de/blog/posts/2018/07/10/aws-how-to-change-s3-response-headers/
Edit to update 2019 - please see #scrrr's answer below for a more up to date answer since the introduction of Lambda # Edge (released a year after the answer below): https://stackoverflow.com/a/51269162/6720449
As far as I've seen, it's not possible to change it intrinsically with S3. See Common Response Headers for a list of headers.
Regarding Server header it says:
The name of the server that created the response.
Type: String
Default: AmazonS3
If you try and modify an objects header in the S3 console, the only headers you can change are:
Cache-Control
Content-Disposition
Content-Type
Content-Language
Expires
Content-Encoding
Website Redirect Location
Also, you can specify custom headers if they start with x-amz-meta-.
If you need to present a different Server header, you will need to wrap access to S3 in a custom reverse proxy with header configurability - e.g. nginx on an ec2 instance.

Change content-type in S3 bucket policy for a specific file extension

Currently when I place a property list file (plist extension) to a S3 bucket, it sets the Content-type to application/octet-stream for some reason. It causes issues when I download it in Objective-C (iOS).
How can change the bucket policy so it automatically sets the Content-type to text/plain
You cannot. The default content-type is "application/octet-stream".
Please find below the aws documentation for the same
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
The only option you have is setting the content-type at the time of upload or updating it once the upload is complete.
You can change the Content-Type using the metadata properties of the file in S3 :
If you're on windows, and use CloudBerry, you can have a rule to set the Content-type for plist extension's automatically. I use this for iOS files and it works great. I dont have to remember to set it each time.
Greg