# Blob

Save uploads and other files from your server, then read or serve them on any host.

Hosts: Node, Docker, Cloudflare, Vercel, Netlify, Deno.

:hero-figure{name="blob"}

:::framework{name="Nuxt"}
```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'node',
    // On Node, files are saved in .vitehub/data/blob
    blob: true,
  },
})
```
:::
:::framework{name="Nitro"}
```ts [vite.config.ts]
import { nitro } from 'nitro/vite'
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      host: 'node',
      // On Node, files are saved in .vitehub/data/blob
      blob: true,
    }),
    nitro(),
  ],
})
```
:::
:::framework{name="Vite"}
```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      host: 'node',
      // On Node, files are saved in .vitehub/data/blob
      blob: true,
    }),
  ],
})
```
:::

```ts [server/api/avatars.post.ts]
import { defineEventHandler } from 'h3'
import { blob } from 'vite-hub/blob'

// Check who may upload before this call
export default defineEventHandler(async (event) => {
  const [error, files] = await blob.handleUpload(
    event,
    {
      formKey: 'files',
      ensure: { maxSize: '8MB', types: ['image'] },
      put: { prefix: 'avatars', addRandomSuffix: true },
    },
  )
  if (error) throw error
  return files
})
```

```vue [app/pages/upload.vue]
<script setup lang="ts">
import { useUpload } from 'vite-hub/blob/vue'

// Send one file to the upload route
const upload = useUpload('/api/avatars', {
  multiple: false,
})

async function onChange(event: Event) {
  const input = event.target as HTMLInputElement
  const file = await upload(input)
  console.log(file?.pathname)
}
</script>

<template>
  <input
    type="file"
    accept="image/*"
    @change="onChange"
  >
</template>
```

```ts [server/api/notes.post.ts]
import { defineEventHandler } from 'h3'
import { blob } from 'vite-hub/blob'

// Save text as a file and return its details
export default defineEventHandler(async () => {
  const [error, file] = await blob.put(
    'notes/hello.txt',
    'Hello from ViteHub',
    { contentType: 'text/plain' },
  )
  if (error) throw error
  return file
})
```

### [Save any file](https://vitehub.dev/docs/blob/server-api#use-it-at-runtime)

Pass text, bytes, or a stream to `blob.put()` with a content type and your own metadata.

### [Uploads in one call](https://vitehub.dev/docs/blob/server-api#upload-files)

`blob.handleUpload()` checks file sizes and types before saving anything.

### [Upload from a Vue page](https://vitehub.dev/docs/blob/server-api#upload-files)

`useUpload()` sends the files from an input to your upload route.

### [Uploads straight to storage](https://vitehub.dev/docs/blob/server-api#direct-uploads)

Set `direct: true` so the browser sends the file to R2, S3, or Vercel Blob, past the host's request size limit.

### [Large files in parts](https://vitehub.dev/docs/blob/server-api#multipart-uploads)

Upload a large file in parts. The server joins them at the end.

### [Copy a file](https://vitehub.dev/docs/blob/server-api#copy-a-file)

`blob.copy()` copies a file with its content type and metadata on the server where the provider supports it.

### [Links that expire](https://vitehub.dev/docs/blob/server-api#signed-requests)

`blob.sign()` creates a short-lived URL to read or upload a private file.

### [Images with alt text, crops, and variants](https://vitehub.dev/docs/blob/media)

Media records, folders, `?w=640` variants, and `<picture>` art direction on every driver.

### [Many storage services](https://vitehub.dev/docs/blob/configure#provider-options)

Use local disk, Cloudflare R2, Vercel Blob, Netlify Blobs, S3, and more, with the same routes.

Use Blob for binary objects such as uploads, generated files, images, and exports. Turn Blob on with `vitehub add blob`, and pass `--option blob.driver=<driver>` where the host needs an explicit driver. Use the same Blob handle in application code when moving between local and hosted storage.

Keep object keys and authorization in application code. `serve` needs `access: "public"`, which sends every object of the served store to anyone, or `authorize: true`, which requires an authenticated session. `put()` has no per-object `access`. Treat filesystem Blob storage as one-host storage.

Accept uploads with `blob.handleUpload(event, { ensure })` and authorize the request in the route first. For files larger than the host's request body limit, such as 4.5 MB on Vercel, pass `direct: true` to `handleUpload()` and to `useUpload()`, so the browser sends the file straight to storage. Direct uploads need `VITEHUB_SECRET`, and on R2, S3, GCS, or Azure a bucket CORS rule that allows `PUT` from the app origin; Netlify Blobs falls back to a form upload. Copy a file with `blob.copy(from, to)`; it returns `BLOB_CONFLICT` when `to` exists, unless you pass `{ overwrite: true }`. To move a file, delete the source after the copy.

Prove upload, read, list, and delete with the selected driver. For hosted output, inspect the generated binding and verify access rules with a real request.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/blob.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/blob/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/blob/configure.md)
- [Server API](https://vitehub.dev/raw/docs/blob/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/blob/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/blob/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/blob/limits-and-errors.md)
- [Media and images](https://vitehub.dev/raw/docs/blob/media.md)

Next: [BLOB_UNKNOWN_STORE_OPTION](https://vitehub.dev/raw/docs/blob/diagnostics/blob_unknown_store_option.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
