Create a tar file that contains all the files in a directory

  aws-lambda, lambda, python, tar

I have this code that creates a file "called" tar for all the files in a directory but it is really just a text file and not a tar file, What have I done wrong?

def get_matching_s3_keys(bucket, prefix='', suffix=''):

    """
    Generate the keys in an S3 bucket.

    :param bucket: Name of the S3 bucket.
    :param prefix: Only fetch keys that start with this prefix (optional).
    :param suffix: Only fetch keys that end with this suffix (optional).
    """
    s3 = boto3.client('s3')
    kwargs = {'Bucket': bucket}

    # If the prefix is a single string (not a tuple of strings), we can
    # do the filtering directly in the S3 API.
    if isinstance(prefix, str):
        kwargs['Prefix'] = prefix

    while True:

        # The S3 API response is a large blob of metadata.
        # 'Contents' contains information about the listed objects.
        resp = s3.list_objects_v2(**kwargs)
        for obj in resp['Contents']:
            key = obj['Key']
            if key.startswith(prefix) and key.endswith(suffix):
                yield key

        # The S3 API is paginated, returning up to 1000 keys at a time.
        # Pass the continuation token into the next response, until we
        # reach the final page (when this field is missing).
        try:
            kwargs['ContinuationToken'] = resp['NextContinuationToken']
        except KeyError:
            break
            
def lambda_handler(event, context):
    
    messages = ''
    newDate = 20201020
    agtBucket = 'agt-logs'
    key = 'hourly_logs/'
    to_key = 'daily_logs/'

    my_bucket = s3object.Bucket(agtBucket)
    files_list = [] 
    tar = tarfile.open('/tmp/' + newDate + '.tar', 'w')
    source_dir="/tmp/"
    for fname in get_matching_s3_keys(bucket=agtBucket, prefix=key, suffix='.log'):
        print(fname)
        file_obj = s3object.Object(agtBucket, fname)
        #file_content = file_obj.get()['Body'].read()
        #tar.add(file_content)
        s3object.Bucket(agtBucket).download_file(fname, '/tmp/'+fname[12])
        tar.add(source_dir, arcname=os.path.basename(source_dir))
        s3object.Object(agtBucket,fname).delete()
    tar.close()
    s3object.meta.client.upload_file(source_dir + newDate + '.tar', agtBucket, to_key + 's3Download_' + newDate + '.tar')

Source: Python Questions

LEAVE A COMMENT