Vercel Blob
Last updated:
Vercel Blob Storage
Overview
Vercel Blob Storage is a serverless object storage service optimized for edge deployments. It provides fast, global access to files with automatic CDN distribution.
Installation
npm install @vercel/blobyarn add @vercel/blobpnpm add @vercel/blobUsage
import { VercelBlobStorage } from "@visulima/storage/provider/vercel";
const storage = new VercelBlobStorage({
token: process.env.BLOB_READ_WRITE_TOKEN,
metaStorageConfig: { directory: "/tmp/upload-metafiles" },
});Configuration
Environment Variables
// Set BLOB_READ_WRITE_TOKEN or VERCEL_BLOB_TOKEN
process.env.BLOB_READ_WRITE_TOKEN = "vercel_blob_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const storage = new VercelBlobStorage({
// Token is automatically read from environment
});With Token
const storage = new VercelBlobStorage({
token: "vercel_blob_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
});Advanced Configuration
const storage = new VercelBlobStorage({
token: process.env.BLOB_READ_WRITE_TOKEN,
multipart: true, // Enable multipart uploads for large files
multipart: 10 * 1024 * 1024, // Enable multipart for files > 10MB
retryConfig: {
maxRetries: 3,
initialDelay: 1000,
},
expiration: {
maxAge: "7d",
purgeInterval: "1h",
},
});Features
- Global CDN: Automatic CDN distribution for fast access
- Serverless: Optimized for edge deployments
- Multipart Uploads: Support for large files
- Automatic Retry: Built-in retry mechanism for transient failures
Basic Operations
// Create a file
const file = await storage.create({
contentType: "image/jpeg",
metadata: { filename: "photo.jpg" },
});
// Write data
await storage.write({
id: file.id,
body: imageStream,
contentLength: imageSize,
});
// Get file
const fileData = await storage.get({ id: file.id });
// Delete file
await storage.delete({ id: file.id });URL Generation
Vercel Blob automatically generates public URLs:
const file = await storage.get({ id: "file-id" });
// file.url - Public URL
// file.downloadUrl - Download URL
// file.pathname - File pathnameBest Practices
- Use environment variables - Store tokens securely
- Enable multipart for large files - Better performance for files > 10MB
- Configure retry - Handle transient failures
- Use CDN URLs - Leverage Vercel's global CDN
- Monitor usage - Track storage and bandwidth usage