Skip to content

Instantly share code, notes, and snippets.

@Ahwar
Last active November 10, 2022 02:50
Show Gist options
  • Save Ahwar/326778ac8c6ee9fa1b87b795b4c0c916 to your computer and use it in GitHub Desktop.
Save Ahwar/326778ac8c6ee9fa1b87b795b4c0c916 to your computer and use it in GitHub Desktop.
Upload a single file to amazon s3 bucket. The upload_file method accepts a file name, a bucket name, and an object name. The method handles large files by splitting them into smaller chunks and uploading each chunk in parallel.
import logging
import boto3
from botocore.exceptions import ClientError
import os
"""
Upload a single file to amazon s3 bucket
The upload_file method accepts a file name, a bucket name, and an object name.
The method handles large files by splitting them into smaller chunks and uploading each chunk in parallel.
reference: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html
"""
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = os.path.basename(file_name)
# Upload the file
s3_client = boto3.client(
's3',
aws_access_key_id="<your-aws-access-key-id?",
aws_secret_access_key="<your_aws_secret_access_key>",
aws_session_token="<your_aws_session_token>" # create your session key from https://unix.stackexchange.com/a/481609
)
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
upload_file("<file-to-upload>", "<bucket-name>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment