Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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
about 4 years ago · Santiago Trujillo Denunciar

0

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];
};
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda