unjs / unstorage

💾 Unstorage provides an async Key-Value storage API with conventional features like multi driver mounting, watching and working with metadata, dozens of built-in drivers and a tiny core.
https://unstorage.unjs.io
MIT License
1.69k stars 126 forks source link

CloudFlare R2 (http) #318

Open juretopolak opened 10 months ago

juretopolak commented 10 months ago

Describe the feature

This is an experimental driver! This driver only works in a cloudflare worker environment and cannot be used in other runtime environments such as Node.js (r2-http driver is coming soon)

I would just like to ask if you have any approx. date when r2-http driver will be released? I would like to use CF R2 in the Node environment and I'm wondering if it is better to wait for a "native" nuxt/nitro/unstorage driver or the release is not close so it's better to go with another solution? 🙂

Thanks!

Additional information

pi0 commented 10 months ago

Hi dear @juretopolak. Feel free to draft a PR if you are willing to help supporting this driver earlier 👍🏼

juretopolak commented 10 months ago

Hi @pi0. I'm actually a TypeScript newbie, so I doubt my PR would be useful at all. 🙂 Maybe someday in the future... until then I'll keep learning and admiring the work you experts do. 👌

cosbgn commented 9 months ago

Hi @juretopolak I had to deal with this recently, I found that the best way is to use aws4fetch which works on all environments, including workers. You need something like this, inside for example /api/upload-file.js:

import { AwsClient } from 'aws4fetch'
const aws = new AwsClient({ accessKeyId, secretAccessKey })
// Then you can just send a PUT / GET / DELETE request
await aws.fetch(R2_BUCKET_URL, { body: buffer, method: 'PUT', ContentType: type })

It's super easy with aws4fetch - With native $fetch is more complex because you need to somehow generate the AWS Signature Version 4

pi0 commented 9 months ago

Really nice find. I certainly like to consider aws4fetch. There are small items (such as crypto polyfill to use uncrypto) we can either (temporarily) inline impl to make those or work with @mhart if you would like to make it happen!