Skip to content
Start here

Upload Object

client.R2.Buckets.Objects.Upload(ctx, bucketName, objectKey, body, params) (*BucketObjectUploadResponse, error)
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
bucketName string

Name of the bucket.

maxLength64
minLength3
objectKey string

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.

body Reader

The object body to upload.

params BucketObjectUploadParams
AccountID param.Field[string]

Path param: Account ID.

maxLength32
Jurisdiction param.Field[BucketObjectUploadParamsCfR2Jurisdiction]Optional

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

const BucketObjectUploadParamsCfR2JurisdictionDefault BucketObjectUploadParamsCfR2Jurisdiction = "default"
const BucketObjectUploadParamsCfR2JurisdictionEu BucketObjectUploadParamsCfR2Jurisdiction = "eu"
const BucketObjectUploadParamsCfR2JurisdictionFedramp BucketObjectUploadParamsCfR2Jurisdiction = "fedramp"
CfR2StorageClass param.Field[BucketObjectUploadParamsCfR2StorageClass]Optional

Header param: Storage class for newly uploaded objects, unless specified otherwise.

const BucketObjectUploadParamsCfR2StorageClassStandard BucketObjectUploadParamsCfR2StorageClass = "Standard"
const BucketObjectUploadParamsCfR2StorageClassInfrequentAccess BucketObjectUploadParamsCfR2StorageClass = "InfrequentAccess"
ReturnsExpand Collapse
type BucketObjectUploadResponse struct{…}

Result of a successful object upload.

Etag stringOptional

The entity tag for the uploaded object.

Key stringOptional

The key (name) of the uploaded object.

Size stringOptional

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

StorageClass BucketObjectUploadResponseStorageClassOptional

Storage class for newly uploaded objects, unless specified otherwise.

One of the following:
const BucketObjectUploadResponseStorageClassStandard BucketObjectUploadResponseStorageClass = "Standard"
const BucketObjectUploadResponseStorageClassInfrequentAccess BucketObjectUploadResponseStorageClass = "InfrequentAccess"
Uploaded TimeOptional

The date and time the object was uploaded.

formatdate-time
Version stringOptional

The version UUID of the uploaded object.

Upload Object

package main

import (
  "bytes"
  "context"
  "fmt"
  "io"

  "github.com/stainless-sdks/cloudflare-go"
  "github.com/stainless-sdks/cloudflare-go/option"
  "github.com/stainless-sdks/cloudflare-go/r2"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  response, err := client.R2.Buckets.Objects.Upload(
    context.TODO(),
    "example-bucket",
    "path/to/my-object.txt",
    io.Reader(bytes.NewBuffer([]byte("Example data"))),
    r2.BucketObjectUploadParams{
      AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
    },
  )
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", 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
}