AgentSkillsCN

vercel-blob

通过Vercel Blob存储实现文件上传、下载与管理。当您需要构建文件存储系统、进行图片上传,或开展资产管理工作时,此工具将为您提供有力支持。

SKILL.md
--- frontmatter
name: vercel-blob
description: Vercel Blob storage integration for file uploads, downloads, and management. Use when implementing file storage, image uploads, or asset management.

Vercel Blob Integration

Vercel Blob is a serverless object storage solution for storing and serving files.

Guidelines

  • Use the @vercel/blob package for all blob operations.
  • The integration uses the BLOB_READ_WRITE_TOKEN environment variable (automatically configured in Vercel deployments).
  • Use put() for uploads, del() for deletions, and list() for listing files.
  • Set access: 'public' for publicly accessible files.
  • File URLs are permanent and globally distributed via Vercel's edge network.

Example Files

app/api/upload/route.ts

typescript
import { put } from '@vercel/blob'
import { type NextRequest, NextResponse } from 'next/server'

export async function POST(request: NextRequest) {
  try {
    const formData = await request.formData()
    const file = formData.get('file') as File

    if (!file) {
      return NextResponse.json({ error: 'No file provided' }, { status: 400 })
    }

    // Upload to Vercel Blob
    const blob = await put(file.name, file, {
      access: 'public',
    })

    return NextResponse.json({
      url: blob.url,
      filename: file.name,
      size: file.size,
      type: file.type,
    })
  } catch (error) {
    console.error('Upload error:', error)
    return NextResponse.json({ error: 'Upload failed' }, { status: 500 })
  }
}

app/api/delete/route.ts

typescript
import { del } from '@vercel/blob'
import { type NextRequest, NextResponse } from 'next/server'

export async function DELETE(request: NextRequest) {
  try {
    const { url } = await request.json()

    if (!url) {
      return NextResponse.json({ error: 'No URL provided' }, { status: 400 })
    }

    // Delete from Vercel Blob
    await del(url)

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Delete error:', error)
    return NextResponse.json({ error: 'Delete failed' }, { status: 500 })
  }
}

app/api/list/route.ts

typescript
import { list } from '@vercel/blob'
import { NextResponse } from 'next/server'

export async function GET() {
  try {
    const { blobs } = await list()

    const files = blobs.map((blob) => ({
      ...blob,
      filename: blob.pathname.split('/').pop() || 'unknown',
    }))

    return NextResponse.json({ files })
  } catch (error) {
    console.error('Error listing files:', error)
    return NextResponse.json({ error: 'Failed to list files' }, { status: 500 })
  }
}