PHP Client for Cloudflare API
R2

Objects

Objects stored in an R2 bucket, over Cloudflare's REST API.

Objects stored in an R2 bucket, over Cloudflare's REST API.

List

List objects in a bucket.

ParamTypeRequiredDefaultDescription
accountIdstringYesAccount Identifier.
bucketNamestringYesBucket Name.
paramsarrayNo[]Query Parameters: `per_page`, `prefix`, `delimiter`, `cursor` and `start_after`.
jurisdictionstring|nullNoJurisdiction where objects in this bucket are guaranteed to be stored.
php
$response = $client->r2()->objects()->list('ACCOUNT_ID', 'BUCKET_NAME', [], 'JURISDICTION');
View this operation on the Cloudflare API Reference

Get

Retrieve an object from a bucket.

The response carries the object itself, not a JSON envelope, so read it with body() rather than json(). Object metadata comes back as response headers, reachable through toPsrResponse().

$object = $client->r2()->objects()->get('ACCOUNT_ID', 'my-bucket', 'path/to/file.txt');

file_put_contents('file.txt', $object->body());
ParamTypeRequiredDefaultDescription
accountIdstringYesAccount Identifier.
bucketNamestringYesBucket Name.
objectKeystringYesKey of the object, e.g. `path/to/file.txt`.
jurisdictionstring|nullNoJurisdiction where objects in this bucket are guaranteed to be stored.
php
$response = $client->r2()->objects()->get('ACCOUNT_ID', 'BUCKET_NAME', 'OBJECT_KEY', 'JURISDICTION');
View this operation on the Cloudflare API Reference

Upload

Upload an object to a bucket.

The object is sent as the raw request body, up to 300 MB. Anything larger needs R2's S3-compatible API and its multipart upload.

$client->r2()->objects()->upload(
    'ACCOUNT_ID',
    'my-bucket',
    'path/to/file.txt',
    file_get_contents('file.txt'),
    'text/plain'
);
ParamTypeRequiredDefaultDescription
accountIdstringYesAccount Identifier.
bucketNamestringYesBucket Name.
objectKeystringYesKey to store the object under, e.g. `path/to/file.txt`.
contentsstringYesThe object itself.
contentTypestringNo'application/octet-stream'Media type of the object.
storageClassstring|nullNoStorage class to store the object as, e.g. `Standard` or `InfrequentAccess`. Defaults to the bucket's own.
jurisdictionstring|nullNoJurisdiction where objects in this bucket are guaranteed to be stored.
php
$response = $client->r2()->objects()->upload('ACCOUNT_ID', 'BUCKET_NAME', 'OBJECT_KEY', 'CONTENTS', 'CONTENT_TYPE', 'STORAGE_CLASS', 'JURISDICTION');
View this operation on the Cloudflare API Reference

Delete

Delete a single object from a bucket.

ParamTypeRequiredDefaultDescription
accountIdstringYesAccount Identifier.
bucketNamestringYesBucket Name.
objectKeystringYesKey of the object to delete.
jurisdictionstring|nullNoJurisdiction where objects in this bucket are guaranteed to be stored.
php
$response = $client->r2()->objects()->delete('ACCOUNT_ID', 'BUCKET_NAME', 'OBJECT_KEY', 'JURISDICTION');
View this operation on the Cloudflare API Reference

Delete Many

Delete a list of objects from a bucket.

Every key given is deleted, and Cloudflare reports failures per key in the response rather than failing the whole request.

ParamTypeRequiredDefaultDescription
accountIdstringYesAccount Identifier.
bucketNamestringYesBucket Name.
objectKeysarrayYesKeys of the objects to delete.
jurisdictionstring|nullNoJurisdiction where objects in this bucket are guaranteed to be stored.
php
$response = $client->r2()->objects()->deleteMany('ACCOUNT_ID', 'BUCKET_NAME', [], 'JURISDICTION');
View this operation on the Cloudflare API Reference

Delete By Prefix

Delete every object whose key begins with a prefix.

Cloudflare answers with a job descriptor rather than doing the work inline: small jobs may come back already COMPLETED, larger ones keep running in the background, so poll the id you get back with $client->r2()->jobs()->get(). Objects written after the job starts are not included in it.

ParamTypeRequiredDefaultDescription
accountIdstringYesAccount Identifier.
bucketNamestringYesBucket Name.
prefixstringYesKey prefix to delete under. Pass an empty string to delete every object — or use `emptyBucket()`, which says so plainly.
dataCatalogCheckboolNofalseRefuse the request with a `409` if R2 Data Catalog is enabled on the bucket.
jurisdictionstring|nullNoJurisdiction where objects in this bucket are guaranteed to be stored.
php
$response = $client->r2()->objects()->deleteByPrefix('ACCOUNT_ID', 'BUCKET_NAME', 'PREFIX', true, 'JURISDICTION');
View this operation on the Cloudflare API Reference

Empty Bucket

Delete every object in a bucket.

The same operation as deleteByPrefix() with an empty prefix, and it behaves the same way: you get a job descriptor back to poll with $client->r2()->jobs()->get().

Cloudflare refuses to empty a bucket that has event notifications configured, answering 409 with error code 10034 — remove the notification rules first. Abort any active multipart uploads before submitting, and avoid writing to the bucket while the job runs.

ParamTypeRequiredDefaultDescription
accountIdstringYesAccount Identifier.
bucketNamestringYesBucket Name.
dataCatalogCheckboolNofalseRefuse the request with a `409` if R2 Data Catalog is enabled on the bucket.
jurisdictionstring|nullNoJurisdiction where objects in this bucket are guaranteed to be stored.
php
$response = $client->r2()->objects()->emptyBucket('ACCOUNT_ID', 'BUCKET_NAME', true, 'JURISDICTION');
View this operation on the Cloudflare API Reference
Copyright © 2026