In this guide, you'll learn how to do the following:
Vercel Blob works with any frontend framework. First, install the package:
Navigate to the Project you'd like to add the blob store to. Select the Storage tab, then select the Connect Database button.
Under the Create New tab, select Blob and then the Continue button.
Use the name "Images" and select Create a new Blob store. Select the environments where you would like the read-write token to be included. You can also update the prefix of the Environment Variable in Advanced Options
Once created, you are taken to the Vercel Blob store page.
Since you created the Blob store in a project, we automatically created and added the following Environment Variable to the project for you.
BLOB_READ_WRITE_TOKEN
To use this Environment Variable locally, we recommend pulling it with the Vercel CLI:
Server uploads are perfectly fine as long as you do not need to upload files larger than 4.5 MB on Vercel. If you need to upload larger files, consider using client uploads.
The following example shows how to use a Server Action with Next.js App Router to upload a file to Vercel Blob.
import { put } from '@vercel/blob';
import { revalidatePath } from 'next/cache';
export async function Form() {
async function uploadImage(formData: FormData) {
'use server';
const imageFile = formData.get('image') as File;
const blob = await put(imageFile.name, imageFile, {
access: 'public',
addRandomSuffix: true,
});
revalidatePath('/');
return blob;
}
return (
<form action={uploadImage}>
<label htmlFor="image">Image</label>
<input
type="file"
id="image"
name="image"
accept="image/jpeg, image/png, image/webp"
required
/>
<button>Upload</button>
</form>
);
}
Then, add the hostname to your next.config.js
file including the store id from the dashboard:
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns: [
new URL('https://my-store-id.public.blob.vercel-storage.com/**'),
],
},
};
module.exports = nextConfig;
This will allow you to use next/image
to display images from your Vercel Blob store.
import { list } from '@vercel/blob';
import Image from 'next/image';
export async function Images() {
const { blobs } = await list();
return (
<section>
{blobs.map((image, i) => (
<Image
priority={i < 2}
key={image.pathname}
src={image.url}
alt="My Image"
width={200}
height={200}
/>
))}
</section>
);
}
Read more about Server Actions and App Router on the Next.js documentation.
You can upload files to Vercel Blob using Route Handlers/API Routes. The following example shows how to upload a file to Vercel Blob using a server upload page and route.
This page will upload files to your server. The files will then be sent to Vercel Blob.
'use client';
import type { PutBlobResult } from '@vercel/blob';
import { useState, useRef } from 'react';
export default function AvatarUploadPage() {
const inputFileRef = useRef<HTMLInputElement>(null);
const [blob, setBlob] = useState<PutBlobResult | null>(null);
return (
<>
<h1>Upload Your Avatar</h1>
<form
onSubmit={async (event) => {
event.preventDefault();
if (!inputFileRef.current?.files) {
throw new Error('No file selected');
}
const file = inputFileRef.current.files[0];
const response = await fetch(
`/api/avatar/upload?filename=${file.name}`,
{
method: 'POST',
body: file,
},
);
const newBlob = (await response.json()) as PutBlobResult;
setBlob(newBlob);
}}
>
<input
name="file"
ref={inputFileRef}
type="file"
accept="image/jpeg, image/png, image/webp"
required
/>
<button type="submit">Upload</button>
</form>
{blob && (
<div>
Blob url: <a href={blob.url}>{blob.url}</a>
</div>
)}
</>
);
}
This route forwards the file to Vercel Blob and returns the URL of the uploaded file to the browser.
import { put } from '@vercel/blob';
import { NextResponse } from 'next/server';
export async function POST(request: Request): Promise<NextResponse> {
const { searchParams } = new URL(request.url);
const filename = searchParams.get('filename');
const blob = await put(filename, request.body, {
access: 'public',
addRandomSuffix: true,
});
return NextResponse.json(blob);
}
Run your application locally and visit /avatar/upload
to upload the file to your store. The browser will display the unique URL created for the file.
When your local website is served on http://localhost:3000
, then the onUploadCompleted
step won't succeed as Vercel Blob cannot contact your localhost. Instead, we recommend you run your local application through a tunneling service like ngrok, so you can experience the full Vercel Blob development flow locally.
You have successfully uploaded an object to your Vercel Blob store and are able to review it's metadata, download, and delete it from your Vercel Storage Dashboard.
@vercel/blob
packageRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4