I encountered this error when I had a bucket with dots (.
) in name, like: cdn.dev.company.com
(which was used with Cloudflare (not AWS Cloudfront) as CDN for serving media files). Below is python code snippet which was used by backend to generate preseigned URLs (that were used by frontend to upload video files directly to S3 bucket). Check the comment next to "client" variable line. In that configuration, it worked well (you also need to add CORS policy in bucket details - which is already described in this thread).
import boto3
from botocore.client import Config
from django.conf import settings
session = boto3.Session(
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_S3_REGION_NAME,
)
# you need to define endpoint_url and addressing_style as virtual in order
# to generate URLs that are friendly for web-browser and work well with CORS
client = session.client(
"s3",
endpoint_url=f"https://s3.{settings.AWS_S3_REGION_NAME}.amazonaws.com",
config=Config(s3={"addressing_style": "virtual"}),
)
bucket = settings.AWS_STORAGE_BUCKET_NAME
key = "file.png"
upload_id = "zxcv"
part_number = "abcd"
default_url_expiration = 1200
client.generate_presigned_url(
ClientMethod="upload_part",
Params={
"Bucket": bucket,
"Key": key,
"UploadId": upload_id,
"PartNumber": part_number,
},
ExpiresIn=default_url_expiration,
HttpMethod="PUT",
)