简体   繁体   中英

aws-sdk get file information

i am trying to get the file information from a file on my Amazon S3 server using the aws-sdk node module.

What i want to get out is the file name, file type and size.

I have attempted the following methods without luck:

s3.headObject(params, function (err, data) {
    if (err) {
        console.log(err, err.stack)
    }
    else {
        d.resolve(data);
    }
});

And

s3.getObject(params, function (err, data) {
    if (err) {
        console.log(err, err.stack)
    }
    else {
        d.resolve(data);
    }
});

Looking through their documentation i cant seem to find any other method that will give me the information i need.

So my question to you is how do i get the above information?

Here is the code to get the file name, size and content-type of all the objects present in a bucket.

  • Change the bucket name

  • Load your access keys from config.json accordingly

Code:-

var AWS = require('aws-sdk');
    // Load credentials and set region from JSON file
    AWS.config.loadFromPath('./config.json');

// Create S3 service object
s3 = new AWS.S3({ apiVersion: '2006-03-01' });

var bucketName = 'yourBucketName';

var params = {
    Bucket: bucketName
};

var headParams = {
    Bucket: bucketName
};

listAllKeys();
function listAllKeys() {
    s3.listObjectsV2(params, function (err, data) {
        if (err) {
            console.log(err, err.stack); // an error occurred
        } else {
            var contents = data.Contents;
            contents.forEach(function (content) {
                //console.log(JSON.stringify(content));                
                headParams["Key"] = content.Key;
                s3.headObject(headParams, function (err, headObjectData) {
                    if (err) {
                        console.log(err, err.stack);
                    } else {
                        console.log("1. File name :" + content.Key + ";" + "   2.  File size :" + content.Size +  ";" + "  3. Content-Type :" + headObjectData.ContentType);
                    }
                });
            });

            if (data.IsTruncated) {
                params.ContinuationToken = data.NextContinuationToken;
                console.log("get further list...");
                listAllKeys();
            }

        }
    });
}

Sample output:-

1. File name :index.html;   2.  File size :48;  3. Content-Type :text/html

s3.headObject works fine. You can find sample code below

let primaryBucket = primarys3bucketname;
var headParams = {
    Bucket: primaryBucket,
};
let size = '';
headParams["Key"] = "/sample/path/to/filename.pdf";
s3.headObject(headParams).promise().then((headObjectData) => {
    size = this.bytesToSize(headObjectData.ContentLength);
});


function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes == 0) return '0 Byte';
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM