1

Can anyone help me to generate preview url for the file in s3 bucket? These are the things i tried

digest = OpenSSL::Digest::Digest.new('sha1')
expire_date = Time.now.utc.to_i + (2*24*60*60)
S3_BUCKET = "test-local"
S3_SECRET_ACCESS_KEY =  BW::Config.aws_access_key
path = "users/2/debts/7/sample.pdf"
can_string = "GET\n\n\n#{expire_date}\n/#{S3_BUCKET}/#{path}"
hmac = OpenSSL::HMAC.digest(digest, S3_SECRET_ACCESS_KEY, can_string)
signature = URI.escape(Base64.encode64(hmac).strip)
"https://#{S3_BUCKET}.s3.amazonaws.com/#{path}?AWSAccessKeyId=#{S3_SECRET_ACCESS_KEY}&Expires=#{expire_date}&Signature=#{signature}"

With this code i am getting error like

  <Error>
  <Code>SignatureDoesNotMatch</Code>
  <Message>
     The request signature we calculated does not match the signature you provided. Check your key and signing method.
 </Message>
 <AWSAccessKeyId>AKIAIWURLFHOWDXB53YQ</AWSAccessKeyId>
 <StringToSign>
 GET 1559811251 /debtcleanse-local/users/2/debts/7/sample.pdf
 </StringToSign>
<SignatureProvided>u04282G45mP5qZ0Xze4QEWVtsUk=</SignatureProvided>
<StringToSignBytes>47 45 54 0a 0a 0a 31 35 35 39 38 31 31 32 35 31 0a 2f 64 65 62 74 63 6c 65 61 6e 73 65 2d 6c 6f 63 61 6c 2f 75 73 65 72 73 2f 32 2f 64 65 62 74 73 2f 37 2f 73 61 6d 70 6c 65 2e 70 64 66
 </StringToSignBytes>
 <RequestId>FCE6C8D7705167E9</RequestId>
<HostId>voXKpRtvMXy4jZ5tOJnm/K8WelEclBEix7NdCulBFEKXJczF5dSqscqkTBoqz3GAHBbdZZoMypQ=
</HostId>
</Error>
meshkati
  • 1,720
  • 2
  • 16
  • 29
Thirupathi
  • 11
  • 6
  • The error is `Access Denied`. This indicates that either the object does not exist, or it is not publicly accessible. What makes you think that it should be publicly accessible via that URL? Is there a Bucket Policy on the bucket? Does it work if you put the URL in your web browser? – John Rotenstein Jun 04 '19 at 07:32
  • Hi Thank for you time now i am getting error like The request signature we calculated does not match the signature you provided. Check your key and signing method. can you plese tell me how to encode hamc using Base64 i am new bee i dont know anything about AWS. – Thirupathi Jun 04 '19 at 09:14
  • What do you mean by "generate preview url"? Are you wanting to make a Pre-Signed URL? I would recommend using the [AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby). It has an in-built [Class: Aws::S3::Presigner](https://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Presigner.html) that can do it in one command. – John Rotenstein Jun 05 '19 at 02:54
  • I uploaded pdf file to s3 bucket. How can i preview them in browser with out downloading? – Thirupathi Jun 05 '19 at 04:08
  • signer = Aws::S3::Presigner.new this line throwing error like undefined method `match' for nil:NilClass – Thirupathi Jun 05 '19 at 04:10
  • Are you wanting to access the file via a URL, or in the S3 management console? If you access it via a URL, are you okay with the entire world being able to access the object ("public"), or do you want to keep the object private but allow certain people to access it? – John Rotenstein Jun 05 '19 at 04:36
  • Yes i wanted to access the file via url. am ok with public access please suggest me in this i am new bee to aws. – Thirupathi Jun 05 '19 at 04:45
  • And suggest me to keep the object private but allow certain people to access it? in this also – Thirupathi Jun 05 '19 at 04:47
  • See: [How to protect some files/objects in public bucket?](https://stackoverflow.com/a/56333367/174777) – John Rotenstein Jun 05 '19 at 04:50
  • I make the policy as public but when i hit https://debtcleans-local.s3.amazonaws.com/users/2/debts/7/nanipkl.pdf this url is getting download instead of preview – Thirupathi Jun 05 '19 at 09:10
  • Ah! So by "preview" you mean "show it in the browser"! It has to do with the `Content-Type` set on the object. See [Uploading PDF to Amazon S3 and display in-browser](https://stackoverflow.com/a/30025072/174777) and [PDFs in Amazon S3 don't open in Chrome for view](https://stackoverflow.com/a/39832482/174777) – John Rotenstein Jun 06 '19 at 00:32

0 Answers0