npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adonis-drive-r2

v1.0.3

Published

R2 driver for AdonisJS drive

Downloads

963

Readme

gh-workflow-image npm-image license-image synk-image

Background

The Cloudflare R2 API implements the S3 API in order to enable users have a seamless migration/transition from Amazon S3 to Cloudflare R2. Because of this, this driver utilises the Amazon S3 Rest API by working within the Cloudflare S3 API compatibility instructions, which ensures S3 APIs not supported are not included in this driver.

This driver works by pointing the endpoint property required in the S3 API config to Cloudflare's R2 endpoint. See here. The Cloudflare R2 endpoint is generated within the driver by injecting the required R2 Account ID and the name of the bucket for the active driver instance.

A major difference between this R2 driver and S3 driver is that this driver only supports bucket-level visibility. This is because Cloudflare R2 only provides bucket-level ACL and Cloudflare R2 does not support for the S3 GetObjectAcl command.

Installation

yarn add adonis-drive-r2
node ace invoke adonis-drive-r2

Validating Environment Variables

The configuration for R2 driver relies on certain environment variables and it is usually a good practice to validate the presence of those environment variables.

Open env.ts file and paste the following code inside it.

R2_ACCESS_KEY_ID: Env.schema.string(),
R2_SECRET_ACCESS_KEY: Env.schema.string(),
R2_BUCKET: Env.schema.string(),
R2_ACCOUNT_ID: Env.schema.string(),
R2_PUBLIC_URL: Env.schema.string.optional(),

Update DRIVE_DISK

- DRIVE_DISK: Env.schema.enum(['local', 's3'] as const),
+ DRIVE_DISK: Env.schema.enum(['local', 's3', 'r2'] as const),

Define a New Disk for R2 Driver

Open the config/drive.ts and paste the following code snippet inside it.

{
  disks: {
    // ... other disk

    r2: {
      driver: 'r2',
      visibility: 'private',
      key: Env.get('R2_ACCESS_KEY_ID'),
      secret: Env.get('R2_SECRET_ACCESS_KEY'),
      bucket: Env.get('R2_BUCKET'),
      accountId: Env.get('R2_ACCOUNT_ID'),
      cdnUrl: Env.get('R2_PUBLIC_URL')
    }
  }
}

Note that the cdnUrl property of this R2 config (which maps to the public URl of your bucket) is required when the visibility of the disk is public.

Improve your Drive Contract

Open the contracts/drive.ts file. Add the name of the new disk to the DisksList interface

declare module '@ioc:Adonis/Core/Drive' {
  interface DisksList {
    // ... other disks
    r2: {
      config: R2DriverConfig // <-- Make sure to use the `R2DriverConfig` interface
      implementation: R2DriverContract // <-- Make sure to use the `R2DriverContract` interface
    }
  }
}

How to Obtain Public URL for Cloudflare R2 Bucket

  1. Login to your Cloudflare Dashboard.
  2. Click R2 on the sidebar and open a bucket (or create one and open it).
  3. Switch to the Settings tab.
  4. Under Bucket Access, click View Public Bucket URL and copy the URL.

Making your R2 Bucket Public

By default, Cloudflare's bucket-level ACL, only provides read access to individual files via the public URL. The public won't be able to list your bucket items via the public URL.

To make your bucket public, click on Allow Access within the Bucket Access section of the Settings tab of your bucket.

Public URLs are only useful when your bucket is public. The driver can generate signed URLs for private buckets.

Credits

A big thank you to Harminder Virk for his work of love on the AdonisJS framework and for the amazing S3 Driver for Adonisjs Drive which inspired this Driver for Cloudflare R2.