Lahiru Ferreira
Lahiru Ferreira

Reputation: 31

Upload Files to S3 Bucket without using access key and secret key in Node Js

Although I am able to upload file to s3 bucket by using access key and secret key. AND I am trying to upload a file to AWS s3 bucket without using access key and secret key. Please assist me. Here is my code,

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);

Upvotes: 3

Views: 7114

Answers (2)

Lahiru Ferreira
Lahiru Ferreira

Reputation: 31

This is one way we can upload files into s3 bucket without using any access key or secret key. You can create a congnito pool id in AWS.

So my problem was how can I upload a file to s3 bucket without using access key and secret key...

Below I have written the Solution for my problem

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];
    }

Upvotes: 0

Marcin
Marcin

Reputation: 238877

The use of credentails from instance role in SDK for JavaScript is explained in:

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

Upvotes: 1

Related Questions