JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1243
  • Score
    100M100P100Q108924F
  • License ISC

S3 wrapper for Cloudflare R2.

Package Exports

  • node-cloudflare-r2
  • node-cloudflare-r2/types

Readme

Cloudflare-R2

This is a wrapper of the AWS S3 client library, designed to provide a user-friendly and efficient way to interact with Cloudflare R2 API in Node.js

Why make this library?

  • As of the writing of this README, there is no official Node.js library for Cloudflare R2.
  • Interacting with object storage APIs, especially Cloudflare R2, should be simple and straightforward.

⚠ This library is currently in development and is not yet ready for production use. It is subject to change and may contain bugs or other issues. Please use it at your own risk.

Installation

npm

npm install node-cloudflare-r2

pnpm

pnpm install node-cloudflare-r2

It is highly recommended that you use a specific version number in your installation to anticipate any breaking changes that may occur in future releases. For example:
npm install node-cloudflare-r2@0.2.0
or
pnpm install node-cloudflare-r2@0.2.0

Check the latest version number in the release page.

Example

import { R2 } from 'node-cloudflare-r2';
import { createReadStream } from 'fs';

const r2 = new R2({
    accountId: '<YOUR_ACCOUNT_ID>',
    accessKeyId: '<YOUR_R2_ACCESS_KEY_ID>',
    secretAccessKey: '<YOUR_R2_SECRET_ACCESS_KEY>',
});

const bucket = r2.bucket('<BUCKET_NAME>');

// Set your bucket's public URL
bucket.provideBucketPublicUrl('https://pub-xxxxxxxxxxxxxxxxxxxxxxxxx.r2.dev');

console.log(await bucket.exists());
// true

// Upload local file
const upload = await bucket.uploadFile('/path/to/file', 'destination_file_name.ext');
console.log(upload);
/*
{
    objectKey: 'destination_file_name.ext',
    uri: 'destination_file_name.ext',
    publicUrl: 'https://pub-xxxxxxxxxxxxxxxxxxxxxxxxx.r2.dev/destination_file_name.ext',
    etag: '',
    versionId: '',
    }
*/

// Upload file or stream
const uploadStream = await bucket.uploadStream(createReadStream('/path/to/file'), 'destination_file_name-stream.ext');