AWS S3预签名URL错误

时间:2018-07-04 02:38:17

标签: node.js amazon-web-services amazon-s3

我正在构建节点应用程序,并且我已有一个具有上传图像的AWS帐户。

这是我的代码,我遵循了文档和之前在此处发布的其他一些问题:

const AWS = require('aws-sdk');

const s3 = new AWS.S3();
AWS.config.update({
  accessKeyId: 'string',
  secretAccessKey: 'string',
  region: 'string'
 });
const myBucket = 'string-bucket';
const myKey = 'file-name.png';
const signedUrlExpireSeconds = 60 * 5;

const params = { Bucket: 'string-bucket' };
s3.headBucket(params, function(err, data) {
  if (err) console.log('there is an error!!!!', err, err.stack);
  else     console.log('this is what i got', data);
});

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

console.log(url);

首先,它记录了URL:https://s3.amazonaws.com/,就这样,没有附加任何字符串,对于存储桶来说,它是CredentialsError: Missing credentials in config

我看不到我所缺少的内容,它们的键是正确的(我已复制并粘贴了它们)

1 个答案:

答案 0 :(得分:0)

问题在于,您在初始化S3 SDK后 后正在更新AWS配置。只需将以下代码段放在const s3 = new AWS.S3();上方:

AWS.config.update({
  accessKeyId: 'string',
  secretAccessKey: 'string',
  region: 'string'
});

最后,您应该具有以下内容:

const AWS = require('aws-sdk');
AWS.config.update({
  accessKeyId: 'string',
  secretAccessKey: 'string',
  region: 'string'
});
const s3 = new AWS.S3();