fastify-xstorage
download
Download a file from S3-compatible storage as a Node.js Buffer.
download
Download a file from the bucket and return its contents as a Buffer. Use for server-side file processing. For large files or browser downloads, prefer getSignedUrl to redirect the client directly.
Signature
fastify.xStorage.download(key: string): Promise<Buffer>
Params
| Name | Type | Required | Description |
|---|---|---|---|
key | string | Yes | The storage key of the file to download. |
Returns
A Buffer containing the full file content.
Throws
[xStorage] key is required and must be a string— ifkeyis missing or not a string.[xStorage] Failed to download file "<key>": <message>— if the S3 GET fails (e.g., key not found, permissions error).
Examples
Basic — download and return file content
const buffer = await fastify.xStorage.download("reports/monthly-2026-01.pdf");
return reply
.header("Content-Type", "application/pdf")
.header("Content-Disposition", 'attachment; filename="report.pdf"')
.send(buffer);
Download and process in memory
const csvBuffer = await fastify.xStorage.download("exports/users.csv");
const rows = parse(csvBuffer.toString("utf8"), { columns: true });
console.log(`${rows.length} rows in CSV`);
Realistic — proxy file download with auth check
fastify.get("/files/:key", { preHandler: [fastify.authenticate] }, async (request, reply) => {
const key = decodeURIComponent(request.params.key);
// Verify the user owns this file
const file = await db.files.findByKey(key);
if (!file || file.userId !== request.user.id) {
return reply.code(404).send({ error: "File not found" });
}
const buffer = await fastify.xStorage.download(key);
return reply
.header("Content-Type", file.contentType)
.header("Content-Disposition", `attachment; filename="${file.originalName}"`)
.send(buffer);
});
See also
- getSignedUrl — generate a temporary pre-signed URL for direct browser downloads (avoids buffering through your server)
- exists — check if a file exists before downloading
- upload / uploadMultiple — upload files
AI Context
package: "@xenterprises/fastify-xstorage"
method: fastify.xStorage.download(key)
use-when: Download a file from storage into a server-side Buffer — for large/browser downloads use getSignedUrl to redirect client directly
returns: Promise<Buffer>
