Upload file to S3 with Python
Upload file to S3 with Python
New chat
To upload a file to AWS S3 using Python, you can use the `boto3` library, which is the official
AWS SDK for Python. Follow these steps:
1. Install the `boto3` library if you haven't already. You can use pip to install it:
shell Copy code
import boto3
import botocore
3. Set up your AWS credentials. You can do this by either configuring the AWS CLI or by
setting the environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
in your script.
4. Create an S3 client using the `boto3` library:
python Copy code
s3 = boto3.client('s3')
5. Specify the bucket name and the file path of the file you want to upload:
python Copy code
bucket_name = 'your-bucket-name'
file_path = 'path/to/your/file.txt'
try:
s3.upload_file(file_path, bucket_name, file_path)
print("File uploaded successfully.")
except botocore.exceptions.NoCredentialsError:
print("AWS credentials not found.")
except botocore.exceptions.ParamValidationError as e:
print("Invalid parameter: ", e)
except botocore.exceptions.EndpointConnectionError as e:
print("Could not connect to the S3 endpoint: ", e)
Send a message.
Note: The `upload_file` method automatically handles the multipart upload for larger
files.
Free Research Preview. ChatGPT may produce inaccurate information about people, places, or facts. ChatGPT May 24 Version
https://chat.openai.com 1/1