# Blob

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

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

::::framework{name="Nuxt"}
```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    preset: '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({
      preset: '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({
      preset: '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)

Save text, bytes, or streams with `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 each file's size and type before it saves 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.

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

Send a big file in parts. The server joins them at the end.

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

`blob.sign()` makes a short-lived link 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 your 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. Enabling `serve` exposes served objects through a public route by default; set `serve.authorize: true` to require an authenticated session. Treat filesystem Blob storage as one-host storage.

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_R0001](https://vitehub.dev/raw/docs/blob/diagnostics/blob_r0001.md)

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