@unikvs/s3.bun
How to use @unikvs/s3.bun, which stores byte sequences in S3-compatible object storage.
Overview
@unikvs/s3.bun is a storage plugin that stores byte sequences in S3-compatible object storage. Bun only Use it for persistence in the cloud or in S3-compatible storage.
nameis"S3", andisOpenreports whether it is open.open()creates the client, andclose()releases the reference.- This storage performs all operations against the bucket given in the constructor, using keys as-is as object keys.
Install it with the following command:
npm install @unikvs/s3.bunpnpm add @unikvs/s3.bunyarn add @unikvs/s3.bunbun add @unikvs/s3.bunnub add @unikvs/s3.bunaube add @unikvs/s3.bunS3-compatible object storage is required separately.
Usage
S3 implements IStorage. Pass it to UniKvs via persist().
import { S3 } from "@unikvs/s3.bun";
S3
Storage that saves to S3-compatible storage. Callers open it with open() and close it with close() when done.
The constructor arguments are as follows:
S3StorageOptions
Bun S3Options minus bucket. Specify the bucket name as the first constructor argument.
The following example saves a value and reads it back:
import { S3 } from "@unikvs/s3.bun";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{ file: unikvs.Value<Uint8Array> }>()
.persist(new S3("my-bucket"))
.build();
await kvs.open();
await kvs.set("file", new Uint8Array([0, 1, 2]));
const data = await kvs.get("file");
console.log(data); // Uint8Array(3) [0, 1, 2]
await kvs.close();
Data types
| Operation | Data type |
|---|---|
write / read |
Uint8Array<ArrayBuffer> |
getWritable / getReadable chunks |
Uint8Array<ArrayBuffer> |
write/readuse byte sequences (Uint8Array<ArrayBuffer>).- Supports streamed reading and writing with
Uint8Array<ArrayBuffer>chunks. - The write-stream part size can be changed with the runtime variable
@unikvs/s3.bun:partSize(or@unikvs/s3:partSize). clear()lists and deletes every object in the bucket.
Errors
This package throws the following errors.
StorageNotOpenError
Thrown when close() is called without opening.
Meaning
close() was called even though the S3 client is not open.
How to fix
Call open() before close().
InvalidPartSizeError
A shared @unikvs/core error. Thrown when the write-stream part size is not a positive integer.
actualunknown
The specified part size value.
unknownMeaning
The runtime-variable part size (@unikvs/s3.bun:partSize or @unikvs/s3:partSize) is not a positive integer. A shared @unikvs/core error.
How to fix
Specify a positive integer (in bytes) as the part size.
StorageAbortedError
A shared @unikvs/core error. Thrown when the signal is already aborted before acquiring a write stream.
keystring
The key that was to be uploaded.
stringMeaning
The AbortSignal is already aborted when getWritable() is called. A shared @unikvs/core error.
How to fix
Pass a signal that is not aborted, or check the signal state before requesting.
UnsupportedRuntimeError
A shared @unikvs/core error. Thrown when opening it on a runtime other than Bun.
Meaning
This storage only works on Bun. A shared @unikvs/core error.
How to fix
Run it on the Bun runtime.