Skip to content

ClayWarren/vercel_blob

 
 

Repository files navigation

Go Vercel Blob Client

A lightweight, idiomatic Go client for the Vercel Blob Storage API. Inspired by the official TypeScript SDK, it provides a simple way to interact with your Vercel Blob store from Go applications.

Installation

go get github.com/claywarren/vercel_blob

Quick Start

Within a Vercel Function

If your code is running in a Vercel Serverless or Edge function, authentication is handled automatically via the BLOB_READ_WRITE_TOKEN environment variable.

import "github.com/claywarren/vercel_blob"

func main() {
    client := vercelblob.NewClient()
    
    // Use the client...
}

Outside of Vercel (Client-side / External)

For external applications, you should use a TokenProvider to securely fetch short-lived tokens from your backend.

import "github.com/claywarren/vercel_blob"

func main() {
    // Custom provider that fetches a token from your API
    provider := &MyTokenProvider{}
    client := vercelblob.NewClientExternal(provider)
    
    // Use the client...
}

Operations

List Blobs

options := vercelblob.ListCommandOptions{
    Limit: 10,
    Prefix: "images/",
}

result, err := client.List(options)
if err != nil {
    log.Fatal(err)
}

for _, blob := range result.Blobs {
    fmt.Printf("Found blob: %s (%d bytes)\n", blob.PathName, blob.Size)
}

Upload a Blob (Put)

file, _ := os.Open("photo.jpg")
defile.Close()

options := vercelblob.PutCommandOptions{
    AddRandomSuffix: true,
    ContentType:     "image/jpeg",
}

result, err := client.Put("uploads/photo.jpg", file, options)
if err != nil {
    log.Fatal(err)
}

fmt.Printf("Uploaded to: %s\n", result.URL)

Copy a Blob

result, err := client.Copy(
    "https://your-store.public.blob.vercel-storage.com/old.txt",
    "new-destination.txt",
    vercelblob.PutCommandOptions{},
)

Download a Blob

// Download entire file
data, err := client.Download("uploads/photo.jpg", vercelblob.DownloadCommandOptions{})

// Download a specific range
rangeOptions := vercelblob.DownloadCommandOptions{
    ByteRange: &vercelblob.Range{Start: 0, End: 1024},
}
partialData, err := client.Download("uploads/large-file.bin", rangeOptions)

Delete a Blob

err := client.Delete("https://your-store.public.blob.vercel-storage.com/file-to-delete.txt")

Environment Variables

Variable Description
BLOB_READ_WRITE_TOKEN Your Vercel Blob read/write token (required if no provider used).
VERCEL_BLOB_API_URL Override the default API endpoint. Only HTTPS Vercel Blob hosts are accepted.
VERCEL_BLOB_API_VERSION Override the default API version (default: 9).

License

MIT

About

Go golang client for the vercel blob API

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages

  • Go 99.3%
  • Makefile 0.7%