Turboreplikation verwalten

Mit Sammlungen den Überblick behalten Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.

Zu den Konzepten

Auf dieser Seite wird beschrieben, wie Sie das Feature der Turboreplikation für einen biregionalen Bucket verwalten.

Erforderliche Berechtigungen

Console

Sie benötigen die entsprechenden IAM-Berechtigungen, um diesen Leitfaden mit der Google Cloud Console abzuschließen. Damit Sie dieses Feature verwenden können, müssen Sie einen Bucket an einem biregionalen Standort haben oder erstellen. Wenn sich der Bucket, auf den Sie zugreifen möchten, in einem Projekt befindet, das nicht Sie erstellt haben, muss Ihnen der Projektinhaber möglicherweise erst eine Rolle zuweisen, die die erforderlichen Berechtigungen enthält.

Eine Liste der erforderlichen Berechtigungen für bestimmte Aktionen finden Sie unter IAM-Berechtigungen für die Google Cloud Console.

Eine Liste der relevanten Rollen finden Sie unter Cloud Storage-Rollen. Alternativ können Sie eine benutzerdefinierte Rolle erstellen, die spezifische, eingeschränkte Berechtigungen hat.

Befehlszeile

Damit Sie diese Anleitung mit einem Befehlszeilen-Dienstprogramm ausführen können, benötigen Sie die entsprechenden IAM-Berechtigungen. Damit Sie dieses Feature verwenden können, müssen Sie einen Bucket an einem biregionalen Standort haben oder erstellen. Wenn sich der Bucket, auf den Sie zugreifen möchten, in einem Projekt befindet, das nicht Sie erstellt haben, muss Ihnen der Projektinhaber möglicherweise erst eine Rolle zuweisen, die die erforderlichen Berechtigungen enthält.

Eine Liste der erforderlichen Berechtigungen für bestimmte Aktionen finden Sie unter IAM-Berechtigungen für gsutil-Befehle.

Eine Liste der relevanten Rollen finden Sie unter Cloud Storage-Rollen. Alternativ können Sie eine benutzerdefinierte Rolle erstellen, die spezifische, eingeschränkte Berechtigungen hat.

Clientbibliotheken

Sie benötigen die entsprechenden IAM-Berechtigungen, um diesen Leitfaden mit den Cloud Storage-Clientbibliotheken abzuschließen. Damit Sie dieses Feature verwenden können, müssen Sie einen Bucket an einem biregionalen Standort haben oder einen solchen erstellen. Wenn sich der Bucket, auf den Sie zugreifen möchten, in einem Projekt befindet, das nicht Sie erstellt haben, muss Ihnen der Projektinhaber möglicherweise erst eine Rolle zuweisen, die die erforderlichen Berechtigungen enthält.

Sofern nicht anders angegeben, werden Clientbibliotheksanfragen über die JSON API gestellt und benötigen die unter IAM-Berechtigungen für JSON-Methoden aufgeführten Berechtigungen. Um zu erfahren, welche JSON API-Methoden aufgerufen werden, wenn Sie Anfragen über eine Clientbibliothek stellen, können Sie die Rohanfragen protokollieren.

Eine Liste der relevanten IAM-Rollen finden Sie unter Cloud Storage-Rollen. Alternativ können Sie eine benutzerdefinierte Rolle erstellen, die spezifische, eingeschränkte Berechtigungen hat.

REST APIs

JSON API

Sie benötigen die entsprechenden IAM-Berechtigungen, um diese Anleitung mit der JSON API abzuschließen. Damit Sie dieses Feature verwenden können, müssen Sie einen Bucket an einem biregionalen Standort haben oder erstellen. Wenn sich der Bucket, auf den Sie zugreifen möchten, in einem Projekt befindet, das nicht Sie erstellt haben, muss Ihnen der Projektinhaber möglicherweise erst eine Rolle zuweisen, die die erforderlichen Berechtigungen enthält.

Eine Liste der für bestimmte Aktionen erforderlichen Berechtigungen finden Sie unter IAM-Berechtigungen für JSON-Methoden.

