Browser AWS S3:使用';响应内容配置';

Browser AWS S3:使用';响应内容配置';,browser,amazon-web-services,amazon-s3,content-type,content-disposition,Browser,Amazon Web Services,Amazon S3,Content Type,Content Disposition,下面几行代码生成了签名URL,浏览器被重定向到该URL以从S3下载文件 我面临着一个众所周知的问题:当内容类型设置为八位字节流时,Chrome无法从S3下载pdf文件 解决方案是强制chrome下载文件,而不是试图读取/打开它 我尝试添加“响应内容配置”来签名和URL,但没有成功 如何在生成url时使用“响应内容处置”标题 String codedFilename= EncodingUtil.urlEncode(bucketPath,'UTF-8'); String stri

下面几行代码生成了签名URL,浏览器被重定向到该URL以从S3下载文件

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

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

我尝试添加“响应内容配置”来签名和URL,但没有成功

如何在生成url时使用“响应内容处置”标题

    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语法

    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;
资料来源:

如果文档的内容类型为“应用程序/八位字节流”(本例中为pdf格式),Chrome就会出现问题。但是,如果内容类型为“二进制/八位字节流”,则chrome是正常的

谢天谢地,如果上传时未设置任何设置,S3将下载的内容类型默认为“二进制/八位字节流”

我的忠告是:-
如果在上传时需要将“any”类型的文件上传到S3而不知道内容类型,只需不将其设置或将其设置为“binary/Octet stream”。这样Chrome会显示警告,但文件会被下载。

下面是一些Ruby代码,用于签署S3URL并设置响应内容配置。这可以迫使Chrome下载PDF,而不是内联呈现


使用我的解决方案在所有S3对象上设置内容配置:

在文件本身上设置自定义标题。这些头文件将在下载文件时发送到客户端。我相信您只能在首次上载文件时执行此操作。@datasage,S3文档没有为PUT请求指定“响应内容类型”标记。因此,我认为我们无法在上传时设置该标题。(虽然我可能在这方面出错)请。在你的答案中包括要点;留下链接以获取更多信息或作为参考。这个答案没有以任何方式解释它为什么以及如何解决OP的具体问题,而不需要点击链接。而且,如果链接断开,那么整个答案完全没有用。