Nodejs AWS SDK S3生成预签名URL

时间:2022-06-24 10:46:08

I am using the NodeJS AWS SDK to generate a presigned S3 URL. The docs give an example of generating a presigned URL.

我正在使用NodeJS AWS SDK生成一个预先签名的S3 URL。文档给出了生成预签名URL的示例。

Here is my exact code (with sensitive info omitted):

这是我的确切代码(省略敏感信息):

const AWS = require('aws-sdk')

const s3 = new AWS.S3()
AWS.config.update({accessKeyId: 'id-omitted', secretAccessKey: 'key-omitted'})

// Tried with and without this. Since s3 is not region-specific, I don't
// think it should be necessary.
// AWS.config.update({region: 'us-west-2'})

const myBucket = 'bucket-name'
const myKey = 'file-name.pdf'
const signedUrlExpireSeconds = 60 * 5

const url = s3.getSignedUrl('getObject', {
    Bucket: myBucket,
    Key: myKey,
    Expires: signedUrlExpireSeconds
})

console.log(url)

The URL that generates looks like this:

生成的URL如下所示:

https://bucket-name.s3-us-west-2.amazonaws.com/file-name.pdf?AWSAccessKeyId=[access-key-omitted]&Expires=1470666057&Signature=[signature-omitted]

I am copying that URL into my browser and getting the following response:

我将该URL复制到浏览器中并得到以下响应:

<Error>
  <Code>NoSuchBucket</Code>
  <Message>The specified bucket does not exist</Message>
  <BucketName>[bucket-name-omitted]</BucketName>
  <RequestId>D1A358D276305A5C</RequestId>
  <HostId>
    bz2OxmZcEM2173kXEDbKIZrlX508qSv+CVydHz3w6FFPFwC0CtaCa/TqDQYDmHQdI1oMlc07wWk=
  </HostId>
</Error>

I know the bucket exists. When I navigate to this item via the AWS Web GUI and double click on it, it opens the object with URL and works just fine:

我知道水桶是存在的。当我通过AWS Web GUI导航到这个项目并双击它时,它就会打开带有URL的对象并正常工作:

https://s3-us-west-2.amazonaws.com/[bucket-name-omitted]/[file-name-omitted].pdf?X-Amz-Date=20160808T141832Z&X-Amz-Expires=300&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Signature=[signature-omitted]&X-Amz-Credential=ASIAJKXDBR5CW3XXF5VQ/20160808/us-west-2/s3/aws4_request&X-Amz-SignedHeaders=Host&x-amz-security-token=[really-long-key]

So I am led to believe that I must be doing something wrong with how I'm using the SDK.

所以我认为我在使用SDK时一定做错了什么。

1 个解决方案

#1


29  

Dustin,

达斯汀,

Your code is correct, double check following:

您的代码是正确的,请仔细检查如下:

  1. Your bucket access policy.

    你的桶访问政策。

  2. Your bucket permission via your API key.

    您的桶权限通过您的API密钥。

  3. Your API key and secret.

    您的API密钥和秘密。

  4. Your bucket name and key.

    你的桶名和钥匙。

#1


29  

Dustin,

达斯汀,

Your code is correct, double check following:

您的代码是正确的,请仔细检查如下:

  1. Your bucket access policy.

    你的桶访问政策。

  2. Your bucket permission via your API key.

    您的桶权限通过您的API密钥。

  3. Your API key and secret.

    您的API密钥和秘密。

  4. Your bucket name and key.

    你的桶名和钥匙。