Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing S3 bucket objects with specific storage class

It's very time consuming to get objects from Glacier so I decided to use S3 IA storage class instead. I need to list all the objects in my bucket that have Glacier storage class (I configured it via LifeCycle policy) and to change it to S3 IA.

Is there any script or a tool for that?

like image 389
Anton Zorin Avatar asked Oct 25 '16 13:10

Anton Zorin


2 Answers

You can do that using list-objects

list-objects will return the StorageClass, in your case you want to filter for values where it is GLACIER

aws s3api list-objects --bucket %bucket_name% --query 'Contents[?StorageClass==`GLACIER`]'

What you want then is to get only the list of Key that matches

aws s3api list-objects --bucket %bucket_name% --query 'Contents[?StorageClass==`GLACIER`][Key]' --output text

Then you will need to copy the object with changing the storage class of the Key

aws s3api list-objects --bucket %bucket_name% --query 'Contents[?StorageClass==`GLACIER`][Key]' --output text
| xargs -I {} aws s3 cp s3://bucket_name/{} s3://bucket_name/{} --storage-class STANDARD_IA
like image 75
Frederic Henri Avatar answered Nov 17 '22 17:11

Frederic Henri


and ... if you need to run this from Powershell in windows, I had to do this:

aws s3api list-objects --bucket Your_Bucket --query 'Contents[?StorageClass==`STANDARD`][Key]' --output text | foreach { aws s3 cp s3://Your_Bucket/$_ s3://Your_Bucket/$_ --storage-class REDUCED_REDUNDANCY }
like image 40
Juan Machado Avatar answered Nov 17 '22 18:11

Juan Machado