Giter VIP home page Giter VIP logo

r2-localize's Introduction

r2-localize

Allows for serving files from different R2 buckets based on the request's region.

Primary use cases would be lowering latency or serving localized files.

Usage

Initializing

const localizedBucket = localizeBucket({
  buckets: {
    wnam: env.WNAM_BUCKET,
    enam: env.ENAM_BUCKET,
    weur: env.WEUR_BUCKET,
    eeur: env.EEUR_BUCKET,
    apac: env.APAC_BUCKET,
  },
  // Region to use if one of the regions is undefined or we couldn't identify what region a colo is in/closest to
  fallbackRegion: 'wnam',
});

Head

// Heading a file
const object: R2Object | null = await localizedBucket.head('some-object', {
  request,
});

Get

const object: R2Object | R2ObjectBody | null = await localizedBucket.get('some-object', {
  request,
  // R2GetOptions properties here, e.g.
  onlyIf: {
    // ...
  },
});

Put

Note: this puts the object in the nearest bucket only. The other buckets are unaffected.

const object: R2Object | null = await localizedBucket.put(
  'some-object',
  'some-value',
  {
    request,
    // R2PutOptions properties here, e.g.
    onlyIf: {
      // ...
    },
  }
);

CreateMultipartUpload

Note: this puts the object in the nearest bucket only. The other buckets are unaffected.

const object: R2MultipartUpload = await localizedBucket.createMultipartUpload(
  'some-object',
  {
    request,
    // R2MultipartOptions properties here, e.g.
    httpMetadata: {
      // ...
    },
  }
);

ResumeMultipartUpload

Note: this puts the object in the nearest bucket only. The other buckets are unaffected.

const object: R2MultipartUpload = await localizedBucket.resumeMultipartUpload(
  'some-object',
  'upload id',
  { request }
);

Delete

Note: this deletes the object in the nearest bucket only. The other buckets are unaffected.

await localizedBucket.resumeMultipartUpload(
  'some-object',
  'upload id',
  { request }
);

List

const object: R2Objects = await localizedBucket.list({
  request,
  // R2ListOptions properties here, e.g.
  prefix: 'asd',
});

Accessing a specific region

To access the bucket of a specific region, you can pass in the region's name instead of the request object.

const object: R2Object | R2ObjectBody | null = await localizedBucket.get('some-object', {
  region: 'wnam',
  // R2GetOptions properties here, e.g.
  onlyIf: {
    // ...
  },
});

License

This repository is licensed under the terms of the MIT License.

r2-localize's People

Contributors

flakey5 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.