Unable to upload image to R2 via workers

Hey folks I am unable to upload an image to my R2 bucket with custom domain endpoints, using my workers api. Here is my code. When executed, there is no error. And the web based dashboard doesn't shows the image in the bucket.
if (thumb) {
if (thumb.size > UserControlsSchema.thumbMinSize
&& thumb.size < UserControlsSchema.thumbMaxSize
&& UserControlsSchema.thumbFileTypes.includes(thumb.type)
) {
await ctx.env.STORAGE.put('someid', thumb.stream())
let thumbURL = `https://images.mydomain.com/${fileId}`
} else {
throw new ServerError("InvalidRequestData", "User thumb doesn't meets the schema requirements");
}
}
if (thumb) {
if (thumb.size > UserControlsSchema.thumbMinSize
&& thumb.size < UserControlsSchema.thumbMaxSize
&& UserControlsSchema.thumbFileTypes.includes(thumb.type)
) {
await ctx.env.STORAGE.put('someid', thumb.stream())
let thumbURL = `https://images.mydomain.com/${fileId}`
} else {
throw new ServerError("InvalidRequestData", "User thumb doesn't meets the schema requirements");
}
}
in my wrangler.toml file, I have bound the R2 details as
[[r2_buckets]]
binding = 'STORAGE' # <~ valid JavaScript variable name
bucket_name = 'assets'
[[r2_buckets]]
binding = 'STORAGE' # <~ valid JavaScript variable name
bucket_name = 'assets'
Since I am uploading from my workers api and not from a browser POST, I do not think I need to set anything like an AUTH_SECRET, or do I? I have also tried with and without CORS in the bucket settinsg without any success.
4 Replies
Rick
RickOP14h ago
Furthermore, when I print the r2Object, the output suggests that the upload was successful. But I cannot see the images in my bucket via the dashboard, nor can I access them using the custom domain. Anything that I directly upload to the bucket using the web dashboard can be seen and accessed via the custom domain.
R2 object: HeadResult {
ssecKeyMd5: undefined,
storageClass: '',
range: undefined,
customMetadata: {},
httpMetadata: {},
uploaded: 2025-01-30T09:19:51.241Z,
checksums: Checksums {
sha512: undefined,
sha384: undefined,
sha256: undefined,
sha1: undefined,
md5: ArrayBuffer {
[Uint8Contents]: <5a 57 6d f0 11 76 7a fd 18 fb 73 79 24 79 44 82>,
byteLength: 16
}
},
httpEtag: '"5a576df011767afd18fb737924794482"',
etag: '5a576df011767afd18fb737924794482',
size: 30335,
version: 'a2a8afad4456c043a6ba1a34a387cac2',
key: 'nohitb78plucoixwxvoqoxqqwbvc92ob__Screenshot 2025-01-24 161252.png'
}

R2 object: HeadResult {
ssecKeyMd5: undefined,
storageClass: '',
range: undefined,
customMetadata: {},
httpMetadata: {},
uploaded: 2025-01-30T09:19:51.241Z,
checksums: Checksums {
sha512: undefined,
sha384: undefined,
sha256: undefined,
sha1: undefined,
md5: ArrayBuffer {
[Uint8Contents]: <5a 57 6d f0 11 76 7a fd 18 fb 73 79 24 79 44 82>,
byteLength: 16
}
},
httpEtag: '"5a576df011767afd18fb737924794482"',
etag: '5a576df011767afd18fb737924794482',
size: 30335,
version: 'a2a8afad4456c043a6ba1a34a387cac2',
key: 'nohitb78plucoixwxvoqoxqqwbvc92ob__Screenshot 2025-01-24 161252.png'
}

_.Mass._
_.Mass._13h ago
hey RIck, you don't need any auth secret nor cors settings when using the workers api. At first glance I don't see anything wrong with how you are storing the image. I have found this repo https://github.com/yusukebe/r2-image-worker you could try running it with your assets bucket configured and see if the images are being saved. At least it should help and tell you where to look
GitHub
GitHub - yusukebe/r2-image-worker: Store and Deliver images with R2...
Store and Deliver images with R2 backend Cloudflare Workers. - yusukebe/r2-image-worker
Rick
RickOP5h ago
Thank you @.Mass. . I think I have found out the issue. I am doing local dev and my uploads are getting stashed in the local r2. the linked repo helped me find that I need to use the --remote arg to use the cloud based r2 instance. Would have helped if the R2 docs had this written anywhere. I found most of this info in the workers docs.
_.Mass._
_.Mass._3h ago
@Rick I am glad you solved the issue. I agree, the docs are generally pretty good but miss some details here and there.

Did you find this page helpful?