Eine Liste der relevanten Rollen finden Sie unter Cloud Storage-Rollen. Alternativ können Sie eine benutzerdefinierte Rolle erstellen, die spezifische, eingeschränkte Berechtigungen hat.

XML API

Dieses Feature kann nicht über die XML API verwaltet werden. Verwenden Sie stattdessen die JSON API.

Turboreplikation festlegen

Führen Sie die folgenden Anweisungen aus, um die Turboreplikation für einen vorhandenen Bucket zu aktivieren oder zu deaktivieren:

Console

  1. Wechseln Sie in der Cloud Console zur Seite Cloud Storage-Buckets.

    Buckets aufrufen

  2. Klicken Sie in der Bucket-Liste auf den Namen des gewünschten Buckets.

  3. Klicken Sie auf den Tab Konfiguration.

  4. Klicken Sie in der Zeile Replikation auf Bearbeiten.

    Das angezeigte Fenster gibt an, ob Sie im Begriff sind, die Turboreplikation zu aktivieren oder die Turboreplikation zu deaktivieren.

  5. Klicken Sie auf Speichern, um die neue Einstellung zu bestätigen.

Befehlszeile

gcloud

Führen Sie den Befehl gcloud storage buckets update mit dem Flag --rpo aus:

gcloud storage buckets update gs://BUCKET_NAME --rpo=STATE

Wobei:

  • BUCKET_NAME ist der Name des entsprechenden Buckets. Beispiel: my-bucket.

  • STATE ist ASYNC_TURBO zum Aktivieren der Turboreplikation oder DEFAULT zum Deaktivieren der Turboreplikation.

Wenn der Vorgang erfolgreich war, sieht die Antwort so aus:

Updating gs://my-bucket/...
  Completed 1  

gsutil

Führen Sie den Befehl gsutil rpo set aus:

gsutil rpo set STATE gs://BUCKET_NAME/

Dabei gilt:

  • STATE ist ASYNC_TURBO zum Aktivieren der Turboreplikation oder DEFAULT zum Deaktivieren der Turboreplikation.

  • BUCKET_NAME ist der Name des entsprechenden Buckets. Beispiel: my-bucket.

Wenn der Vorgang erfolgreich war, wird keine Antwort zurückgegeben. Die Änderung wird sofort wirksam.

Clientbibliotheken

C++

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage C++ API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name) {
  auto updated = client.PatchBucket(
      bucket_name,
      gcs::BucketMetadataPatchBuilder().SetRpo(gcs::RpoAsyncTurbo()));
  if (!updated) throw std::move(updated).status();

  std::cout << "RPO is set to 'ASYNC_TURBO' for " << updated->name() << "\n";
}

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name) {
  auto updated = client.PatchBucket(
      bucket_name,
      gcs::BucketMetadataPatchBuilder().SetRpo(gcs::RpoDefault()));
  if (!updated) throw std::move(updated).status();

  std::cout << "RPO is set to 'default' for " << updated->name() << "\n";
}

C#

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage C# API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

// Copyright 2021 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Google.Cloud.Storage.V1;

public class SetRpoAsyncTurboSample
{
    public void SetRpoAsyncTurbo(string bucketName = "your-unique-bucket-name")
    {
        // Enabling turbo replication requires a bucket with dual-region configuration
        var storage = StorageClient.Create();
        var bucket = storage.GetBucket(bucketName);
        bucket.Rpo = "ASYNC_TURBO";
        storage.UpdateBucket(bucket);

        Console.WriteLine($"Turbo replication enabled for bucket {bucketName}");
    }
}

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

// Copyright 2021 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Google.Cloud.Storage.V1;

public class SetRpoDefaultSample
{
    public void SetRpoDefault(string bucketName = "your-unique-bucket-name")
    {
        var storage = StorageClient.Create();
        var bucket = storage.GetBucket(bucketName);
        bucket.Rpo = "DEFAULT";
        storage.UpdateBucket(bucket);

        Console.WriteLine($"Turbo replication disabled for bucket {bucketName}");
    }
}

Go

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Go API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buckets

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

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

