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

sanity-plugin-cloudflare-r2-files

v0.0.9

Published

Store Sanity media files in Cloudflare R2 Bucket

Downloads

662

Readme

Sanity.io - Cloudflare R2

Allows uploading, referencing and deleting files to Cloudflare R2 directly from your Sanity studio. Is a flavor of sanity-plugin-external-files.

Screenshot of the plugin

Why Cloudflare?

  • Cost-effective: Cloudflare R2 is a cost-effective solution for storing large files. You only pay for what you use. No egress fees.
  • Fast: Cloudflare R2 is built on Cloudflare's global network, making it fast to upload and download files.
  • Secure: Cloudflare R2 is built on Cloudflare's security-first architecture, making it secure by default.
  • Simplicity: Cloudflare R2 is easy to set up and use.

Using the plugin

  1. Configure Cloudflare R2 Bucket
  2. Configure Sanity Studio

Configuring the Cloudflare R2 Bucket

  1. Create Cloudflare Account here
  2. Create a new R2 Bucket (e. g. sanity-media)
  3. Either use the R2.dev public domain or add your custom domain
  4. Deploy the Cloudflare Worker as described below
  5. Add the worker URL to your plugin configuration (workerUrl)
  6. Add the R2 Bucket URL (either R2.dev subdomain or custom domain) to your plugin configuration (url)

Deploy Cloudflare Worker

The plugin requires a Cloudflare Worker to handle the file uploads and deletions. You can find the code for the worker in the worker directory of this repository. This is required because Sanity Studio doesn't support any server-side logic.

  1. Install the Wrangler CLI
  2. Login to your Cloudflare account by running wrangler login
  3. git clone this repository (git clone https://github.com/hdoro/sanity-plugin-external-files)
  4. cd into the worker directory (cd packages/cloudflare-r2/worker)
  5. Adjust the wrangler.toml file and configure ALLOWED_ORIGINS and bucket_name to match your setup
  6. Add SECRET as Cloudflare Secret as described here (e. g. SECRET=your-secret)
  7. Run wrangler publish to deploy the worker
  8. Copy the worker URL from the output and add it to your plugin configuration

Cloudflare R2 Bucket with R2.dev Public Domain

  1. Login to your Cloudflare account here
  2. Go to "R2" and either create a new bucket or choose your existing one (e. g. sanity-media)
  3. Go to "Settings" and choose "R2.dev subdomain"
  4. Hit "Enable"

Cloudflare R2 Bucket with Custom Public Domain

  1. Login to your Cloudflare account here
  2. Go to "Website" and choose "Add domain" (e. g. example.com)
  3. Follow the instructions to add your domain
  4. Go to "R2" and either create a new bucket or choose your existing one (e. g. sanity-media)
  5. Go to "Settings" and choose "Custom domain"
  6. Add your custom domain (or subdomain) by entering it and follow the instructions to add the necessary DNS records

Configuring Sanity Studio

  1. Install the plugin sanity-plugin-cloudflare-r2-files by running:
npm i sanity-plugin-cloudflare-r2-files
yarn add sanity-plugin-cloudflare-r2-files
pnpm i sanity-plugin-cloudflare-r2-files
  1. Include the plugin in your sanity.config.(js|ts):
import { cloudflareR2Files } from 'sanity-plugin-cloudflare-r2-files'
import { defineConfig } from 'sanity'

export default defineConfig({
  plugins: [
    cloudflareR2Files({
      toolTitle: 'Media Library',
      credentials: {
        url: 'https://<random>.r2.dev',
        workerUrl: 'https://<worker>.<user>.workers.dev'
      },
    }),
  ],
})
  1. And use its cloudflare-r2-files.media type in schemas you want to use Cloudflare R2 files from:
export default {
  name: 'caseStudy',
  type: 'document',
  fields: [
    {
      name: 'featuredVideo',
      type: 'cloudflare-r2-files.media',
      options: {
        accept: {
          'video/*': ['mp4', 'webm', 'mov'],
        },
      },
    },
  ],
}

Data structure & querying

Each media item is a Sanity document that holds information of the object stored in Cloudflare R2, like its fileURL, contentType and fileSize. It's analogous to Sanity's sanity.imageAsset and sanity.fileAsset: they're pointers to the actual blob, not the files themselves.

These files' type is cloudflare-r2-files.storedFile.

When selected by other document types, media is stored as references to these file documents. You can get the URL of the actual assets by following references in GROQ:

*[_type == 'caseStudy'] {
  ...,
  
  featuredVideo-> {
    fileSize,
    fileURL,
    cloudflareR2 {
      fileKey,
      baseUrl,
    },
  },
}

Contributing, roadmap & acknowledgments

Refer to sanity-plugin-external-files for those.