Last active
July 29, 2020 07:55
-
-
Save pushp1997/0126000cad0fe705f2a1a9f0bd77ed1d to your computer and use it in GitHub Desktop.
Use Python & Boto3 to Backup files / logs to AWS S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
from botocore.exceptions import ClientError | |
def upload_file_to_s3(file_name, bucket, object_name=None, folder_name=None): | |
""" | |
Upload a file to an S3 bucket. | |
Params: | |
file_name: File to upload | |
bucket: Bucket to upload to | |
object_name: S3 object name. If not specified then file_name is used | |
folder_name: Folder name in which file is to be uploaded | |
""" | |
# If S3 object_name was not specified, use file_name | |
if object_name is None: | |
object_name = file_name.split('/')[-1] | |
# If folder_name was specified, upload in the folder | |
if folder_name is not None: | |
object_name = f'{folder_name}/{object_name}' | |
# Upload the file | |
try: | |
s3_client = boto3.client( | |
service_name='s3', | |
aws_access_key_id=YOUR_AWS_ACCESS_KEY_ID, | |
aws_secret_access_key=YOUR_AWS_SECRET_ACCESS_KEY | |
) | |
response = s3_client.upload_file(file_name, bucket, object_name) | |
print(response) | |
except ClientError as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment