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:
- Write code to
CopyObject
with the same Key (overwriting itself) while specifying the new Storage Class, or - Transition objects using Amazon S3 Lifecycle to let S3 do it automatically
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