1234567891011121314151617181920212223242526272829 |
- def upload_s3(dirpath: str) -> None:
- import boto3, time
- real_filepath = ''
- compressed_filepath = compress_backup(dirpath)
- if c.S3_GPG_ENCRYPT and not c.NOGPG:
- real_filepath = gpg_encrypt_file(compressed_filepath)
- real_filepath = compressed_filepath if not real_filepath else real_filepath
- datepart = time.strftime("%Y%m%d%H%M%S")
- remote_filename = os.path.split(real_filepath)[1] + '.' + datepart
- with open(real_filepath, 'rb') as data:
- message('Uploading file to Amazon S3')
- if not c.DRY_RUN:
- s3 = boto3.client('s3', aws_access_key_id=c.S3_ACCESS_KEY,
- aws_secret_access_key=c.S3_SECRET_KEY)
- s3.upload_fileobj(data, c.S3_BUCKET, remote_filename)
- if not c.DRY_RUN:
- if c.S3_GPG_ENCRYPT:
- # Remove the local encrupted file
- os.unlink(real_filepath)
- os.unlink(compressed_filepath)
- message('File uploaded to S3 bucket "{}" as key "{}"'.
- format(c.S3_BUCKET, remote_filename))
|