【发布时间】:2021-07-12 12:23:53
【问题描述】:
虽然我可以使用访问密钥和密钥将文件上传到 s3 存储桶。 和 我正在尝试在不使用访问密钥和密钥的情况下将文件上传到 AWS s3 存储桶。请帮助我。 这是我的代码,
import {path} from "path";
const fs = require('fs');
const AWS = require('aws-sdk');
AWS.config.update({region: 'Region'});
// Enter copied or downloaded access ID and secret key here
const ID = 'id';
const SECRET = 'id';
// The name of the bucket that you have created
const BUCKET_NAME = 'name';
const s3 = new AWS.S3({
accessKeyId: ID,
secretAccessKey: SECRET
});
const FILE_PATH = 'filepath';
const uploadFile = (fileName) => {
// Read content from the file
const fileContent = fs.readFileSync(fileName);
// Setting up S3 upload parameters
const params = {
Bucket: BUCKET_NAME,
Key: path.basename(FILE_PATH), // File name you want to save as in S3
Body: fileContent
};
// Uploading files to the bucket
s3.upload(params, function(err, data) {
if (err) {
throw err;
}
console.log('File uploaded successfully. ${data.Location}');
});
};
uploadFile(FILE_PATH);
【问题讨论】:
-
如果不是来自您的访问密钥和密钥,AWS 凭据从哪里来?
-
你问的问题我不清楚,可能是因为我是一个新人。我需要的只是使用 nodejs,我应该能够将文件上传到 s3 存储桶! (没有访问密钥和密钥)
-
是否可以通过添加具有完全 s3 访问权限的 IAM 角色来做到这一点?如果可以,你能帮我吗?
-
需要更多细节。您的应用托管在哪里?拉姆达? EC2?本地?
-
我用过 EC2
标签: node.js amazon-web-services amazon-s3