node.js Nodejs AWS SDK S3 生成预​​签名 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38831829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:28:36  来源:igfitidea点击:

Nodejs AWS SDK S3 Generate Presigned URL

node.jsamazon-web-servicesamazon-s3aws-sdk-js

提问by Dustin

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 的方式上做错了什么。

采纳答案by Reza Mousavi

Dustin,

达斯汀,

Your code is correct, double check following:

您的代码是正确的,请仔细检查以下内容:

  1. Your bucket access policy.

  2. Your bucket permission via your API key.

  3. Your API key and secret.

  4. Your bucket name and key.

  1. 您的存储桶访问策略。

  2. 您通过 API 密钥获得的存储桶权限。

  3. 您的 API 密钥和秘密。

  4. 您的存储桶名称和密钥。

回答by Prateek G

I had a use case where using node.js ; I wanted to get object from s3 and download it to some temp location and then give it as attachment to third-party service! This is how i broke the code:

我有一个使用 node.js 的用例;我想从 s3 获取对象并将其下载到某个临时位置,然后将其作为第三方服务的附件!这就是我破解代码的方式:

  1. get signed url from s3
  2. make rest call to get object
  3. write that into local location
  1. 从 s3 获取签名的 url
  2. 进行休息调用以获取对象
  3. 将其写入本地位置

It may help anyone; if there is same use case; chekout below link; https://medium.com/@prateekgawarle183/fetch-file-from-aws-s3-using-pre-signed-url-and-store-it-into-local-system-879194bfdcf4

它可以帮助任何人;如果有相同的用例;结帐下面的链接https://medium.com/@prateekgavarle183/fetch-file-from-aws-s3-using-pre-signed-url-and-store-it-into-local-system-879194bfdcf4

回答by Ankit Kumar Rajpoot

Try this function with promise.

用承诺试试这个功能。

const AWS = require("aws-sdk");
const s3 = new AWS.S3({
  accessKeyId: 'AK--------------6U',
  secretAccessKey: 'kz---------------------------oGp',
  Bucket: 'bucket-name'
});

const getSingedUrl = async () => {    
  const params = {
    Bucket: 'bucket_name',
    Key: 'file-name.pdf',
    Expires: 60 * 5
  };

  try {
    const url = await new Promise((resolve, reject) => {
      s3.getSignedUrl('getObject', params, (err, url) => {
        err ? reject(err) : resolve(url);
      });
    });
    console.log(url)
  } catch (err) {
    if (err) {
      console.log(err)
    }
  }
}


getSingedUrl()