创建具有增强型复制功能的双区域存储桶
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需了解详情,请参阅 Cloud Storage C# API 参考文档。
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++
如需了解详情,请参阅 Cloud Storage C++ API 参考文档。
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
如需了解详情,请参阅 Cloud Storage Go API 参考文档。
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*10)
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
如需了解详情,请参阅 Cloud Storage Java API 参考文档。
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
如需了解详情,请参阅 Cloud Storage Node.js API 参考文档。
/**
* 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
如需了解详情,请参阅 Cloud Storage PHP API 参考文档。
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
如需了解详情,请参阅 Cloud Storage Python API 参考文档。
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
如需了解详情,请参阅 Cloud Storage Ruby API 参考文档。
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
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。