node.js - 在 Node Js 中不使用访问密钥和密钥将文件上传到 S3 存储桶
问题描述
虽然我可以使用访问密钥和密钥将文件上传到 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);
解决方案
在 SDK for JavaScript 中使用来自实例角色的凭据在以下内容中进行了说明:
SDK 会自动为您的应用程序选择 IAM 凭证,无需手动提供凭证。
推荐阅读
- python - 与python平台的硬件接口中的串行通信问题
- c# - 如果 this.somefield 引用了由“using”语句初始化的对象say X,那么垃圾收集器将来会使X为空吗?
- android - maxjavastacktracedepth=-1 创建 JVM 失败
- c# - .NET Core 中的共享设置?
- angularjs - 如何在使用 angular-route 路由期间将数据发送到路由视图
- facebook - {shadow-ig-media-id} 是什么?
- php - 在 WooCommerce 中为特定产品类别的购物车项目自动添加产品
- cakephp - 从数据库创建下拉菜单
- php - 如何在 OpenCart 中做像 site.com/brands/BrandName/productName 这样的嵌套制造商?
- javascript - 如何在 json 中正确格式化 WP_REST_Response 并获取数组而不是对象?