Skip to content
Start here

Create Bucket

client.R2.Buckets.New(ctx, params) (*Bucket, error)
POST/accounts/{account_id}/r2/buckets

Creates an R2 bucket in the account and selected jurisdiction, with an optional location hint and default storage class.

Security

API Token

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

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
Accepted Permissions (at least one required)
Workers R2 Storage Write
ParametersExpand Collapse
params BucketNewParams
AccountID param.Field[string]

Path param: Cloudflare account ID that owns the R2 resource.

maxLength32
Name param.Field[string]

Body param: Name of the bucket.

maxLength64
minLength3
LocationHint param.Field[BucketNewParamsLocationHint]Optional

Body param: Location of the bucket.

const BucketNewParamsLocationHintApac BucketNewParamsLocationHint = "apac"
const BucketNewParamsLocationHintEeur BucketNewParamsLocationHint = "eeur"
const BucketNewParamsLocationHintEnam BucketNewParamsLocationHint = "enam"
const BucketNewParamsLocationHintWeur BucketNewParamsLocationHint = "weur"
const BucketNewParamsLocationHintWnam BucketNewParamsLocationHint = "wnam"
const BucketNewParamsLocationHintOc BucketNewParamsLocationHint = "oc"
StorageClass param.Field[BucketNewParamsStorageClass]Optional

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

const BucketNewParamsStorageClassStandard BucketNewParamsStorageClass = "Standard"
const BucketNewParamsStorageClassInfrequentAccess BucketNewParamsStorageClass = "InfrequentAccess"
CfR2Jurisdiction param.Field[BucketNewParamsCfR2Jurisdiction]Optional

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

const BucketNewParamsCfR2JurisdictionDefault BucketNewParamsCfR2Jurisdiction = "default"
const BucketNewParamsCfR2JurisdictionEu BucketNewParamsCfR2Jurisdiction = "eu"
const BucketNewParamsCfR2JurisdictionUs BucketNewParamsCfR2Jurisdiction = "us"
const BucketNewParamsCfR2JurisdictionFedramp BucketNewParamsCfR2Jurisdiction = "fedramp"
const BucketNewParamsCfR2JurisdictionFedrampHigh BucketNewParamsCfR2Jurisdiction = "fedramp-high"
ReturnsExpand Collapse
type Bucket struct{…}

A single R2 bucket.

CreationDate stringOptional

Creation timestamp.

Jurisdiction BucketJurisdictionOptional

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

One of the following:
const BucketJurisdictionDefault BucketJurisdiction = "default"
const BucketJurisdictionEu BucketJurisdiction = "eu"
const BucketJurisdictionUs BucketJurisdiction = "us"
const BucketJurisdictionFedramp BucketJurisdiction = "fedramp"
const BucketJurisdictionFedrampHigh BucketJurisdiction = "fedramp-high"
Location BucketLocationOptional

Location of the bucket.

One of the following:
const BucketLocationApac BucketLocation = "apac"
const BucketLocationEeur BucketLocation = "eeur"
const BucketLocationEnam BucketLocation = "enam"
const BucketLocationWeur BucketLocation = "weur"
const BucketLocationWnam BucketLocation = "wnam"
const BucketLocationOc BucketLocation = "oc"
Name stringOptional

Name of the bucket.

maxLength64
minLength3
StorageClass BucketStorageClassOptional

Storage class for newly uploaded objects, unless specified otherwise.

One of the following:
const BucketStorageClassStandard BucketStorageClass = "Standard"
const BucketStorageClassInfrequentAccess BucketStorageClass = "InfrequentAccess"

Create Bucket

package main

import (
  "context"
  "fmt"

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

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  bucket, err := client.R2.Buckets.New(context.TODO(), r2.BucketNewParams{
    AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
    Name: cloudflare.F("example-bucket"),
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", bucket.CreationDate)
}
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    "string"
  ],
  "result": {
    "creation_date": "creation_date",
    "jurisdiction": "default",
    "location": "apac",
    "name": "example-bucket",
    "storage_class": "Standard"
  },
  "success": true
}
Returns Examples
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    "string"
  ],
  "result": {
    "creation_date": "creation_date",
    "jurisdiction": "default",
    "location": "apac",
    "name": "example-bucket",
    "storage_class": "Standard"
  },
  "success": true
}