S3

Random snippets abooot S3

s3cmd

In order to create a bucket in the Object Storage service, you can use s3cmd with the following command.

# create bucket
$ s3cmd mb s3://BUCKET

# List all 'objects' in bucket
$ s3cmd ls

# add file to bucket
$ s3cmd put ~/test.json s3://BUCKET

# you can specify a lot more on upload 
$ s3cmd put --mime-type='application/json' --add-header='Cache-Control: max-age=3600' ~/test.json s3://BUCKET

# delete file 
$ s3cmd del s3://BUCKET/test.json

# delete bucket
$ s3cmd rb s3://BUCKET

Object Expiration with s3cmd

You can set an object expiration policy on a bucket, so that objects older than a particular age will be deleted automatically. The expiration policy can have a prefix, an effective date, and number of days to expire after.

$ s3cmd expire s3://BUCKET --expiry-days 2
Bucket 's3://BUCKET/': expiration configuration is set.

$ s3cmd getlifecycle s3://BUCKET
<?xml version="1.0" ?>
<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <Rule>
        <ID>ir0smpb610i0lthrl31jpxzegwximbcz3rrgb1he2yfxgudm</ID>
        <Prefix/>
        <Status>Enabled</Status>
        <Expiration>
            <Days>2</Days>
        </Expiration>
    </Rule>
</LifecycleConfiguration>

Additional s3cmd expire options include:

# Indicates when the expiration rule takes effect. (only for [expire] command)
 --expiry-date=EXPIRY_DATE 

# Indicates the number of days after object creation the expiration rule takes effect. (only for [expire] command)
  --expiry-days=EXPIRY_DAYS 

# Identifying one or more objects with the prefix to which the expiration rule applies. (only for [expire] command)
  --expiry-prefix=EXPIRY_PREFIX

and these commands can set/get/delete a lifecycle policy:

#  Upload a lifecycle policy for the bucket
$ s3cmd setlifecycle FILE s3://BUCKET

#  Get a lifecycle policy for the bucket
$ s3cmd getlifecycle s3://BUCKET

#  Remove a lifecycle policy for the bucket
$ s3cmd dellifecycle s3://BUCKET