【问题标题】:Google doc viewer doesn't work with Amazon s3 signed urlsGoogle 文档查看器不适用于 Amazon s3 签名的网址
【发布时间】:2017-12-22 04:32:48
【问题描述】:

借助 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(未编码)在我将其粘贴到浏览器时有效,因此我拥有所需的所有权限。

【问题讨论】:

  • 这适用于我使用来自 S3 的预签名 URL,打开一个新的浏览器选项卡并转到一个稍微不同的 URL:https://docs.google.com/viewer?url= + 冗余 url 编码(从技术上讲,这就是你的重新做,似乎是正确的)预签名的 S3 URL(签名版本 4)。备用 Google URL 被发现 here。您可能希望在尝试嵌入之前在其自己的窗口中对其进行测试,以消除变量。
  • 链接已失效且无法正常工作

标签: javascript angularjs iframe amazon-s3 google-api


【解决方案1】:

Amazon S3 Presigned URL 包含查询字符串参数,因此它包含“?”和混淆外部 URL 的“&”。

因此,您必须先对 S3 预签名 URL 进行编码,然后再将其传递给 Google Doc Viewer。

像这样:

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));
 }

【讨论】:

    【解决方案2】:

    检查专门针对 Amazon s3 签名 URL 的解决方案。基本上它用于使用 Google Docs Viewer 查看私人文档

    Google Docs Viewer with Amazon S3 Private Documents and Presigned URLs

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 1970-01-01
      • 2020-05-04
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      相关资源
      最近更新 更多