Boto3: Upload a file to s3

Create an IAM role for s3 and attach the role to the EC2 instance.

import boto3
import sys
import os
from datetime import datetime
import traceback

bucket_name = 'some-bucket'
file_to_upload = "/path/to/file"
file_name = 'file'

try :
  s3 = boto3.resource('s3')
  bucket = s3.Bucket(bucket_name)
  bucket.upload_file(file_to_upload, file_name)
  print( "{} copied to {}".format( file_to_upload, bucket ) )
except Exception as err:
  traceback.print_exc()
finally:
  # time is always in GMT 
  print (datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

AWS boto3 + S3 + Lambda auto add cache control

For AWS Lambda with python, we need to use boto3. So a different code is required.

from __future__ import print_function

import json
import urllib
import boto3
import email
import time
from datetime import datetime, timedelta

s3 = boto3.resource('s3')
one_month = 3600*24*30
one_year  = 3600*24*365

def lambda_handler(event, context):
    # Get the object from the event and show its content type
    bucket_name = event['Records'][0]['s3']['bucket']['name']
    key_name = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
    
    print(key_name)
    cache_time = one_month # or one_year
    #get the object
    object = s3.Object(bucket_name, key_name)
    
    #get object details 
    response = object.get() 
    # check if 'CacheControl' exists
    if 'CacheControl' in response:
        print("exists")
        return
    modified = response
    modified['Body'] = response['Body'].read()
    modified.pop("VersionId", None)
    modified.pop("AcceptRanges", None)
    modified.pop("ETag", None)
    modified.pop("LastModified", None)
    modified.pop("ResponseMetadata", None)
    modified['Metadata'] = response['Metadata']
    modified['CacheControl'] = ('max-age=%d, public' % (cache_time))
    # put the same object with a new CacheControl
    response = object.put(**modified)
    return

AWS S3 add metadata cache control to existing keys

Code for adding cache-control to the all S3 key/object for a given bucket.

    import json
    import urllib
    from boto.s3.connection import S3Connection
    one_month = 3600*24*30
    one_year  = 3600*24*365
    cckey = 'cache-control'

    s3_connection = S3Connection()
    bucket_name = ''
    bucket = s3_connection.get_bucket(bucket_name, validate=False)

    for key in bucket:
        key_name = key.key
        if  key.size == 0: # continue on directories
            continue
        key = bucket.get_key(key_name)

        cache_time = one_month
        key.set_metadata(name=cckey, value = ('max-age=%d, public' % (cache_time)))
        key.set_metadata(name='content-type', value = key.content_type) # preserve the content-type
        
        key2 = key.copy(key.bucket.name, key.name, key.metadata, preserve_acl=True)
        #print(key2.__dict__)
        continue

Code for adding cache-control to a particular key for a given bucket.

    import json
    import urllib
    from boto.s3.connection import S3Connection
    one_month = 3600*24*30
    one_year  = 3600*24*365
    cckey = 'cache-control'

    s3_connection = S3Connection()
    bucket_name = ''
    key_name = ''
    bucket = s3_connection.get_bucket(bucket_name, validate=False)
    key = bucket.get_key(key_name) 
    key_name = key.key
    cache_time = one_month
    key.set_metadata(name=cckey, value = ('max-age=%d, public' % (cache_time)))
    key.set_metadata(name='content-type', value = key.content_type) # preserve the content-type
    
    key2 = key.copy(key.bucket.name, key.name, key.metadata, preserve_acl=True)