Create a dual-region bucket with turbo replication

Stay organized with collections Save and categorize content based on your preferences.

Create a dual-region bucket with turbo replication

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C#

For more information, see the Cloud Storage C# API reference documentation.


using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;

public class CreateBucketWithTurboReplicationSample
{
    public Bucket CreateBucketWithTurboReplication(
        string projectId = "your-project-id",
        string location = "your-bucket-location",
        string bucketName = "bucket-name")
    {
        var storage = StorageClient.Create();
        // Enabling turbo replication requires a bucket with dual-region configuration
        var bucket = storage.CreateBucket(projectId, new Bucket { Name = bucketName , Location = location, Rpo = "ASYNC_TURBO" });

        Console.WriteLine($"Created {bucket.Name} in {bucket.Location} with RPO Setting {bucket.Rpo}.");
        return bucket;
    }

}

C++

For more information, see the Cloud Storage C++ API reference documentation.

namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name) {
  auto bucket =
      client.CreateBucket(bucket_name, gcs::BucketMetadata()
                                           .set_rpo(gcs::RpoAsyncTurbo())
                                           .set_location("NAM4"));
  if (!bucket) throw std::runtime_error(bucket.status().message());

  std::cout << "Created bucket " << bucket->name() << " with RPO set to "
            << bucket->rpo() << " in " << bucket->location() << ".\n";
}

Go

For more information, see the Cloud Storage Go API reference documentation.

import (
	"context"
	"fmt"
	"io"
	"time"

	"cloud.google.com/go/storage"
)

// createBucketTurboReplication creates a new dual-region bucket with
// turbo replication enabled.
func createBucketTurboReplication(w io.Writer, projectID, bucketName, location string) error {
	// projectID := "my-project-id"
	// bucketName := "bucket-name"
	// location := "NAM4" // the name of a dual-region location
	// See this documentation for other valid locations:
	// https://cloud.google.com/storage/docs/locations#location-dr

	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("storage.NewClient: %v", err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(ctx, time.Second*30)
	defer cancel()

	storageLocationAndRPO := &storage.BucketAttrs{
		Location: location,
		RPO:      storage.RPOAsyncTurbo,
	}
	bucket := client.Bucket(bucketName)
	if err := bucket.Create(ctx, projectID, storageLocationAndRPO); err != nil {
		return fmt.Errorf("Bucket(%q).Create: %v", bucketName, err)
	}
	fmt.Fprintf(w, "Created bucket %v with turbo replication in %v\n", bucketName, storageLocationAndRPO.Location)
	return nil
}

Java

For more information, see the Cloud Storage Java API reference documentation.

import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Rpo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class CreateBucketWithTurboReplication {
  public static void createBucketWithTurboReplication(
      String projectId, String bucketName, String location) {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    // The ID to give your GCS bucket
    // String bucketName = "your-unique-bucket-name";

    // The dual-region location to create your bucket in
    // String location = "NAM4"

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

    Bucket bucket =
        storage.create(
            BucketInfo.newBuilder(bucketName)
                .setLocation(location)
                .setRpo(Rpo.ASYNC_TURBO)
                .build());

    System.out.println(
        "Created bucket "
            + bucket.getName()
            + " in "
            + bucket.getLocation()
            + " with RPO setting"
            + bucket.getRpo());
  }
}

Node.js

For more information, see the Cloud Storage Node.js API reference documentation.

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The name of a dual-region location
// See this documentation for other valid locations:
// https://cloud.google.com/storage/docs/locations#location-dr
// const location = 'NAM4';

// Flag to enable turbo replication for this bucket
const rpo = 'ASYNC_TURBO';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
// The bucket in the sample below will be created in the project associated with this client.
// For more information, please see https://cloud.google.com/docs/authentication/production or https://googleapis.dev/nodejs/storage/latest/Storage.html
const storage = new Storage();

async function createBucketWithTurboReplication() {
  // For default values see: https://cloud.google.com/storage/docs/locations and
  // https://cloud.google.com/storage/docs/storage-classes
  const [bucket] = await storage.createBucket(bucketName, {
    location,
    rpo,
  });

  console.log(
    `${bucket.name} created with the recovery point objective (RPO) set to ${rpo} in ${location}.`
  );
}

createBucketWithTurboReplication();

PHP

For more information, see the Cloud Storage PHP API reference documentation.

use Google\Cloud\Storage\StorageClient;

/**
 * Create a Cloud Storage bucket with the recovery point objective (RPO) set to `ASYNC_TURBO`.
 * The bucket must be a dual-region bucket for this setting to take effect.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 * @param string $location The Dual-Region location where you want your bucket to reside (e.g. "US-CENTRAL1+US-WEST1").
                                           Read more at https://cloud.google.com/storage/docs/locations#location-dr
 */
function create_bucket_turbo_replication($bucketName, $location = 'nam4')
{
    // $bucketName = 'my-bucket';

    $storage = new StorageClient();
    $rpo = 'ASYNC_TURBO';

    // providing a location which is a dual-region location
    // makes sure the locationType is set to 'dual-region' implicitly
    // we can pass 'locationType' => 'dual-region'
    // to make it explicit
    $bucket = $storage->createBucket($bucketName, [
        'location' => $location,
        'rpo' => $rpo
    ]);
    printf('Bucket with recovery point objective (RPO) set to \'ASYNC_TURBO\' created: %s' . PHP_EOL, $bucket->name());
}

Python

For more information, see the Cloud Storage Python API reference documentation.


from google.cloud import storage
from google.cloud.storage.constants import RPO_ASYNC_TURBO


def create_bucket_turbo_replication(bucket_name):
    """Creates dual-region bucket with turbo replication enabled."""
    # The ID of your GCS bucket
    # bucket_name = "my-bucket"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    bucket.location = "NAM4"
    bucket.rpo = RPO_ASYNC_TURBO
    bucket.create()

    print(f"{bucket.name} created with the recovery point objective (RPO) set to {bucket.rpo} in {bucket.location}.")

Ruby

For more information, see the Cloud Storage Ruby API reference documentation.

def create_bucket_turbo_replication bucket_name:
  # The ID to give your GCS bucket
  # bucket_name = "your-unique-bucket-name"

  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  bucket  = storage.create_bucket bucket_name,
                                  location: "ASIA1",
                                  rpo:      "ASYNC_TURBO"

  puts "Created bucket #{bucket.name} in #{bucket.location} with "
  + "the recovery point objective (RPO) set to #{bucket.rpo}."
end

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.