1

我正在使用aws-sdk将文件上传到s3。我正在配置我的凭据

aws.config.update({
  accessKeyId: aws.config.credentials.accessKeyId,
  secretAccessKey: aws.config.credentials.secretAccessKey,
  region: 'us-east-1'
});

然后上传multer-s3

const s3 = new aws.S3();

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'my-bucket-v1',
    acl: 'public-read',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    key: function (req, file, cb) {
      const today = new Date();
      cb(null, file.originalname)
      console.log("file\n", file);
    }
  })
}).array('upl', 1);

router.post('/api/upload', (req, res, next) => {
  upload(req, res, err => {
    if (err) return console.log("err\n", err);
    res.status(201).send();
  })
});

我不断收到的错误是我的访问密钥 ID:"The AWS Access Key Id you provided does not exist in our records."

我在我的 aws 帐户中创建了多个新的访问密钥,但没有任何效果。我正在使用 root 用户访问密钥(我尝试了 IAM 用户,但它仍然无法正常工作)。

我还在我的节点服务器 ( console.log(s3)) 中记录了我的 aws 凭证,它与我的 aws 安全凭证中的内容相匹配。

如何正确配置我的 aws 凭据以上传到 s3?

4

3 回答 3

2

终于想通了。在对象s3中使用它之前,我必须在初始化的位置设置访问键。multer

const s3 = new aws.S3({
  accessKeyId: ACCESS_KEY_ID // Set access key here
  secretAccessKey: SECRET_ACCESS_KEY,
});

const upload = multer({
  storage: multerS3({
    s3: s3, // Use s3 instance here
    bucket: 'my-match',
    acl: 'public-read',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    key: function (req, file, cb) {
      const today = new Date();
      cb(null, file.originalname)
      console.log("file\n", file);
    }
  })
}).array('upl', 1);

我仍然不明白为什么以前的实现不再起作用,但至少这种方式有效。

于 2020-05-20T13:55:49.660 回答
0

可能仅适用于 aws 教育帐户的解决方案

添加访问令牌使其工作

注意在代码中更新您的凭据和存储桶变量

import fs from 'fs';
import AWS from 'aws-sdk';


const aws_access_key_id="ASICDENCJVVNVHV"
const aws_secret_access_key="xrLcJTh5xHsNRHFRFMFRFTRygO7evgyTZCuJ7KDa7HNgMg"

const s3 = new AWS.S3({
    accessKeyId: aws_access_key_id,
    secretAccessKey: aws_secret_access_key,
    sessionToken:"IQoJb3JpZ2luX2VjEKv//////////wEaCXVzLXdlc3QtMiJHMEUCIDRIZOebUzz0+HfsimpnMGQp27oQWoByJxZhXFR34mgPAiEAwkUHMSf0iac3p/8VaEnjrfruuUkt8mf2m4cWfc23lj3QqrwIIQxABGgwzMzMwODQ3NzM0NDciDMpDh4tfMrUzU9EikyqMAoACnGKcTtOWbVxYsnaRIY0jve4YvyEvFJlk+RUl2/Tp1wnHvQF+oyQlXEQ1NIvQCedEV0ThLm6fePZrr+23qMEg7DHyZZ2q+UYubMIFwj4jkL4OF4vqluXPJz/vMnqTe+n+vcgioY2/quQWpBNVMBWG9egIQaevXzd7QStzVznEgIhZ3OMLEABWMZHE3ipY5vhwvWf6thLcwTHeb65l3wAUOmG89102WerI/a4nJ5Ivye4w2EVTA4laeSSRFmq0u20FwIx4xUSrDySjurBqJD0VIhQh7XcY4bsBc8XavubKq0JzmOC993N5Hr2JLb2N+KiVIW+r6qDmkPCNso4ndJhtC/YTX5v07zMbdgcwuYeJhQY6nQEqeEXY9FmVM2HXFqLmIwYilafVQSFO5gP9VmS8BhylIUmyzN/Uplg8V/f06b6Xhnm1MY28UGe27ALE/dO8U6muwjIxGBB33Albtr1BtBDk7yWplpayE9NBlAJigIxAQy2OqNUfrbJtzD4aFBUlC0r/enzLO4PrPq/rOkvfR4di87kpcfJX/zPLkNHTXEdzz/2boCd69/uMIwi611yP"
});

// : MulterFile[])
const uploadToS3 = async (ls) => {
    fs.readFile('/home/hari/learn/shakti/shaktiserver/src/image-upload/ram.png', (err, data) => {
        if (err) throw err;
        const Body = JSON.stringify(data, null, 2);
        s3.upload({
            Bucket: AWS_S3_BUCKET_NAME, // pass your bucket name
            Key: 'ram.png', // file will be saved as testBucket/contacts.csv
            ACL: 'public-read',
            Body: Body
        }, (e, data) => {
            if (e) throw e
            console.log({ data })
            console.log(`File uploaded successfully at ${data.Location}`)
        })

    })
}
uploadToS3([])
于 2021-05-17T10:50:24.000 回答
0

似乎 AWS 无法找到您提供给 AWS 配置的accessKey 。

验证您的配置。

aws.config.update({
  accessKeyId: aws.config.credentials.accessKeyId,
  secretAccessKey: aws.config.credentials.secretAccessKey,
  region: 'us-east-1'
});

在此处阅读有关 accessKeyId 和 secretAccessKey 的更多信息:

https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/

于 2020-05-20T11:05:49.820 回答