Cloud Computing‎ > ‎AWS‎ > ‎S3‎ > ‎

boto3



#### Create AWS S3 Session and Get bucket object

```
import boto3
# session = boto3.session.Session(profile_name='aws-profile')
session = boto3.session.Session(region_name='', aws_access_key_id='', aws_secret_access_key='')
s3 = session.resource('s3')
bucket = s3.Bucket('com.vpc.user')

```

#### Read all bucket objects

```
>>> for key in bucket.objects.all():
...     print str(key.key)
...
1234
1235
```

#### Read bucket metadata

```
s3.meta.client.head_bucket(Bucket='com.vpc.user')
```


#### Read the bucket object

```
obj = s3.Object('com.vpc.user', '1234')
file_data = obj.get()['Body'].read()
print str(file_data)

```

Object TTL

It seems that S3 does not support per object expiration.

The Expires header is not meant to set the TTL of the object. S3 documentations says "Expires = The date and time at which the object is no longer cacheable" (http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html)

To get your files automatically deleted by S3 you need to create Lifecycle rules on your bucket.


S3 object expiration using boto library

http://stackoverflow.com/questions/14969273/s3-object-expiration-using-boto


Use boto3 library

http://boto3.readthedocs.io/en/latest/reference/services/s3.html#bucketlifecycle


Comments