3 ms·
Got any examples for https://github.com/aws/aws-sdk-js-v3 https://github.com/aws/aws-sdk-js-v3 or https://github.com/mhart/aws4fetch https://github.com/mhart/aw
by eins1234 4y ago
Got any examples for https://github.com/aws/aws-sdk-js-v3 https://github.com/aws/aws-sdk-js-v3 or https://github.com/mhart/aws4fetch https://github.com/mhart/aws4fetch?
I haven't been able to get either working:
const s3Client = new S3Client({
endpoint: 'https://my-account-id.r2.cloudflarestorage.com',
credentials: secrets.r2Credentials,
})
await s3Client.send(
new PutObjectCommand({
Bucket: 'my-bucket,
Key: 'test',
Body: JSON.stringify({hello: 'world'}),
}),
)
^ This fails with a 500
const r2Client = new AwsClient({
...secrets.r2Credentials,
})
fetch(
await r2Client.sign(
'https://my-account-id.r2.cloudflarestorage.com/' +
'?Key=test&Bucket=my-bucket',
{
method: 'put',
body: JSON.stringify({ hello: 'world' }),
},
),
).then((response) => console.log(response.status))
^ This fails with 401
I've verified that the key has full access and even rerolled it to make sure I didn't mess up the copy paste, but still no luck.
Any ideas where I could be going wrong?
- vlovich123 4y agoWe have several examples [1]. I think you need to set s3ForcePathStyle to true for the first example (or however you do it in the v3 version of the SDK[2]). For the latter, we don't support pre-signed requests. Feel free to ask on discord [3] too. Might get more timely responses. [1] https://developers.cloudflare.com/r2/examples https://developers.cloudflare.com/r2/examples [2] https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/bucketendpointinputconfig.html https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clien... [3] https://discord.gg/cloudflaredev https://discord.gg/cloudflaredev
- eins1234 4y agoThanks for the resources! I managed to get both working in the end. Some notes for future readers: For SDK v3, I just needed to add the forcePathStyle flag. For aws4fetch (note that this isn't a presigned request, it's just signing the request for Sig V4 like the official SDK would), I needed to change the fetch url to https://my-account-id.r2.cloudflarestorage.com/my-bucket/key https://my-account-id.r2.cloudflarestorage.com/my-bucket/key.