Skip to content
Start here

Upload Object

r2.buckets.objects.upload(strobject_key, objectbody, ObjectUploadParams**kwargs) -> ObjectUploadResponse
PUT/accounts/{account_id}/r2/buckets/{bucket_name}/objects/{object_key}

Uploads an object to an R2 bucket. The object body is provided as the request body. Returns metadata about the uploaded object.

The maximum upload size for this endpoint is 300 MB. For most workloads, we recommend using R2’s S3-compatible API or a Worker with an R2 binding instead.

Security

API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
ParametersExpand Collapse
account_id: str

Account ID.

maxLength32
bucket_name: str

Name of the bucket.

maxLength64
minLength3
object_key: str

The key (name) to assign to the object. May contain slashes for path-like keys. Slashes (/) within the key MUST be sent literally and MUST NOT be percent-encoded (i.e. %2F); other reserved characters should be percent-encoded as usual.

The object body to upload.

jurisdiction: Optional[Literal["default", "eu", "fedramp"]]

Jurisdiction where objects in this bucket are guaranteed to be stored.

One of the following:
"default"
"eu"
"fedramp"
cf_r2_storage_class: Optional[Literal["Standard", "InfrequentAccess"]]

Storage class for newly uploaded objects, unless specified otherwise.

One of the following:
"Standard"
"InfrequentAccess"
ReturnsExpand Collapse
class ObjectUploadResponse:

Result of a successful object upload.

etag: Optional[str]

The entity tag for the uploaded object.

key: Optional[str]

The key (name) of the uploaded object.

size: Optional[str]

The size of the uploaded object in bytes (as a string).

storage_class: Optional[Literal["Standard", "InfrequentAccess"]]

Storage class for newly uploaded objects, unless specified otherwise.

One of the following:
"Standard"
"InfrequentAccess"
uploaded: Optional[datetime]

The date and time the object was uploaded.

formatdate-time
version: Optional[str]

The version UUID of the uploaded object.

Upload Object

import os
from cloudflare import Cloudflare

client = Cloudflare(
    api_token=os.environ.get("CLOUDFLARE_API_TOKEN"),  # This is the default and can be omitted
)
response = client.r2.buckets.objects.upload(
    object_key="path/to/my-object.txt",
    body=b"Example data",
    account_id="023e105f4ecef8ad9ca31a8372d0c353",
    bucket_name="example-bucket",
)
print(response.etag)
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    "string"
  ],
  "result": {
    "etag": "d41d8cd98f00b204e9800998ecf8427e",
    "key": "path/to/my-object.txt",
    "size": "1048576",
    "storage_class": "Standard",
    "uploaded": "2024-01-15T10:30:00Z",
    "version": "3fd5b4a8-1234-5678-abcd-ef0123456789"
  },
  "success": true
}
Returns Examples
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    "string"
  ],
  "result": {
    "etag": "d41d8cd98f00b204e9800998ecf8427e",
    "key": "path/to/my-object.txt",
    "size": "1048576",
    "storage_class": "Standard",
    "uploaded": "2024-01-15T10:30:00Z",
    "version": "3fd5b4a8-1234-5678-abcd-ef0123456789"
  },
  "success": true
}