FeaturesStorageOverview

Storage

Aerostack Storage provides file uploads and object storage backed by Cloudflare R2. Files are globally distributed via CDN.

Upload a file

From the browser (React)

import { useAerostack } from '@aerostack/react'
 
function AvatarUpload({ userId }) {
  const { sdk } = useAerostack()
 
  const handleFile = async (e) => {
    const file = e.target.files[0]
    const formData = new FormData()
    formData.append('file', file)
    formData.append('path', `avatars/${userId}`)
 
    const result = await fetch(`/api/storage/upload`, {
      method: 'POST',
      headers: { Authorization: `Bearer ${tokens.accessToken}` },
      body: formData,
    })
 
    const { url } = await result.json()
    // url is the CDN URL for the uploaded file
  }
 
  return <input type="file" accept="image/*" onChange={handleFile} />
}

From the server (Workers / Node.js)

import { sdk } from '@aerostack/sdk'
 
// Upload from a request
app.post('/upload', async (c) => {
  const formData = await c.req.formData()
  const file = formData.get('file') as File
 
  const result = await sdk.storage.upload({
    file,
    path: `uploads/${Date.now()}-${file.name}`,
    contentType: file.type,
  })
 
  return c.json({ url: result.url, key: result.key })
})

REST API

POST /api/v1/projects/{slug}/storage/upload
Content-Type: multipart/form-data
Authorization: Bearer {token}
 
# Form fields:
# file: (the file binary)
# path: optional custom path (default: uuid)

Response:

{
  "url": "https://cdn.aerostack.ai/projects/slug/uploads/file.jpg",
  "key": "projects/slug/uploads/file.jpg",
  "size": 204800,
  "contentType": "image/jpeg"
}

Delete a file

await sdk.storage.delete('projects/slug/uploads/file.jpg')

Supported file types

All file types are accepted. Maximum file size is 100 MB per upload.

Files are stored in Cloudflare R2 and served via Cloudflare’s CDN. Typical edge delivery latency is under 10ms worldwide.