# Upload files from the browser with presigned URLs

Source: https://resilis.io/docs/object-storage/presigned-uploads/

A presigned URL lets a browser upload one object without holding your bucket credential. Your server signs the URL, the browser sends the file to it, and your database stores the object key.

## Why upload directly to storage

For larger files, upload directly to object storage instead of sending the file through your app. The file never occupies your app’s memory or request time, and presigned uploads and S3 API calls go straight to the storage endpoint, so they are not limited by the app firewall.

The browser never sees your access key. It receives a URL that works for one object, one method and a short time.

- [App firewall: request body limit](/docs/firewall/)

## Sign an upload on your server

This is an example with @aws-sdk/s3-request-presigner and the client from the S3 API page.

### Example: sign an upload on your server

```ts
import { PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { s3 } from './s3';

export async function signUpload(key: string, contentType: string) {
  const command = new PutObjectCommand({
    Bucket: 'uploads',
    Key: key,
    ContentType: contentType,
  });
  return getSignedUrl(s3, command, { expiresIn: 900 });
}
```

- [Set up the S3 client](/docs/object-storage/s3-api/#set-up-an-s3-client)

## Upload from the browser

The browser sends the file with a PUT to the signed URL. Send the same Content-Type you signed. Then send the object key to your server so it can store it in your database.

### Example: upload from the browser

```ts
await fetch(signedUrl, {
  method: 'PUT',
  headers: { 'Content-Type': file.type },
  body: file,
});
```

## Lifetime, content type and browser access

A presigned URL can last from one second up to seven days. Keep the lifetime short: the example above uses 900 seconds, which is long enough to start an upload and short enough to limit a leaked URL.

Sign the content type you expect, so the browser must send the same one. The storage endpoint accepts cross-origin requests from browsers, answers the preflight request, and lets browser code read the ETag response header, which is what a multipart upload needs to complete its parts. The endpoint authorizes each request by its signature, not by cookies.

## How do I upload large files without passing them through my app?

Sign a URL on your server and let the client upload straight to storage. The file never passes through your app, and presigned uploads are not limited by the app firewall, which only sees requests sent to your app.

Presigned URLs last from one second up to seven days, so choose a short lifetime. The example above uses 900 seconds.

- [Sign an upload on your server](#sign-an-upload)
- [App firewall](/docs/firewall/)
