javascript - Google 文档查看器不适用于 Amazon s3 签名网址

标签 javascript angularjs iframe amazon-s3 google-api

感谢 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/

相关文章:

javascript - 我无法打印 Protractor 中 element.all 语句中的项目

javascript - 使用日期差输出字符串

javascript - 单击菜单列表中的任何选项时,执行 url 的目标位于同一 html 文件中的另一个 div 标签内

javascript - 如何在不添加随机数的情况下阻止css缓存?

asp.net-mvc - 是否可以删除 .Net Web Api 应用程序中的所有 .Net MVC 组件,以便我可以使用 AngularJS 作为前端?

rest - 当集合服务返回字典而不是数组时如何使用 Restangular?

php - 使用iframe显示外部内容有哪些安全风险和可能的防范措施?

javascript - 有没有一种方法可以唯一标识内容脚本在我的 Chrome 扩展程序中运行的 iframe?

javascript - Jquery Ajax 未在成功正文中返回任何响应

javascript - 如何将 iFrame 的内容插入数据库?