Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Integration with CloudFlare R2 #228

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

rhamses
Copy link
Contributor

@rhamses rhamses commented Feb 5, 2024

This PR creates an api endpoint that abstracts object storage and integrates with CloudFlare R2 for uploading and manage all type of files while adding support for returning S3 Presigning URLs, base64, public url and more.

@lane711 and @acoreyj I'd love to hear your thoughts specially because @acoreyj already has a similar PR on the queue. I could mark this as WIP while we discuss the progress of this PR.

What this PR do:

  • Creates a /bucket endpoint that supports POST as an application/form-data that handles a form with field called file representing a file upload.

  • On POST the api can customize the return using the method query parameter as one of the options url | tempurl | blob | base64 | arrayBuffer | sha1 | text | null - each one being a type of return explained below

  • On GET the way to retrieve the object from the bucket is to pass as query string the values of key being the R2 file key name and optionally the method parameter like on POST

  • On Delete the request body has to contain the name filename with the R2 Object key so it can be deleted.

  • It works with aws/sdk like the docs says to presign urls and creates the security layer on top of the objects.

  • On wrangler.toml It creates a new binding entry:

[[r2_buckets]]
binding = "R2_BUCKET"
bucket_name = "sonicjs"
[vars]
BUCKET_CUSTOM_DOMAIN = "https://sonicjs-media.amb1.io"
  • Also, to be able to create the Presign URL and generally use the S3 api it'll require to inform the bucket's access_key_id and secret_access_key
[vars]
BUCKET_ACCESS_KEY_ID = "0000000000000000000000000000000"
BUCKET_SECRET_ACCESS_KEY = "0000000000000000000000000000000000000000000000000000000000000000"

What this PR dosen't do:

  • It does not process in any way any type of files like: resize images. The file that is uploaded is gonna be stored in the client's bucket without modifications.
  • Right now it does not have a frontend demonstration on the admin section. I did not have the time to do it since I've been working on a side admin section.

( here I think we could merge the work that @acoreyj did and maybe creates a type of middleware for this request)

Parameters explained

blob | arrayBuffer | text | json | readblestream

According to the docs that is the possible returns from the R2 bucket when we retrieve an existing object from the bucket

sha1 | base64

This are returns created by the API consuming the arrayBuffer retrieved by the bucket and then transforming into a more ready-to-go output

tempurl | url

The tempurl is the S3 Presign output since this links has an expiry date that can be configured (but right now is set to 1 hour).
The url is the public URL of the image if the credentials is set on the wrangler.toml file.

Live Example

A live version of this PR can be found here.

This commit creates the integration between CloudFlare R2 and SonicJS. The core function abstract the three methods of CRUD with the idea to be extendable in the future for other platforms. A sample route is created to show the basic "upload and show" result. Basic tests were created but can be improved.
- added an example endpoint on the api.ts and updated the screen version.
- Updated the Bucket class to return object with the File info (size, name, type, sha1).
- created an function that return sha1 of the image.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant