Google doc viewer doesn't work with Amazon s3 signed urls

后端 未结 2 427
星月不相逢
星月不相逢 2021-01-15 05:04

I try to display a .doc file stored on a S3 bucket inside an iframe thanks to google doc viewer api.

I already did some research and found this, which i tried to app

相关标签:
2条回答
  • 2021-01-15 05:27

    Check the solution specifically for Amazon s3 signed urls. Basically its for viewing private documentation using Google Docs Viewer

    Google Docs Viewer with Amazon S3 Private Documents and Presigned URLs

    0 讨论(0)
  • 2021-01-15 05:43

    The Amazon S3 Presigned URL contains query string parameters, so it contains "?" and "&" which confuses the outer URL.

    So you have to Encode the S3 Presigned URL before passing it to the Google Doc Viewer.

    Like this:

    var encodedUrl = encodeURIComponent(presigned_url);
    var iFrameUrl = 'https://docs.google.com/gview?url=' + encodedUrl;
    

    OR (using 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));
     }
    
    0 讨论(0)
提交回复
热议问题