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/
Cloudflare Workers
YouTube
Access R2 Storage from a Worker
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/...
Use R2 from Workers · Cloudflare R2 docs
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 …
4 Replies
kian
kian11mo ago
I’m a little confused as to what your code is doing - that isn’t from either the video or the documentation
MyFriendliestFace
MyFriendliestFaceOP11mo ago
its the same code. i retyped from the video, but the documentation is faster to copy over, just the last step for authentication needs to be added
kian
kian11mo ago
Yes, but “new R2Bucket” and “r2Object.fetch” aren’t
MyFriendliestFace
MyFriendliestFaceOP11mo ago
oh, r2Object is the name of the file which holds that code, so i am trying to import that file/object and initialize an intance and save a sample file. fetch is the function from the documentation, see it says "export default" but no name
Want results from more Discord servers?
Add your server