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,
},
fallbackRegion: 'wnam',
});
Head
const object: R2Object | null = await localizedBucket.head('some-object', {
request,
});
Get
const object: R2Object | R2ObjectBody | null = await localizedBucket.get(
'some-object',
{
request,
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,
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,
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,
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',
onlyIf: {
},
}
);
License
This repository is licensed under the terms of the MIT License.