StorageStorage ServicesNetlify Blob

Netlify Blob

Last updated:

Netlify Blob Storage

Overview

Netlify Blob Storage is a serverless object storage service for Netlify deployments. It provides persistent storage for edge functions and serverless applications.

Installation

npm install @netlify/blobs
yarn add @netlify/blobs
pnpm add @netlify/blobs

Usage

import { NetlifyBlobStorage } from "@visulima/storage/provider/netlify";

const storage = new NetlifyBlobStorage({
    storeName: "uploads",
    siteID: process.env.NETLIFY_SITE_ID,
    token: process.env.NETLIFY_TOKEN,
    metaStorageConfig: { directory: "/tmp/upload-metafiles" },
});

Configuration

Environment Variables

// Set NETLIFY_SITE_ID and NETLIFY_TOKEN
process.env.NETLIFY_SITE_ID = "your-site-id";
process.env.NETLIFY_TOKEN = "your-token";

const storage = new NetlifyBlobStorage({
    storeName: "uploads",
    // Site ID and token are automatically read from environment
});

With Credentials

const storage = new NetlifyBlobStorage({
    storeName: "uploads",
    siteID: "your-site-id",
    token: "your-token",
});

Advanced Configuration

const storage = new NetlifyBlobStorage({
    storeName: "uploads",
    siteID: process.env.NETLIFY_SITE_ID,
    token: process.env.NETLIFY_TOKEN,
    retryConfig: {
        maxRetries: 3,
        initialDelay: 1000,
    },
    expiration: {
        maxAge: "7d",
        purgeInterval: "1h",
    },
});

Features

  • Serverless: Optimized for Netlify Edge Functions
  • Persistent Storage: Reliable storage for serverless applications
  • Automatic Retry: Built-in retry mechanism for transient failures
  • Metadata Support: Rich metadata for file organization

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

Netlify Blob generates URLs based on your deployment:

const file = await storage.get({ id: "file-id" });

// file.url - Generated URL (typically via Netlify Function)
// file.pathname - File pathname

Netlify Edge Functions

Use with Netlify Edge Functions:

import { NetlifyBlobStorage } from "@visulima/storage/provider/netlify";

export default async (request: Request, context: any) => {
    const storage = new NetlifyBlobStorage({
        storeName: "uploads",
        // Credentials are automatically available in Netlify context
    });

    // Use storage...
    return new Response("OK");
};

Best Practices

  1. Use environment variables - Store credentials securely
  2. Configure retry - Handle transient failures
  3. Use appropriate store names - Organize files by store
  4. Monitor usage - Track storage usage in Netlify dashboard
  5. Handle errors gracefully - Netlify Blob may have rate limits
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues