AWS S3: Force File Download using 'response-content-disposition'

空扰寡人 提交于 2019-12-22 10:43:59

问题


Few lines(below) generate signed URL to which browser is redirected to download a file from S3.

I am facing well known issue of Chrome not downloading pdf files from S3 when content type is set as Octet Stream.

The solution is to force chrome to download file instead of trying to read/open it.

I tried adding 'response-content-disposition' to sign as well as URL, but it didn't work.

How to use 'response-content-disposition' header when url is to be generated?

    String codedFilename=  EncodingUtil.urlEncode(bucketPath,'UTF-8');

    String stringtosign = 'GET\n\n\n'+Lexpires+'\n/'+bucketName+'/'+codedFilename;

    String signed = make_sig(stringtosign); 

    String codedsigned = EncodingUtil.urlEncode(signed,'UTF-8');

    String url = serverURL+'/'+bucketName+'/'+codedFilename+'?AWSAccessKeyId='+awskey+'&Expires='+Lexpires+'&Signature='+codedsigned;

Note:- This is salesforce-apex syntax.


回答1:


    String codedFilename=  EncodingUtil.urlEncode(bucketPath,'UTF-8');

    String stringtosign = 'GET\n\n\n'+Lexpires+'\n/'+bucketName+'/'+codedFilename+'?response-content-disposition=attachment; filename=abc.doc';

    String signed = make_sig(stringtosign); 

    String codedsigned = EncodingUtil.urlEncode(signed,'UTF-8');

    String url = serverURL+'/'+bucketName+'/'+codedFilename+'?response-content-disposition='+EncodingUtil.urlEncode('attachment; filename=abc.doc','UTF-8')+'&AWSAccessKeyId='+awskey+'&Expires='+Lexpires+'&Signature='+codedsigned;

Source: Unable to override content disposition header in s3

Chrome has problem if content type is 'application/octet-stream' for a document(pdf int this case). However chrome is OK if content type is 'binary/octet-stream'.

Thankfully S3 defaults Content-Type of download to 'binary/Octet-stream' if nothig is set at the time of upload.

My Advice:- If one needs to files of 'any' type to S3 without knowing content type at the time of upload, simply do not set it or set it to 'binary/Octet-stream'. This way Chrome will show warning but file is downloaded.




回答2:


Here is some Ruby code that signs an S3 url and sets response-content-disposition. This works to force Chrome to download a PDF instead of rendering it inline.

https://gist.github.com/diamondap/9134867



来源:https://stackoverflow.com/questions/19046718/aws-s3-force-file-download-using-response-content-disposition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!