Home > Back-end >  How to get S3 object's different version contents?
How to get S3 object's different version contents?

Time:09-03

I have the following code in python boto3 for getting versions of a S3 object and the assoicated content/body of that object.

versions = s3.Bucket(bucket).object_versions.filter(Prefix=key)
for version in versions:
    obj = version.get()
    data = obj.get('Body').read().decode('utf-8')

What would be the equivalent for this or otherwise different way to get object version contents in Nodejs using AWS SDK for JavaScript v3. I am able to get the versions associated with an object but not the contents.

CodePudding user response:

You can get all versions of an object by the s3.listObjectVersions method.

var params = {
  Bucket: "examplebucket", 
  Prefix: "HappyFace.jpg"
 };
 s3.listObjectVersions(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);  
});

After getting the version you can pass it on param like below

    var params = {
       Bucket: 'STRING_VALUE', /* required */
       Key: 'STRING_VALUE', /* required */
       VersionId: 'STRING_VALUE'
     };
     const result = await s3.getObject(getParams).promise();

From above code you will get content of specific version id object which you want to get.

  • Related