// setRPOAsyncTurbo enables turbo replication for the bucket by setting RPO to
// "ASYNC_TURBO". The bucket must be dual-region to use this feature.
func setRPOAsyncTurbo(w io.Writer, bucketName string) error {
	// bucketName := "bucket-name"
	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()

	bucket := client.Bucket(bucketName)
	setRPO := storage.BucketAttrsToUpdate{
		RPO: storage.RPOAsyncTurbo,
	}
	if _, err := bucket.Update(ctx, setRPO); err != nil {
		return fmt.Errorf("Bucket(%q).Update: %v", bucketName, err)
	}
	fmt.Fprintf(w, "Turbo replication enabled for %v", bucketName)
	return nil
}

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package buckets

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

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

// setRPODefault disables turbo replication for the bucket by setting RPO to
// "DEFAULT".
func setRPODefault(w io.Writer, bucketName string) error {
	// bucketName := "bucket-name"
	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()

	bucket := client.Bucket(bucketName)
	setRPO := storage.BucketAttrsToUpdate{
		RPO: storage.RPODefault,
	}
	if _, err := bucket.Update(ctx, setRPO); err != nil {
		return fmt.Errorf("Bucket(%q).Update: %v", bucketName, err)
	}
	fmt.Fprintf(w, "Turbo replication disabled for %v", bucketName)
	return nil
}

Java

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Java API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.storage.bucket;

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

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

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

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);

    bucket.toBuilder().setRpo(Rpo.ASYNC_TURBO).build().update();

    System.out.println("Turbo replication was enabled for " + bucketName);
  }
}

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

/*
 * Copyright 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.storage.bucket;

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

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

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

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);

    bucket.toBuilder().setRpo(Rpo.DEFAULT).build().update();

    System.out.println("Replication was set to default for " + bucketName);
  }
}

Node.js

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Node.js API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * This application demonstrates how to perform basic operations on buckets with
 * the Google Cloud Storage API.
 *
 * For more information, see the README.md under /storage and the documentation
 * at https://cloud.google.com/storage/docs.
 */

function main(bucketName = 'my-bucket') {
  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // The name of your GCS bucket in a dual-region
  // const bucketName = 'Name of a bucket, e.g. my-bucket';

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

  // Creates a client
  const storage = new Storage();

  // Enable turbo replication for the bucket by setting rpo to ASYNC_TURBO.
  // The bucket must be a dual-region bucket.
  async function setRPOAsyncTurbo() {
    await storage.bucket(bucketName).setMetadata({
      rpo: 'ASYNC_TURBO',
    });

    console.log(`Turbo replication enabled for ${bucketName}.`);
  }

  setRPOAsyncTurbo();
}

process.on('unhandledRejection', err => {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * This application demonstrates how to perform basic operations on buckets with
 * the Google Cloud Storage API.
 *
 * For more information, see the README.md under /storage and the documentation
 * at https://cloud.google.com/storage/docs.
 */

function main(bucketName = 'my-bucket') {
  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // The name of your GCS bucket in a dual-region
  // const bucketName = 'Name of a bucket, e.g. my-bucket';

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

  // Creates a client
  const storage = new Storage();

  // Disable turbo replication for the bucket by setting RPO to default.
  // The bucket must be a dual-region bucket.
  async function setRPODefault() {
    await storage.bucket(bucketName).setMetadata({
      rpo: 'DEFAULT',
    });

    console.log(`Turbo replication disabled for ${bucketName}.`);
  }

  setRPODefault();
}

process.on('unhandledRejection', err => {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));

PHP

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage PHP API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

<?php
/**
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * For instructions on how to run the full sample:
 *
 * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
 */

namespace Google\Cloud\Samples\Storage;

use Google\Cloud\Storage\StorageClient;

/**
 * Set the bucket's Turbo Replication(rpo) setting to `ASYNC_TURBO`.
 * The bucket must be a dual-region bucket.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 *        (e.g. 'my-bucket')
 */
function set_rpo_async_turbo(string $bucketName): void
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $rpo = 'ASYNC_TURBO';

    $bucket->update([
        'rpo' => $rpo
    ]);

    printf(
        'The replication behavior or recovery point objective (RPO) has been set to ASYNC_TURBO for %s.' . PHP_EOL,
        $bucketName
    );
}

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

<?php
/**
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * For instructions on how to run the full sample:
 *
 * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md
 */

namespace Google\Cloud\Samples\Storage;

use Google\Cloud\Storage\StorageClient;

/**
 * Set the bucket's replication behavior or recovery point objective (RPO) to `DEFAULT`.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 *        (e.g. 'my-bucket')
 */
function set_rpo_default(string $bucketName): void
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $rpo = 'DEFAULT';

    // Updating the rpo value of a multi-region bucket to DEFAULT has no effect
    // and updating the rpo value of a regional bucket will throw an exception.
    $bucket->update([
        'rpo' => $rpo
    ]);

    printf(
        'The replication behavior or recovery point objective (RPO) has been set to DEFAULT for %s.' . PHP_EOL,
        $bucketName
    );
}

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

