如何为存储在s3存储桶中的文件生成预览URL?

时间:2019-06-04 04:30:25

标签: ruby amazon-web-services amazon-s3 aws-sdk

有人可以帮助我为s3存储桶中的文件生成预览URL吗? 这些是我尝试过的事情

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}"

使用此代码,我会收到类似

的错误
  <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>

0 个答案:

没有答案