Enable blob storage in your project by setting blob: true in the NuxtHub config.
export default defineNuxtConfig({
hub: {
blob: true
}
})
NuxtHub automatically configures the blob storage driver based on your hosting provider when blob: true is set in the NuxtHub config.
When deploying to Vercel, it automatically configures Vercel Blob Storage.
@vercel/blob packagepnpm add @vercel/blob
yarn add @vercel/blob
npm install @vercel/blob
bun add @vercel/blob
deno add npm:@vercel/blob
npx nypm add @vercel/blob
BLOB_READ_WRITE_TOKEN environment variable to enable the Vercel Blob driver:BLOB_READ_WRITE_TOKEN=your-token
When deploying to Cloudflare, it automatically configures Cloudflare R2.
Add a BLOB binding to a Cloudflare R2 bucket in your wrangler.jsonc config.
{
"$schema": "node_modules/wrangler/config-schema.json",
// ...
"r2_buckets": [
{
"binding": "BLOB",
"bucket_name": "<bucket_name>"
}
]
}
Learn more about adding bindings on Cloudflare's documentation.
To configure Amazon S3 as a blob storage driver.
aws4fetch packagepnpm add aws4fetch
yarn add aws4fetch
npm install aws4fetch
bun add aws4fetch
deno add npm:aws4fetch
npx nypm add aws4fetch
S3_ACCESS_KEY_ID=your-access-key-id
S3_SECRET_ACCESS_KEY=your-secret-access-key
S3_BUCKET=your-bucket-name
S3_REGION=your-region
S3_ENDPOINT=your-endpoint # (optional)
To customize the directory, you can set the dir option.
export default defineNuxtConfig({
hub: {
blob: {
driver: 'fs',
dir: '.data/my-blob-directory' // Defaults to `.data/blob`
}
}
})
You can set a custom driver by providing a configuration object to blob.
export default defineNuxtConfig({
hub: {
blob: {
driver: 'fs',
dir: '.data/blob'
}
},
// or overwrite only in production
$production: {
hub: {
blob: {
driver: 'cloudflare-r2',
binding: 'BLOB'
}
}
}
})
ensureBlob()ensureBlob() is a handy util to validate a Blob by checking its size and type:
import { ensureBlob } from 'hub:blob'
// Will throw an error if the file is not an image or is larger than 1MB
ensureBlob(file, { maxSize: '1MB', types: ['image']})
maxSize or types should be provided.1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024) + (B | KB | MB | GB) '512KB', '1MB', '2GB', etc.['image/jpeg'].Returns nothing.
Throws an error if file doesn't meet the requirements.
server/ directory).useUpload()useUpload is to handle file uploads in your Nuxt application.
<script setup lang="ts">
const upload = useUpload('/api/blob', { method: 'PUT' })
async function onFileSelect({ target }: Event) {
const uploadedFiles = await upload(target as HTMLInputElement)
// file uploaded successfully
}
</script>
<template>
<input
accept="image/jpeg, image/png"
type="file"
name="file"
multiple
@change="onFileSelect"
>
</template>
'files'.true.Return a MultipartUpload function that can be used to upload a file in parts.
const { completed, progress, abort } = upload(file)
const data = await completed
useMultipartUpload()Application composable that creates a multipart upload helper.
export const mpu = useMultipartUpload('/api/files/multipart')
handleMultipartUpload().10MB.1.3.query and headers will be merged with the options provided by the uploader.Return a MultipartUpload function that can be used to upload a file in parts.
const { completed, progress, abort } = mpu(file)
const data = await completed
BlobObjectinterface BlobObject {
pathname: string
contentType: string | undefined
size: number
httpEtag: string
uploadedAt: Date
httpMetadata: Record<string, string>
customMetadata: Record<string, string>
url: string | undefined
}
BlobMultipartUploadexport interface BlobMultipartUpload {
pathname: string
uploadId: string
uploadPart(
partNumber: number,
value: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView | Blob
): Promise<BlobUploadedPart>
abort(): Promise<void>
complete(uploadedParts: BlobUploadedPart[]): Promise<BlobObject>
}
BlobUploadedPartexport interface BlobUploadedPart {
partNumber: number;
etag: string;
}
MultipartUploaderexport type MultipartUploader = (file: File) => {
completed: Promise<SerializeObject<BlobObject> | undefined>
progress: Readonly<Ref<number>>
abort: () => Promise<void>
}
BlobListResultinterface BlobListResult {
blobs: BlobObject[]
hasMore: boolean
cursor?: string
folders?: string[]
}