Nuxt Blob Storage
Getting Started
Enable blob storage
Enable blob storage in your project by setting blob: true in the NuxtHub config.
export default defineNuxtConfig({
hub: {
blob: true
}
})
Set a driver
NuxtHub automatically configures the blob storage driver based on your environment variables or hosting provider.
.data/blob directory.To set the storage directory, you can set the dir option.
export default defineNuxtConfig({
hub: {
blob: {
driver: 'fs',
dir: '.data/my-blob-directory' // Defaults to `.data/blob`
}
}
})
To configure Amazon S3 as a blob storage driver.
- Install the
aws4fetchpackage
pnpm add aws4fetch
yarn add aws4fetch
npm install aws4fetch
bun add aws4fetch
deno add npm:aws4fetch
npx nypm add aws4fetch
- Set the following environment variables:
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)
When deploying to Vercel, it automatically configures Vercel Blob Storage.
- Install the
@vercel/blobpackage
pnpm 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
- Assign a Vercel Blob Store to your project from the Vercel dashboard -> Project -> Storage
- When running locally, you can set the
BLOB_READ_WRITE_TOKENenvironment variable to enable the Vercel Blob driver:
BLOB_READ_WRITE_TOKEN=your-token
When deploying to Cloudflare, configure Cloudflare R2 by providing the bucket name. NuxtHub auto-generates the wrangler bindings at build time.
export default defineNuxtConfig({
hub: {
blob: {
driver: 'cloudflare-r2',
bucketName: '<bucket-name>'
}
}
})
Upload a file
To upload a file, you can use the blob.put() method.
import { blob } from 'hub:blob'
const file = await blob.put('avatars/user-1.png', imageData)
Driver options
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: 'vercel-blob'
}
}
}
})
Available drivers:
export default defineNuxtConfig({
hub: {
blob: {
driver: 'fs',
dir: '.data/files' // defaults to '.data/blob'
}
}
})
export default defineNuxtConfig({
hub: {
blob: {
driver: 's3',
accessKeyId: 'your-access-key-id', // defaults to S3_ACCESS_KEY_ID
secretAccessKey: 'your-secret-access-key', // defaults to S3_SECRET_ACCESS_KEY
bucket: 'your-bucket-name', // defaults to S3_BUCKET
region: 'your-region', // defaults to S3_REGION or 'auto'
endpoint: 'your-endpoint' // optional, defaults to S3_ENDPOINT
}
}
})
export default defineNuxtConfig({
hub: {
blob: {
driver: 'vercel-blob',
token: 'your-token' // optional, defaults to BLOB_READ_WRITE_TOKEN env var
}
}
})
export default defineNuxtConfig({
hub: {
blob: {
driver: 'cloudflare-r2',
binding: 'BLOB' // optional, defaults to 'BLOB'
}
}
})
Nuxt Image Integration
@nuxt/image provides automatic image optimization. You can combine it with blob storage to serve optimized images from your storage.
Install @nuxt/image
npx nuxt module add image
@nuxt/image and add it to your modules section of your nuxt.config.ts.Expose your blobs on a route
Create a server route (for example /images/**) that serves blobs using blob.serve():
This route should exist in both development and production. In production, providers like Cloudflare and Vercel optimize by generating URLs that wrap this route.
import { blob } from 'hub:blob'
import { createError, eventHandler, getRouterParam } from 'h3'
export default eventHandler(async (event) => {
const pathname = getRouterParam(event, 'pathname')
if (!pathname) {
throw createError({ statusCode: 404, statusMessage: 'Not Found' })
}
return blob.serve(event, pathname)
})
Configure the image provider
Configure @nuxt/image to use the appropriate provider for your hosting platform:
/cdn-cgi/image/ for Cloudflare, /_vercel/image for Vercel), so enable them only in $production.In development, @nuxt/image defaults to the ipx provider if you don't configure it. IPX generates /_ipx/... URLs and tries to read the source from the filesystem, so it won't work with blob routes like /images/** (you may see IPX_FILE_NOT_FOUND errors). Use provider: 'none' in development to keep src="/images/..." working.Use NuxtImg or NuxtPicture
Use the components to display optimized images from blob storage:
<template>
<NuxtImg src="/images/photo.jpg" width="300" quality="80" />
</template>
The src path combines your route prefix (/images) with the blob pathname (photo.jpg).