Python

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Python API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

"""Sample that sets RPO (Recovery Point Objective) to ASYNC_TURBO
This sample is used on this page:
    https://cloud.google.com/storage/docs/managing-turbo-replication
For more information, see README.md.
"""

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

def set_rpo_async_turbo(bucket_name):
    """Sets the RPO to ASYNC_TURBO, enabling the turbo replication feature"""
    # The ID of your GCS bucket
    # bucket_name = "my-bucket"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)

    bucket.rpo = RPO_ASYNC_TURBO
    bucket.patch()

    print(f"RPO is set to ASYNC_TURBO for {bucket.name}.")

if __name__ == "__main__":
    set_rpo_async_turbo(bucket_name=sys.argv[1])

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

"""Sample that sets the replication behavior or recovery point objective (RPO) to default.
This sample is used on this page:
    https://cloud.google.com/storage/docs/managing-turbo-replication
For more information, see README.md.
"""

from google.cloud import storage
from google.cloud.storage.constants import RPO_DEFAULT

def set_rpo_default(bucket_name):
    """Sets the RPO to DEFAULT, disabling the turbo replication feature"""
    # The ID of your GCS bucket
    # bucket_name = "my-bucket"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)

    bucket.rpo = RPO_DEFAULT
    bucket.patch()

    print(f"RPO is set to DEFAULT for {bucket.name}.")

if __name__ == "__main__":
    set_rpo_default(bucket_name=sys.argv[1])

Ruby

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Ruby API.

Im folgenden Beispiel wird die Turboreplikation für einen Bucket aktiviert:

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def set_rpo_async_turbo bucket_name:
  # The ID of your GCS bucket
  # bucket_name = "your-unique-bucket-name"

  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  bucket  = storage.bucket bucket_name,
                           location: "ASIA"

  bucket.rpo = :ASYNC_TURBO

  puts "Turbo replication is enabled for #{bucket_name}."
end

set_rpo_async_turbo bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__

Im folgenden Beispiel wird die Standardreplikation für einen Bucket aktiviert:

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def set_rpo_default bucket_name:
  # The ID of your GCS bucket
  # bucket_name = "your-unique-bucket-name"

  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  bucket  = storage.bucket bucket_name

  bucket.rpo = :DEFAULT

  puts "The replication behavior or recovery point objective (RPO) for #{bucket_name} is set to default."
end

set_rpo_default bucket_name: ARGV.shift if $PROGRAM_NAME == __FILE__

REST APIs

