PUT object with presigned URL sets the incorrect Type in R2 object

Hey! I'm trying to put an object to a bucket using presigned URL. But for some reason in the R2 I can always see the file type: application/x-www-form-urlencoded even when the file is a jpg, png,... This is the code I'm using in nodejs:
const config = {
header: {
'Content-Type': 'image/jpeg'
}
};

axios.put(url, photo.value, config)
.catch(error => {
console.log("Error");
});
const config = {
header: {
'Content-Type': 'image/jpeg'
}
};

axios.put(url, photo.value, config)
.catch(error => {
console.log("Error");
});
The image is correctly uploaded, but the type in the R2 bucket is wrong as explained before. If I download the image, I can open it correctly and windows shows it as a .jpg as expected. To generate the presigned URL I use python's boto3 package:
url = s3.generate_presigned_url('put_object',
Params={'Bucket': bucket_name, 'Key': object_name},
ExpiresIn=expiration,
HttpMethod='PUT')
url = s3.generate_presigned_url('put_object',
Params={'Bucket': bucket_name, 'Key': object_name},
ExpiresIn=expiration,
HttpMethod='PUT')
I saw this post of a user having a similar problem on your forums, but with no response: https://community.cloudflare.com/t/jpg-on-r2-served-with-wrong-content-type/484955/1
1 Reply
lpares12
lpares12OP3mo ago
Note that setting the ContentType when creating the presigned URL just makes the request return a CORS error. E.g: this does not work:
url = s3.generate_presigned_url('put_object',
Params={
'Bucket': bucket_name,
'Key': object_name,
'ContentType': 'image/jpeg'},
ExpiresIn=expiration,
HttpMethod='PUT')
url = s3.generate_presigned_url('put_object',
Params={
'Bucket': bucket_name,
'Key': object_name,
'ContentType': 'image/jpeg'},
ExpiresIn=expiration,
HttpMethod='PUT')
Okay, this seems like a limitation of R2: https://github.com/boto/boto3/issues/934 Since R2 does not allow POST for uploading objects to the bucket, so there's no way around it. In any case, R2 should read the content-type of the request I'm making I believe.
Want results from more Discord servers?
Add your server