感谢 google doc viewer api,我尝试在 iframe 中显示存储在 S3 存储桶中的 .doc 文件。
我已经做了一些研究并找到了 this ,我试着在这里申请:
var encodedUrl = encodeURIComponent("http://myAPI.com/1d293950-67b2-11e7-8530-318c83fb9802/example.docx?X-Amz-Algorithm=AWS4-HMAC-SHA256%26X-Amz-Credential=GNRO0BLDYAJP1FU7ALIS%2F20170717%2Fus-east-1%2Fs3%2Faws4_request%26X-Amz-Date=20170717T145429Z%26X-Amz-Expires=600%26X-Amz-SignedHeaders=host%26X-Amz-Signature=ee556c5c92427bb38265507218a701660936bc2774ecc919648bb2d780a2619f");
$scope.selectedSource = $sce.trustAsResourceUrl(
"http://docs.google.com/gview?url=" + encodedUrl + "&embedded=true"
);
iframe 看起来像:
<iframe ng-src="{{selectedSource}}" frameborder="no" style="width:100%;height:100%"></iframe>
但我仍然从 iframe 中的查看器收到“无可用预览”,知道为什么吗?
我指定签名的 url(未编码)在我将其粘贴到浏览器时有效,因此我拥有所需的所有权利。
最佳答案
Amazon S3 预签名 URL 包含查询字符串参数,因此它包含“?”和混淆外部 URL 的“&”。
因此,您必须先对 S3 预签名 URL 进行编码,然后再将其传递给 Google 文档查看器。
像这样:
var encodedUrl = encodeURIComponent(presigned_url);
var iFrameUrl = 'https://docs.google.com/gview?url=' + encodedUrl;
或(使用 S3 .NET Core API)
using (AmazonS3Client S3Client = new AmazonS3Client(accKey, secKey, new AmazonS3Config() { ServiceURL = serviceUrl }))
{
GetPreSignedUrlRequest getPreSignedUrl = new GetPreSignedUrlRequest
{
BucketName = s3BucketName,
Key = fileName,
Expires = DateTime.Now.AddMinutes(30)
};
encoded_SignedUrl = WebUtility.UrlEncode(S3Client.GetPreSignedURL(getPreSignedUrl));
}
关于javascript - Google 文档查看器不适用于 Amazon s3 签名网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45147600/