AWS S3:使用'response-content-disposition'强制下载文件]]

问题描述 投票:3回答:3

以下几行生成签名的URL,浏览器重定向到该URL,以便从S3下载文件。

[当内容类型设置为八位位流时,我面临一个众所周知的Chrome无法从S3下载pdf文件的问题。

解决方案是强制chrome下载文件,而不是尝试读取/打开文件。

我尝试添加'response-content-disposition'来进行签名以及URL,但是没有用。

要生成url时如何使用'response-content-disposition'标头?

    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;

注意:-这是salesforce-apex语法。

以下几行会生成签名的URL,浏览器将重定向到该URL以从S3下载文件。当内容类型设置为...

browser amazon-web-services amazon-s3 content-type content-disposition
3个回答
4
投票
    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;

0
投票

这里有一些Ruby代码,用于签名S3网址并设置response-content-disposition。这可以强制Chrome浏览器下载PDF,而不是内嵌呈现。


0
投票

使用我的解决方案在所有S3对象上设置Content-Disposition:GitHub

© www.soinside.com 2019 - 2024. All rights reserved.