JSON API

  1. Rufen Sie ein Zugriffstoken für die Autorisierung aus dem OAuth 2.0 Playground ab. Konfigurieren Sie den Playground so, dass Ihre eigenen OAuth-Anmeldedaten verwendet werden. Eine Anleitung finden Sie unter API-Authentifizierung.
  2. Erstellen Sie eine JSON-Datei, die folgende Informationen enthält:

    {
      "rpo": "STATE"
    }

    Dabei ist STATE ASYNC_TURBO zum Aktivieren der Turbo-Replikation oder DEFAULT zum Deaktivieren der Turbo-Replikation.

  3. Verwenden Sie cURL, um die JSON API mit einer PATCH-Bucket-Anfrage aufzurufen:

    curl -X PATCH --data-binary @JSON_FILE_NAME \
      -H "Authorization: Bearer OAUTH2_TOKEN" \
      -H "Content-Type: application/json" \
      "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=rpo"

    Dabei gilt:

    • JSON_FILE_NAME ist der Pfad für die JSON-Datei, die Sie in Schritt 2 erstellt haben.
    • OAUTH2_TOKEN ist das Zugriffstoken, das Sie in Schritt 1 generiert haben.
    • BUCKET_NAME ist der Name des entsprechenden Buckets. Beispiel: my-bucket.

    Wenn die Anfrage erfolgreich ist, wird keine Antwort zurückgegeben.

XML API

Dieses Feature kann nicht über die XML API verwaltet werden. Verwenden Sie stattdessen die JSON API.

Replikationsstatus eines Buckets prüfen

Führen Sie die folgenden Schritte aus, um das Recovery Point Objective (RPO) oder den Replikationsstatus eines Buckets zu prüfen:

Console

  1. Wechseln Sie in der Cloud Console zur Seite Cloud Storage-Buckets.

    Buckets aufrufen

  2. Klicken Sie in der Bucket-Liste auf den Namen des Buckets, den Sie prüfen möchten.

  3. Klicken Sie auf den Tab Konfiguration.

  4. Wenn die Turboreplikation für den Bucket aktiviert ist, wird die Replikation auf Turbo gesetzt.

Befehlszeile

gcloud

Führen Sie den Befehl gcloud storage buckets describe mit dem Flag --format aus.

gcloud storage buckets describe gs://BUCKET_NAME --format="default(rpo)"

Wobei:

  • BUCKET_NAME ist der Name des entsprechenden Buckets. Beispiel: my-bucket.

Wenn der Vorgang erfolgreich ausgeführt wurde, sieht die Antwort in etwa so aus:

rpo: ASYNC_TURBO

gsutil

Führen Sie den Befehl gsutil rpo get aus:

gsutil rpo get gs://BUCKET_NAME/

Wobei:

  • BUCKET_NAME ist der Name des entsprechenden Buckets. Beispiel: my-bucket.

Wenn der Vorgang erfolgreich ausgeführt wurde, sieht die Antwort in etwa so aus:

gs://my-bucket: ASYNC_TURBO

Clientbibliotheken

C++

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage C++ API.

namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name) {
  auto metadata = client.GetBucketMetadata(bucket_name);
  if (!metadata) throw std::move(metadata).status();

  std::cout << "RPO is " << metadata->rpo() << " for bucket "
            << metadata->name() << "\n";
}

C#

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage C# API.


using System;
using Google.Cloud.Storage.V1;

public class GetRpoSample
{
    public string GetRpo(string bucketName = "your-unique-bucket-name")
    {
        var storage = StorageClient.Create();
        var rpo = storage.GetBucket(bucketName).Rpo;
        Console.WriteLine($"The RPO Setting of bucket {bucketName} is {rpo}.");

        return rpo;
    }
}

Go

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Go API.

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

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

// getRPO gets the current RPO (Recovery Point Objective) setting
// for the bucket, either "DEFAULT" or "ASYNC_TURBO".
func getRPO(w io.Writer, bucketName string) error {
	// bucketName := "bucket-name"
	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()

	attrs, err := client.Bucket(bucketName).Attrs(ctx)
	if err != nil {
		return fmt.Errorf("Bucket(%q).Attrs: %v", bucketName, err)
	}
	fmt.Fprintf(w, "RPO is %s for %v", attrs.RPO, bucketName)
	return nil
}

Java

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Java API.

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

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

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

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Bucket bucket = storage.get(bucketName);
    String rpo = bucket.getRpo().toString();

    System.out.println("The RPO setting of bucket " + bucketName + " is " + rpo);
  }
}

