Use the fastest syscalls available to copy from input
into destination
.
If destination
exists, it must be a regular file or symlink to a file. If destination
's directory does not exist, it will be created by default.
Symbol
Use the fastest syscalls available to copy from input
into destination
.
If destination
exists, it must be a regular file or symlink to a file. If destination
's directory does not exist, it will be created by default.
The file or file path to write to
The data to copy into destination
.
A promise that resolves with the number of bytes written.
Persist a Response body to disk.
The file to write to. If the file doesn't exist, it will be created and if the file does exist, it will be overwritten. If input
's size is less than destination
's size, destination
will be truncated.
Response
object
A promise that resolves with the number of bytes written.
Persist a Response body to disk.
The file path to write to. If the file doesn't exist, it will be created and if the file does exist, it will be overwritten. If input
's size is less than destination
's size, destination
will be truncated.
Response
object
A promise that resolves with the number of bytes written.
Use the fastest syscalls available to copy from input
into destination
.
If destination
exists, it must be a regular file or symlink to a file.
On Linux, this uses copy_file_range
.
On macOS, when the destination doesn't already exist, this uses clonefile()
and falls back to fcopyfile()
The file to write to. If the file doesn't exist, it will be created and if the file does exist, it will be overwritten. If input
's size is less than destination
's size, destination
will be truncated.
The file to copy from.
A promise that resolves with the number of bytes written.
Use the fastest syscalls available to copy from input
into destination
.
If destination
exists, it must be a regular file or symlink to a file.
On Linux, this uses copy_file_range
.
On macOS, when the destination doesn't already exist, this uses clonefile()
and falls back to fcopyfile()
The file path to write to. If the file doesn't exist, it will be created and if the file does exist, it will be overwritten. If input
's size is less than destination
's size, destination
will be truncated.
The file to copy from.
A promise that resolves with the number of bytes written.
Blob
powered by the fastest system calls available for operating on files.
This Blob is lazy. That means it won't do any work until you read from it.
size
will not be valid until the contents of the file are read at least once.type
is auto-set based on the file extension when possibleconst file = Bun.file("./hello.json");
console.log(file.type); // "application/json"
console.log(await file.text()); // '{"hello":"world"}'
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 (same as unlink)
Does the file exist?
This returns true for regular files and FIFOs. It returns false for directories. Note that a race condition can occur where the file is deleted or renamed after this is called but before you open it.
This does a system call to check if the file exists, which can be slow.
If using this in an HTTP server, it's faster to instead use return new Response(Bun.file(path))
and then an error
handler to handle exceptions.
Instead of checking for a file's existence and then performing the operation, it is faster to just perform the operation and handle the error.
For empty Blob, this always returns true.
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 a application/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 the BodyMixin
API.
Read the data from the blob as a JSON object.
This first decodes the data from UTF-8, then parses it as JSON.
Offset any operation on the file starting at begin
and ending at end
. end
is relative to 0
Similar to TypedArray.subarray
. Does not copy the file, open the file, or modify the file.
If begin
> 0, () will be slower on macOS
start offset in bytes
absolute offset in bytes (relative to 0)
MIME type for the new BunFile
Offset any operation on the file starting at begin
Similar to TypedArray.subarray
. Does not copy the file, open the file, or modify the file.
If begin
> 0, Bun.write() will be slower on macOS
start offset in bytes
MIME type for the new BunFile
Provides useful information about the file.
Returns a readable stream of the blob's contents
Returns a promise that resolves to the contents of the blob as a string
Deletes the file.
Write data to the file. This is equivalent to using Bun.write with a BunFile.
The data to write.
The options to use for the write.
Represents a file in an S3-compatible storage service. Extends the Blob interface for compatibility with web APIs.
The bucket name containing the file.
const file = s3.file("s3://my-bucket/file.txt");
console.log(file.bucket); // "my-bucket"
The name or path of the file in the bucket.
const file = s3.file("folder/image.jpg");
console.log(file.name); // "folder/image.jpg"
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);
}
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.
Promise 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.
Promise 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 a application/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 the BodyMixin
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.
Configuration for the presigned URL
Presigned 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.
Starting byte offset
Ending byte offset (exclusive)
Optional MIME type for the slice
A 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
Uploads data to S3. Supports various input types and automatically handles large files.
The data to upload
Upload configuration options
Promise 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.
Configuration for the upload
A NetworkSink for writing data
// Basic streaming write
const writer = file.writer({
type: "application/json"
});
writer.write('{"hello": ');
writer.write('"world"}');
await writer.end();
A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system.
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())
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 a application/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 the BodyMixin
API.
Read the data from the blob as a JSON object.
This first decodes the data from UTF-8, then parses it as JSON.
Returns a readable stream of the blob's contents
Returns a promise that resolves to the contents of the blob as a string
This Fetch API interface represents the response to a request.