【问题标题】:Upload Files to S3 Bucket without using access key and secret key in Node Js在 Node Js 中不使用访问密钥和密钥将文件上传到 S3 存储桶
【发布时间】: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


【解决方案1】:

SDK for JavaScript 中实例角色的凭据的使用解释如下:

SDK自动为您的应用程序选择 IAM 凭证,无需手动提供凭证。

【讨论】:

    【解决方案2】:

    这是我们无需使用任何访问密钥或密钥即可将文件上传到 s3 存储桶的一种方式。您可以在 AWS 中创建一个 congnito 池 id。

    所以我的问题是如何在不使用访问密钥和密钥的情况下将文件上传到 s3 存储桶...

    下面我已经为我的问题写了解决方案

    private async uploadFile(fileName: string) {
            CustomLogger.logger.info('Inside File Upload Method');
            
            AWS.config.region = 'ADD YOUR REGION';
            AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                IdentityPoolId: 'ADD YOUR COGNITO ID'
            });
    
            const BUCKET_NAME = 'ADD YOUR BUCKET NAME';
    
            const s3 = new AWS.S3({
                region: AWS.config.region,
                credentials: AWS.config.credentials
            });
    
            const fileContent = fs.readFileSync(fileName);
    
            const params = {
                Bucket: BUCKET_NAME,
                Key: LpServiceImpl.getZipFileName(fileName),
                Body: fileContent
            };
    
            s3.upload(params, (err: Error, data: any) => {
                if (err) {
                    CustomLogger.logger.info('File upload failed.');
                    throw err;
                }
    
               
    
                CustomLogger.logger.info('File uploaded successfully.' + data.Location);
    
            });
        };
    
        private static getZipFileName(filePath: string){
            const n = filePath.split("\\");
            CustomLogger.logger.info('Split Successfully');
            return n[n.length - 1];
        }
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
    • @Tyler2P 我已经用一个小解释编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 2014-07-10
    • 2022-01-12
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多