How to successfully initialize the R2 object?
Hello all, first off, I apologize for a potentially dumb question. I am not able to initialize an R2 object and test it out correctly. I have the R2 object code taken from the documentation, which is shown below. I am trying to call this object from a different module and execute it, but its just erroring out whatever i try. here is my latest attempt, getting errors about that I didn't initialize the object properly or that I didn't have the right formats. I appreciate anyone who can give me pointers, nothing is available on google.
////////////////////////////////////////////////////
my attempt at creating sample data in r2
///////////////////////////////////////////////////
// r2PushData.js
import r2Object from './objectR2';
// Create an instance of Env
const env = {
R2_BUCKET: R2Bucket,
AUTH_KEY_SECRET: 'hidden-from-chat',
};
// Create an instance of R2Bucket
env.R2_BUCKET = new R2Bucket;
// Use the exported fetch function to push data to the R2 bucket
const requestData = "Test body 'first upload of data to r2'";
const request = new Request('https://hiddenurl.com/test', {
method: 'PUT',
body: requestData,
headers: {
'X-Custom-Auth-Key': env.AUTH_KEY_SECRET,
},
});
r2Object.fetch(request, env, {}).then((response) => {
console.log(response.status);
console.log(response.statusText);
});
///////////////////////////////////////
This is the object below, no errors with the file.
//////////////////////////////////////
// objectR2.ts
I dont have space to include all code, but it is this object from this support video https://www.youtube.com/watch?v=VEATzKttaMI&t=331s
and also this article (if you want to copy/paste code)
https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
////////////////////////////////////////////////////
my attempt at creating sample data in r2
///////////////////////////////////////////////////
// r2PushData.js
import r2Object from './objectR2';
// Create an instance of Env
const env = {
R2_BUCKET: R2Bucket,
AUTH_KEY_SECRET: 'hidden-from-chat',
};
// Create an instance of R2Bucket
env.R2_BUCKET = new R2Bucket;
// Use the exported fetch function to push data to the R2 bucket
const requestData = "Test body 'first upload of data to r2'";
const request = new Request('https://hiddenurl.com/test', {
method: 'PUT',
body: requestData,
headers: {
'X-Custom-Auth-Key': env.AUTH_KEY_SECRET,
},
});
r2Object.fetch(request, env, {}).then((response) => {
console.log(response.status);
console.log(response.statusText);
});
///////////////////////////////////////
This is the object below, no errors with the file.
//////////////////////////////////////
// objectR2.ts
I dont have space to include all code, but it is this object from this support video https://www.youtube.com/watch?v=VEATzKttaMI&t=331s
and also this article (if you want to copy/paste code)
https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
YouTubeCloudflare Workers
In this video , we'll show you how to integrate R2 storage into your applications using Cloudflare's Workers API and Wrangler, a command-line tool for building and deploying Cloudflare Workers. With R2's global object storage and zero egress fees, building scalable and efficient applications has never been easier!
Demo Code: https://github.com/...
Demo Code: https://github.com/...

C3 (create-cloudflare-cli) is a command-line tool designed to help you set up and deploy Workers & Pages applications to Cloudflare as fast as …
