简体   繁体   中英

File uploading in Amazon S3 with Node.js

I am using aws-sdk to upload files on Amazon S3. It is working fine and uploading files, but my problem is; it changed file name after uploaded to the server. For example, if I upload sample.jpg , and it renamed to something like b4c743c8a2332525.jpg . Here is my code.

AWS.config.update({ 
    accessKeyId: key, 
    secretAccessKey: secret
});

var fileStream = fs.createReadStream(path);

fileStream.on('error', function (err) {
    if (err) { throw err; }
});

fileStream.on('open', function () {
    var s3 = new AWS.S3();
    s3.putObject({
        Bucket: bucket,
        Key: directory + file,
        Body: fileStream
    }, function (err) {
        if (err)
            res.send(err);
        fs.unlinkSync(path);
    });
});

Is it normal to change file name after uploaded files to S3 server, or is there any options to upload the same file name? Thank you.

Neither S3 nor the AWS SDK pick arbitrary file names for things you upload. The names are set by your own code.

Check the value of directory + file when you set it as the S3 object key. You may be uploading 'sample.jpg' from your browser (so the file is called sample.jpg locally on your disk), but the temporary file name that node.js uses to identify the file on it's disk may be using a hash like b4c743c8a2332525.jpg .

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