我已正确设置具有 S3 源的 Cloudfront 签名 URL,并使用 response-content-disposition 查询字符串参数来指定文件下载名称。我使用 .NET AWS SDK AmazonCloudFrontUrlSigner.GetCannedSignedURL 方法生成的签名 URL 在内容处置文件名不包含空格时正常工作。但是,如果文件名包含空格,我会被拒绝访问。因此,类似于下面的代码将生成一个拒绝访问的 URL。
var contentDisposition = HttpUtility.UrlEncode("attachment;filename=My File.txt");
var key = "example.txt?response-content-disposition="+contentDisposition;
return AmazonCloudFrontUrlSigner.GetCannedSignedURL(
AmazonCloudFrontUrlSigner.Protocol.https,
"myBucket",
cloudFrontPrivateKey,
key, cloudFrontAccessKeyId, expirationDateTime);
这显然与 URL 编码有关。
我已经阅读了文档中有关 Serving Private Content through CloudFront 的所有信息.我读了code of the AmazonCloudFrontUrlSigner class .我还尝试了一些 UrlEncode 的组合,比如不编码、只编码文件名部分甚至不编码但在生成签名 URL 后替换为编码版本。所有这些要么拒绝访问,要么给出签名与 url 不匹配的错误。
请您参考如下方法:
HttpUtility.UrlEncode 方法将空格编码为 +,这符合标准。但是,出于某种我不明白的原因,这会导致签名 URL 和内容处置出现问题。另一种空格编码为 %20 可以正常工作。所以在编码之后,将+替换为%20。工作版本是:
var contentDisposition = HttpUtility.UrlEncode("attachment;filename=My File.txt");
contentDisposition = contentDisposition.Replace("+", "%20");
var key = "example.txt?response-content-disposition="+contentDisposition;
return AmazonCloudFrontUrlSigner.GetCannedSignedURL(
AmazonCloudFrontUrlSigner.Protocol.https,
"myBucket",
cloudFrontPrivateKey,
key, cloudFrontAccessKeyId, expirationDateTime);
