Represents a file in an S3-compatible storage service. Extends the Blob interface for compatibility with web APIs.
Symbol
S3File
interface S3File
- readonly bucket?: string
The bucket name containing the file.
const file = s3.file("s3://my-bucket/file.txt"); console.log(file.bucket); // "my-bucket"
- readonly name?: string
The name or path of the file in the bucket.
const file = s3.file("folder/image.jpg"); console.log(file.name); // "folder/image.jpg"
- readonly readable: ReadableStream
Gets a readable stream of the file's content. Useful for processing large files without loading them entirely into memory.
// Basic streaming read const stream = file.stream(); for await (const chunk of stream) { console.log('Received chunk:', chunk); }
- unlink: () => Promise<void>
Alias for delete() method. Provided for compatibility with Node.js fs API naming.
await file.unlink();
Returns a promise that resolves to the contents of the blob as an ArrayBuffer
Returns a promise that resolves to the contents of the blob as a Uint8Array (array of bytes) its the same as
new Uint8Array(await blob.arrayBuffer())
Deletes the file from S3.
@returnsPromise that resolves when deletion is complete
// Basic deletion await file.delete();
Checks if the file exists in S3. Uses HTTP HEAD request to efficiently check existence without downloading.
@returnsPromise resolving to true if file exists, false otherwise
// Basic existence check if (await file.exists()) { console.log("File exists in S3"); }
Read the data from the blob as a FormData object.
This first decodes the data from UTF-8, then parses it as a
multipart/form-data
body or aapplication/x-www-form-urlencoded
body.The
type
property of the blob is used to determine the format of the body.This is a non-standard addition to the
Blob
API, to make it conform more closely to theBodyMixin
API.Read the data from the blob as a JSON object.
This first decodes the data from UTF-8, then parses it as JSON.
Generates a presigned URL for the file. Allows temporary access to the file without exposing credentials.
@param optionsConfiguration for the presigned URL
@returnsPresigned URL string
// Basic download URL const url = file.presign({ expiresIn: 3600 // 1 hour });
Creates a new S3File representing a slice of the original file. Uses HTTP Range headers for efficient partial downloads.
@param beginStarting byte offset
@param endEnding byte offset (exclusive)
@param contentTypeOptional MIME type for the slice
@returnsA new S3File representing the specified range
// Reading file header const header = file.slice(0, 1024); const headerText = await header.text();
Returns a promise that resolves to the contents of the blob as a string
- write(data: string | ArrayBuffer | SharedArrayBuffer | Request | Response | Blob | BunFile | ArrayBufferView<ArrayBufferLike> | S3File, options?: S3Options): Promise<number>
Uploads data to S3. Supports various input types and automatically handles large files.
@param dataThe data to upload
@param optionsUpload configuration options
@returnsPromise resolving to number of bytes written
// Writing string data await file.write("Hello World", { type: "text/plain" });
Creates a writable stream for uploading data. Suitable for large files as it uses multipart upload.
@param optionsConfiguration for the upload
@returnsA NetworkSink for writing data
// Basic streaming write const writer = file.writer({ type: "application/json" }); writer.write('{"hello": '); writer.write('"world"}'); await writer.end();