David Faizulaev
David Faizulaev

Reputation: 5721

AWS SDK in node js - Get object from sub directory

I'm using the aws-sdk for Nodejs and I'm trying to get an object that's located in a sub-directory. But I don't get any files back.

The file path is: mybucket/subfoldername/file.txt

getObject = function getListObjects(bucketName, bucketPath, fileName) {
return new Promise((resolve, reject) => {
    let params = {Key: fileName, Bucket: bucketName+bucketPath};
    s3.getObject(params, function (error, data) {
        if (error){
            console.log(`Can't connect to S3. S3 Path = [${fileName}] to Bucket = [${params.Bucket}]`, error);
            return reject(error);
        } else {
            console.log(`Found files in S3. S3 Path =[${fileName}] to Bucket = [${params.Bucket}]`);
            return resolve(data.Contents);
        }
    });
});

};

The arguments I pass are:

bucketName: "mybucket"
bucketPath: "/subfoldername"
fileName: "file.txt"

Please advise.

Upvotes: 0

Views: 3038

Answers (1)

Miguel Calderón
Miguel Calderón

Reputation: 3091

The Keyargument should include the "path" string, too, as S3 bucket files are referenced by its complete path:

let params = { Key: `${bucketPath}/${fileName}`, Bucket: bucketName };

I've taken the liberty of using template quotes there (``). Also, notice how I've added a "/" separator, please check that it's not already included in bucketPath.

Upvotes: 4

Related Questions