Home > database >  How to restore objects from amazon glacier to standard tier permanently in a s3 bucket using python?
How to restore objects from amazon glacier to standard tier permanently in a s3 bucket using python?

Time:12-06

I am trying to restore some objects in a s3 bucket from glacier to standard tier permanently below is my code

def restoreObject(latest_object):
    #s3_resource=boto3.resource('s3')
    s3 = boto3.client('s3')
    my_bucket = s3.Bucket('bucket_name')
    for bucket_object in my_bucket.objects.all():
        object_key=bucket_object.key  
        if(bucket_object.storage_class == "Glacier_Deep_Archive"):
            if object_key in latest_object:
                my_bucket.restore_object(Bucket="bucket_name",Key=object_key,RestoreRequest={'Days': 30,'Tier': 'Standard'})

But this restores the bundle for a particular time only (30 days in my case) Is there a way to restore bundles permanently from Glacier_Deep_Archive to standard tier?

CodePudding user response:

To permanently change the Storage Class of an object in Amazon S3, either:

However, Lifecycle policies do not seem to support going from Glacier to Standard tiers.

Therefore, you would need to copy the object to itself to change the storage class:

  copy_source = {'Bucket': 'bucket_name', 'Key': object_key}
  my_bucket.copy(copy_source, object_key, ExtraArgs = {'StorageClass': 'STANDARD','MetadataDirective': 'COPY'})

Here's a nice example: aws s3 bucket change storage class by object size · GitHub

  • Related