How to create a pre-signed URL for an S3 object?

To create a pre-signed URL for an S3 object, you can use the AWS SDK for your programming language of choice. Here is an example using the AWS SDK for Python (Boto3):

  1. Install boto3 using pip:
pip install boto3
  1. Create a new Python script and import the necessary libraries:
import boto3 import botocore import datetime
  1. Initialize the S3 client:
s3 = boto3.client('s3')
  1. Generate the pre-signed URL for the S3 object:
bucket_name = 'your_bucket_name' object_key = 'your_object_key' expiration = datetime.datetime.now() + datetime.timedelta(hours=1) try: response = s3.generate_presigned_url( 'get_object', Params={'Bucket': bucket_name, 'Key': object_key}, ExpiresIn=3600 ) print(response) except botocore.exceptions.ClientError as e: print(e)

Replace 'your_bucket_name' and 'your_object_key' with the actual bucket name and object key of the S3 object you want to create a pre-signed URL for. The ExpiresIn parameter specifies the expiration time of the pre-signed URL in seconds.

  1. Run the script, and it will output the pre-signed URL that can be used to access the S3 object for the specified expiration time.

Make sure to handle any errors that may occur during the generation of the pre-signed URL.