0

I am looking for a way to use a custom domain with the S3 pre signed post functionality. Right now the URL returned is the default S3 bucket URL e.g. https://mybucket.s3.amazonaws.com/. Using python I generate the pre-signed post data as such:

content_type = "text/csv"
data = s3.generate_presigned_post(
            Bucket="my-bucket",
            Key=path,
            Fields={
                "Content-Type": content_type
            },
            Conditions=[
                {"Content-Type": content_type},
                ["content-length-range", 0, 10 * 1000000]
            ],
            ExpiresIn=300,
        )

The data returned by boto3 to perform a multi-part form upload is:

{
        "url": "https://my-bucket.s3.amazonaws.com/",
        "fields": {
            "Content-Type": "text/csv",
            "key": "pri...",
            "AWSAccessKeyId": "A....",
            "policy": "e....",
            "signature": "CJR..."
        }
}

I would like to get a custom domain as the "url" part to upload to. How can I do this?

Edit: This question is about AWS S3 Pre-Signed POST data for multi-part form upload. Not Pre-Signed URLs.

Jelle
  • 1,020
  • 2
  • 13
  • 22
  • Does this answer your question? [AWS Get Pre-Signed URL with custom domain](https://stackoverflow.com/questions/53128521/aws-get-pre-signed-url-with-custom-domain) – esqew Feb 23 '23 at 14:13
  • No, this is about generating pre-signed POST data. Not a pre-signed URL which has less functionality. @esqew – Jelle Feb 23 '23 at 14:14
  • Did you manage to figure this out? – robert smith Jun 02 '23 at 15:53

1 Answers1

0
import boto3
from botocore.client import Config

bucket_name = 'your-bucket-name'
object_name = 'your-object-name'

# set up the S3 client with a custom endpoint URL
endpoint_url = 'https://your-custom-domain.com'
s3_client = boto3.client('s3', endpoint_url=endpoint_url, config=Config(signature_version='s3v4'))

# generate a pre-signed POST URL for the object
response = s3_client.generate_presigned_post(bucket_name, object_name)

# extract the URL and form data from the response
url = response['url']
fields = response['fields']

# add any additional form fields that you need to include
fields['key'] = object_name
fields['success_action_redirect'] = 'https://your-redirect-url.com'

# print out the URL and form data
print(f"POST URL: {url}")
print("Form fields:")
for name, value in fields.items():
    print(f"  {name}: {value}")
Mrinal
  • 125
  • 5