Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Get version ID of uploaded S3 file right after upload?

I'm dealing with the inevitable issue of users being able to overwrite files in my bucket via a JavaScript S3 upload.

After uploading a file, assuming I have versioning enabled, is there a way to get the original version ID so I can store it in my database for retrieval later when displaying that file?

This is my current upload code:

s3.upload(params).on('httpUploadProgress', function(evt) {
                var uploaded = Math.round(evt.loaded / evt.total * 100);
                console.log(`File uploaded: ${uploaded}%`);
            }).send(function(err, data) {
                if(err){
                    alert(err);
                } else {
                    alert('File is uploaded successfully!');
                    console.log(data);
                }
            });

I do not see any version ID in the data response since it seems to be in the response headers. Is there a way to extract it from there?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're right; your code doesn't have access to the previous/original versionId.

I don't know your exact requirements, but I see two ways how to do this:

  1. Check the file metadata before upload with s3.headObject which return data.VersionId. If the file doesn't exist, it doesn't matter; otherwise, you can save the original version ID in DB.

  2. Don't save the versions in the DB at all, and fetch them with listObjectVersions when the file is displayed from S3.

about 4 years ago · Juan Pablo Isaza Report

0

I was able to retrieve the original version ID immediately after upload using the following code (it uploads the image and then grabs the version right after using listObjectVersions):

s3.upload(params).on('httpUploadProgress', function(evt) {
                var uploaded = Math.round(evt.loaded / evt.total * 100);
                console.log(`File uploaded: ${uploaded}%`);
            }).send(function(err, data) {
                if(err){
                    alert(err);
                } else {
                    alert('File is uploaded successfully!');
                    console.log(data);
                    var params = {
                      Bucket: "mybucket", 
                      Prefix: "images/path-to-image.jpg"
                     };
                     s3.listObjectVersions(params, function(err, data) {
                       var versionId = data["Versions"][0]["VersionId"];
                       alert(versionId);
                   });
                }
            });

Be sure to have list object versions enabled in your bucket policy:

{
        "Sid": "Statement2",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:ListBucketVersions",
        "Resource": "arn:aws:s3:::mybucket"
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!