i trying host media/image files on cloudflare r2 bucket. I tried lots of stuff but I still can't reach bucket from outsite with nodeJS.
How can i fix this code?
My Final Code:
fetch('https://USER_ID.r2.cloudflarestorage.com/PROJECT_NAME/src/thumbs/BLAHBLAH', {
method: 'GET',
headers: {
'Content-Type': 'image/jpeg',
'Authorization': 'SECRET_KEY_FOR_API',
'X-Amz-Ac': 'private',
'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
'X-Amz-Date': (new Date().toISOString().split(':').join('').split('.')[0] + 'Z').split('-').join(''),
'X-Amz-Expires': '86400',
'x-amz-content-sha256': 'UNSIGNED-PAYLOAD',
}}).then(res => res.text()).then(buffer => {
console.log(buffer);
}).catch(err => {
console.log(err);
});
You'll need to implement proper SigV4 signing for your request.
It'll be easiest to use one of the AWS SDKs, like aws-sdk-js
- take a look at the example documentation for R2: https://developers.cloudflare.com/r2/examples/aws-sdk-js-v3/