How to use AWS SDK to upload files to S3?

To upload files to Amazon S3 using the AWS SDK, you can follow these steps:

  1. Install the AWS SDK for your desired programming language (e.g. Python, Java, JavaScript/Node.js, etc.).

  2. Configure the AWS SDK with your AWS credentials and the region of the S3 bucket you want to upload files to. Here is an example of how to configure the AWS SDK in Python:

import boto3 # Create an S3 client s3 = boto3.client('s3', aws_access_key_id='YOUR_ACCESS_KEY', aws_secret_access_key='YOUR_SECRET_KEY', region_name='YOUR_REGION')
  1. Use the put_object method provided by the S3 client to upload files to S3. Here is an example of how to upload a file in Python:
# Upload a file to S3 file_path = 'path/to/file.jpg' bucket_name = 'your-bucket-name' s3.upload_file(file_path, bucket_name, 'file.jpg')
  1. You can also use the put_object method with additional parameters to specify metadata or permissions for the uploaded file. Here is an example in Python:
# Upload a file to S3 with metadata and permissions file_path = 'path/to/file.jpg' bucket_name = 'your-bucket-name' s3.upload_file(file_path, bucket_name, 'file.jpg', ExtraArgs={'ACL': 'public-read', 'ContentType': 'image/jpeg'})
  1. After you have uploaded the file to S3, you can access it using the S3 URL provided by AWS, which will be in the format https://s3.<region>.amazonaws.com/<bucket-name>/<filename>.