> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dreep.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Use Dreep with your R2 bucket

> Keep your images in R2 and resize them with Dreep.

Your images stay in your R2 bucket. Dreep resizes them when they're requested.

> [!NOTE]
>
> Needs the **Pro** plan.

## 1. Create an R2 key

1. In Cloudflare, open **R2 Object Storage** and click **Manage API Tokens**.

![R2 Object Storage overview with the Manage API Tokens button](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/r2-overview.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=5b1706553e8183da82290e4b61d98118)

2. Click **Create Account API token**.

![R2 API Tokens page with the Create Account API token button](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/r2-api-tokens.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=51ac8c5d6616774100518c3d42820c15)

3. Choose **Object Read & Write**, then **Apply to specific buckets only** and pick your bucket.

![Creating a token with Object Read & Write on one bucket](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/r2-create-token.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=9d50beef12715fc2a681af62bc272884)

4. Create the token, then copy the **Access Key ID**, **Secret Access Key** and the **S3 endpoint**. The secret is only shown once.

## 2. Connect the bucket

1. In Dreep, open your project, go to **Settings** > **Storage** and click **Configure Bucket**.

![Dreep storage settings with the Configure Bucket button](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/dreep-storage-settings.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=031c5886ed3440f3755f92c8a76ad863)

2. Choose **Cloudflare R2** and click **Continue**.

![Choosing Cloudflare R2 as the provider](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/dreep-choose-r2.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=db2a2df264e613c3cc7c78f3ec7beeb8)

3. Paste the bucket name, keys and S3 endpoint. Leave the region as `auto`.

![Bucket name, keys and endpoint form](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/dreep-r2-credentials.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=2d6db2f6032207774319bd7478a7cdf1)

4. Click **Review & Test**, then connect the bucket. When it's done, you'll see **Connected**.

![Connected bucket with the setup checklist complete](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/dreep-bucket-connected.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=97b009a0e11e41ff9833a104679858c9)

## 3. Use your images

Copy the **Images URL** from your bucket details, then add a file's path from your bucket:

```text theme={null}
https://cdn.dreep.cloud/<your-project>/products/123/photo.jpg
```

Add options to resize or convert it:

```text theme={null}
https://cdn.dreep.cloud/<your-project>/products/123/photo.jpg?width=400&format=webp
```

## Options

| Option | Example |
| :- | :- |
| Width | `?width=400` |
| Height | `?height=300` |
| Format | `?format=webp` (or `avif`, `jpeg`, `png`) |
| Quality | `?quality=75` |
| Crop to fill | `?width=400&height=400&fit=cover` |
| Preset | `?p=thumbnail` |

See [Image Transformations](https://docs.dreep.cloud/transformations/images.md) for everything else.

## Move your image URLs

Your Cloudflare Images links work on Dreep with the same options and the same file paths. Only the start of the URL changes.

**On Dreep's domain**, swap the domain for your Images URL:

```text theme={null}
Before:  https://images.example.com/cdn-cgi/image/width=400,format=webp/products/123/photo.jpg
After:   https://cdn.dreep.cloud/<your-project>/cdn-cgi/image/width=400,format=webp/products/123/photo.jpg
```

**On your own domain** ([see below](#optional-your-own-domain)), keep the domain and swap `/cdn-cgi/image/` for `/_img/`. Cloudflare reserves `/cdn-cgi/` on every domain it serves, so that path never reaches Dreep:

```text theme={null}
Before:  https://images.example.com/cdn-cgi/image/width=400,format=webp/products/123/photo.jpg
After:   https://images.example.com/_img/width=400,format=webp/products/123/photo.jpg
```

Links without options, like `https://images.example.com/products/123/photo.jpg`, need no change on your own domain.

### In your code

If your app builds image URLs, change them in one place. The JavaScript SDK has [`toDreepUrl()`](https://docs.dreep.cloud/node-sdk/media/to-dreep-url) built in:

```js JavaScript theme={null}
import { toDreepUrl } from "dreep";

const src = toDreepUrl(url, {
  from: "https://images.example.com",
  to: "https://cdn.dreep.cloud/your-project", // or your own domain
});
```

```python Python theme={null}
OLD_BASE = "https://images.example.com"
NEW_BASE = "https://cdn.dreep.cloud/your-project"  # or your own domain

def to_dreep_url(url: str) -> str:
    return url.replace(OLD_BASE, NEW_BASE, 1).replace("/cdn-cgi/image/", "/_img/", 1)
```

```php PHP theme={null}
const OLD_BASE = 'https://images.example.com';
const NEW_BASE = 'https://cdn.dreep.cloud/your-project'; // or your own domain

function toDreepUrl(string $url): string {
    return str_replace([OLD_BASE, '/cdn-cgi/image/'], [NEW_BASE, '/_img/'], $url);
}
```

```go Go theme={null}
const (
	oldBase = "https://images.example.com"
	newBase = "https://cdn.dreep.cloud/your-project" // or your own domain
)

func ToDreepURL(url string) string {
	url = strings.Replace(url, oldBase, newBase, 1)
	return strings.Replace(url, "/cdn-cgi/image/", "/_img/", 1)
}
```

### In your database

If you store full image URLs, update them in place. Back up the table first.

```sql PostgreSQL / MySQL theme={null}
UPDATE images
SET url = REPLACE(
  REPLACE(url, 'https://images.example.com', 'https://cdn.dreep.cloud/your-project'),
  '/cdn-cgi/image/', '/_img/'
);
```

If you're keeping your own domain, skip the first `REPLACE` and only swap `/cdn-cgi/image/` for `/_img/`. The same goes for links on R2's development URL (`https://pub-….r2.dev`): put that as the old base.

## Optional: your own domain

1. If the domain is connected to your R2 bucket, remove it there first: your bucket > **Settings** > **Custom Domains**.
2. In Dreep, go to **Settings** > **Custom Domains** and add the domain, such as `images.example.com`.
3. Add the DNS record Dreep shows you.

Your images are then at:

```text theme={null}
https://images.example.com/products/123/photo.jpg?width=400
```

## Optional: CORS for browser uploads

Only needed if your website uploads files from the browser straight to your bucket. Serving and resizing images works without it.

1. In Cloudflare, open your bucket and go to **Settings**. Under **CORS Policy**, click **Add**.

![R2 bucket settings with the CORS Policy section](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/r2-bucket-settings.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=6e211d59f28d0a8f7da2630a5db869bb)

2. Paste this policy, with your website's address in `AllowedOrigins`, then click **Save**.

```json theme={null}
[
  {
    "AllowedOrigins": ["https://your-website.com"],
    "AllowedMethods": ["PUT", "GET", "HEAD"],
    "AllowedHeaders": ["*"],
    "ExposeHeaders": ["ETag"],
    "MaxAgeSeconds": 3000
  }
]
```

![R2 CORS policy editor with the policy pasted in](https://mintcdn.com/dreep/tuOu7EXXbw6fqYyc/images/r2/r2-cors-policy.png?fit=max\&auto=format\&n=tuOu7EXXbw6fqYyc\&q=85\&s=e70b141a12c00d6e2431c8d9626ec052)

3. Back in Dreep, click **Verify Again** in the setup checklist. **S3 CORS Configuration** shows **Configured**.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.