Node.js

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Node.js API.

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The name of your GCS bucket in a dual-region
// const bucketName = 'Name of a bucket, e.g. my-bucket';

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

// Creates a client
const storage = new Storage();

async function getRPO() {
  // Gets Bucket Metadata and prints RPO value (either 'default' or 'async_turbo').
  // If RPO is undefined, the bucket is a single region bucket
  const [metadata] = await storage.bucket(bucketName).getMetadata();
  console.log(`RPO is ${metadata.rpo} for ${bucketName}.`);
}

getRPO();

PHP

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage PHP API.

use Google\Cloud\Storage\StorageClient;

/**
 * Get the bucket's recovery point objective (RPO) setting.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 *        (e.g. 'my-bucket')
 */
function get_rpo(string $bucketName): void
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);

    printf(
        'The bucket\'s RPO value is: %s.' . PHP_EOL,
        $bucket->info()['rpo']
    );
}

Python

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Python API.


from google.cloud import storage
from google.cloud.storage.constants import RPO_DEFAULT

def get_rpo(bucket_name):
    """Gets the RPO of the bucket"""
    # The ID of your GCS bucket
    # bucket_name = "my-bucket"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)

    bucket.rpo = RPO_DEFAULT
    rpo = bucket.rpo

    print(f"RPO for {bucket.name} is {rpo}.")

Ruby

Weitere Informationen finden Sie in der Referenzdokumentation zur Cloud Storage Ruby API.

def get_rpo bucket_name:
  # The ID of your GCS bucket
  # bucket_name = "your-unique-bucket-name"

  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  bucket  = storage.bucket bucket_name

  puts "The RPO setting of bucket '#{bucket_name}' is #{bucket.rpo}."
end

REST APIs

JSON API

  1. Rufen Sie ein Zugriffstoken für die Autorisierung aus dem OAuth 2.0 Playground ab. Konfigurieren Sie den Playground so, dass Ihre eigenen OAuth-Anmeldedaten verwendet werden. Eine Anleitung finden Sie unter API-Authentifizierung.
  2. Rufen Sie mithilfe von cURL die JSON API mit einer GET-Bucket-Anfrage auf:

    curl -X GET \
      -H "Authorization: Bearer OAUTH2_TOKEN" \
      "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=rpo"

    Dabei gilt:

    • OAUTH2_TOKEN ist der Name des Zugriffstokens, das Sie in Schritt 1 generiert haben.
    • BUCKET_NAME ist der Name des entsprechenden Buckets. Beispiel: my-bucket.

    Die Antwort sieht in etwa so aus:

    {
      "name": "my-bucket",
      "projectNumber": "234...",
      ...
      "rpo": "ASYNC_TURBO"
    }

    Beachten Sie den Schlüssel rpo. Der Wert ASYNC_TURBO gibt an, dass die Turboreplikation aktiviert ist. DEFAULT gibt an, dass die Standardreplikation angewendet wird. Das Feld rpo ist immer für bi- und multiregionale Buckets vorhanden, fehlt aber bei Buckets mit nur einer Region.

XML API

Dieses Feature kann nicht über die XML API verwaltet werden. Verwenden Sie stattdessen die JSON API.

Turboreplikationsleistung eines Buckets überwachen

  1. Wechseln Sie in der Cloud Console zur Seite Cloud Storage-Buckets.

    Buckets aufrufen

  2. Klicken Sie in der Bucket-Liste auf den Namen des gewünschten Buckets.

  3. Klicken Sie auf den Tab Konfiguration.

  4. Klicken Sie in der Zeile Replikation auf Replikationsmesswerte anzeigen.

    Wenn die Replikation eines Objekts länger als 15 Minuten dauerte, werden die zusätzlichen Minuten zusammengefasst und in der Google Cloud Console als Anzahl der fehlenden Minuten RPO angezeigt. Die Google Cloud Console verfolgt auch die Anzahl der abgeschlossenen Objektreplikationen, die als Objektreplikationen mit Turbo angezeigt werden.

    Weitere Informationen finden Sie unter Überwachung der Turbo-Replikationsleistung.

Nächste Schritte