External Storage - Cloudflare R2

External Storage / Cloudflare R2

To use Cloudflare R2 as your External Storage of choice, you must set the provider attribute to cloudflare within the store hash and, at a minimum, provide your Cloudflare R2 credentials (key and secret) as well as your bucket name. The full list of store hash attibutes for Cloudflare R2 is presented below.

Alternatively, you can use our Secure Storage Connectors. Simply save your credentials in your Optidash Account and reference them by ID. This mechanism significantly enhances the security of your cloud credentials. When Secure Storage Connectors are in use, you only need to provide the Optidash API with your Connector id instead of provider, key, and secret properties. You can add a new Connector in your Optidash Account.

Authentication

When passing Cloudflare R2 credentials in your request JSON, you have to set the following authentication properties:

Attribute Type Description
provider String provider must be set to cloudflare
key String Cloudflare R2 Key Id
secret String Cloudflare R2 Secret Access Key
{
    "store": {
        "provider": "cloudflare",
        "key": "your-cloudflare-key",
        "secret": "your-cloudflare-secret"
    }
}

When using Secure Storage Connectors, you only need to provide your Connector ID:

Attribute Type Description
id String Secure Storage Connector ID
{
    "store": {
        "id": "your-connector-id"
    }
}

Cloudflare R2 properties and settings

Attribute Type Required Description
bucket String Yes Name of a destination bucket in your Cloudflare R2.
path String No Destination path in your Cloudflare R2 bucket (without leading slash). Defaults to root.
acl String No The Access Control List of the destination object. This can be public-read or private. Defaults to public-read.
metadata Hash No Custom metadata .
headers Hash No Custom HTTP headers.

The Optidash API allows you to set the following custom headers on your objects: Cache-Control, Content-Type, Content-Encoding and Content-Disposition.

{
    "store": {
        "provider": "cloudflare",
        "key": "your-cloudflare-key",
        "secret": "your-cloudflare-secret",
        "bucket": "images",
        "path": "assets/image.jpg",
        "acl": "public-read",
        "metadata": {
            "key": "value"
        },
        "headers": {
            "Cache-Control": "max-age=2592000000"
        }
    }
}

An example cURL request of using Cloudflare R2 as External Storage provider will look like the following:

curl https://api.optidash.ai/1.0/fetch -X POST -u your-api-key: \
-H "Content-Type: application/json" \
-d '{
    "url": "https://www.website.com/image.jpg",
    "resize": {
        "width": 100,
        "height": 75
    },
    "store": {
        "provider": "cloudflare",
        "key": "your-cloudflare-key",
        "secret": "your-cloudflare-secret",
        "bucket": "images",
        "path": "assets/image.jpg",
        "headers": {
            "Cache-Control": "max-age=2592000000"
        }
    }
}'

When using Cloudflare R2 as your External Storage, the url property within the JSON response will point to the object's location within the Cloudflare R2 and you can safely use that URL in production, for example:

HTTP/1.1 200 OK

Status: 200 OK
Content-Type: application/json

{
    "success": true,
    "code": 200,
    "id": "9fccf4b5-cfab-4e92-9276-5d2028fcb6a0",
    "input": {
        "name": "image.jpg",
        ..
    },
    "output": {
        "url": "https://bucket-name.account.r2.cloudflarestorage.com/image.jpg"",
        ..
    }
}