import sys
import boto3
from datetime import date
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

args = getResolvedOptions(sys.argv,['JOB_NAME','SOURCE_BUCKET','TARGET_BUCKET'])

#instantiate the s3 bucket using boto3
sourcebucket =  args['SOURCE_BUCKET']
s3 = boto3.resource('s3')
my_bucket = s3.Bucket(sourcebucket)
targetbucket = args['TARGET_BUCKET']

#copy the file from source to target
for obj in my_bucket.objects.all():
    source_filename = (obj.key).split('/')[-1]
    copy_source = {
        'Bucket': sourcebucket,
        'Key': obj.key
    }
    target_filename = "{}/{}".format(targetbucket,source_filename)
    s3.meta.client.copy(
        copy_source,
        Bucket = targetbucket, 
        Key = str(date.today())+'/'+obj.key,
        ExtraArgs = {
            'StorageClass': 'STANDARD_IA',
            'MetadataDirective': 'COPY'
        }
        )
    #Delete the file from source
    s3.Object(sourcebucket, obj.key).delete()
