Force S3 PDF to be viewed in browser instead of download - amazon-web-services

So you can force a download by using Content-Disposition: attachment
Content-Disposition: inline is the default and should display in the browser, and it does in fact work with most files like PNG, JPG, etc.
But for some reason somehow when generating a presigned URL from S3, PDF files will always force download even if I don't use the content-disposition: attachment header.
I want to be able to make the PDF open in the browser when the browser allows it
I am using the presigned URL generate call from S3 client http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURLJavaSDK.html

Check you file's metadata and remove Content-Disposition entry from that file. and set content type according to the file type.
Like for
text file Content-Type='text/plain'
image png Content-Type='image/png'
pdf Content-Type=application/pdf
pdfxml Content-Type=application/vnd.adobe.pdfxml
If your file's Content-Type is binary/octet-stream then it will download instead of display.
Thanks

You have to set your putObject params as follows to view pdf instead of download.
params = {
Bucket: process.env.S3_BUCKET,
Key: <fileName>,
Body: <fileContent>,
ContentDisposition:"inline",
ContentType:"application/pdf"
};

You also need to set the Content-Type correctly. The browser will check the content-type value, and if it isn't something it knows how to display it will always just download the file.

You need to provide both these meta data inorder to view the file instead of download:
Content-Disposition: attachment
Content-Type: application/pdf

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.

Upload file test with Postman

I am testing a file upload to SharePoint online with Postman. In my post request I set the body to form-data and add a file param with a text file called a.txt. The text file contains the text
one
two
three
When I execute the request my file is uploaded but when I open the file the contents is
----------------------------235004993628819232914336
Content-Disposition: form-data; name="file"; filename="a.txt"
Content-Type: text/plain
one
two
three
----------------------------235004
I have tried setting the Content-Type header to
application/x-www-form-urlencoded
or
multipart/form-data
But no luck
Please follow the below steps-
1. Go to the body of the API
2. pass the parameter or key
3. When you pass the key you have a option of value of Text/File like below
4.Choose the file from
5. Click on send.
6. Get your response.
Hope this helps buddy.
In my case setting the body to type binary solved my problem with testing with Postman

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'.

Get download file format and name in VC++

I am using Visual C++ to implement a function that could download image from a query.
(for example http://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap
&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318
&markers=color:red%7Ccolor:red%7Clabel:C%7C40.718217,-73.998284&sensor=false)
Note that there's no image information associate with the query itself.
Now I can successfully download it, but I want to know if there is a way I can find the download image format (i.e. jpg or png, etc) and the image name?
Edit: if you try to save the image download from the above link, you can see the browser could automatically save it as staticmap and with extension png. I was wondering where the browser could get this information?
In this case the server is responding with the content type in the HTTP response headers:
Content-Type: image/png
This is the "Internet media type" or "MIME type" of the response content.

Being able to download, not just stream files, from Amazon S3

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)