gpt4 book ai didi

node.js - 在Node Js中不使用访问 key 和 secret key 将文件上传到S3 Bucket

转载 作者:行者123 更新时间:2023-12-05 06:00:09 28 4
gpt4 key购买 nike

虽然我可以使用访问 key 和 key 将文件上传到 s3 存储桶。和我试图在不使用访问 key 和 secret key 的情况下将文件上传到 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);

最佳答案

在 SDK for JavaScript 中使用来自实例角色的凭证在以下内容中进行了解释:

the SDK automatically selects the IAM credentials for your application, eliminating the need to manually provide credentials.

关于node.js - 在Node Js中不使用访问 key 和 secret key 将文件上传到S3 Bucket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67831792/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com