Storage Control Pause Anywhere Cache

Storage Control Pause Anywhere Cache

深入探索

如需包含這個程式碼範例的詳細說明文件,請參閱下列文章:

程式碼範例

C++

詳情請參閱「Cloud Storage C++ API 參考文件」。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

namespace storagecontrol = google::cloud::storagecontrol_v2;
[](storagecontrol::StorageControlClient client,
   std::string const& cache_name) {
  auto anywhere_cache = client.PauseAnywhereCache(cache_name);
  if (!anywhere_cache) throw std::move(anywhere_cache).status();
  std::cout << "Paused anywhere cache: " << anywhere_cache->name() << "\n";
}

Java

詳情請參閱「Cloud Storage Java API 參考文件」。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。


import com.google.storage.control.v2.AnywhereCache;
import com.google.storage.control.v2.PauseAnywhereCacheRequest;
import com.google.storage.control.v2.StorageControlClient;
import java.io.IOException;

public final class AnywhereCachePause {

  public static void anywhereCachePause(String cacheName) throws IOException {
    try (StorageControlClient storageControl = StorageControlClient.create()) {

      PauseAnywhereCacheRequest request =
          PauseAnywhereCacheRequest.newBuilder().setName(cacheName).build();

      AnywhereCache anywhereCache = storageControl.pauseAnywhereCache(request);

      System.out.printf("Paused anywhere cache: %s%n", anywhereCache.getName());
    }
  }
}

PHP

詳情請參閱「Cloud Storage PHP API 參考文件」。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\PauseAnywhereCacheRequest;

/**
 * Pauses an Anywhere Cache instance.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 *        (e.g. 'my-bucket')
 * @param string $anywhereCacheId Uniquely identifies the cache.
 *        (e.g. 'us-east1-b')
 */
function pause_anywhere_cache(string $bucketName, string $anywhereCacheId): void
{
    $storageControlClient = new StorageControlClient();

    // Set project to "_" to signify global bucket
    $formattedName = $storageControlClient->anywhereCacheName('_', $bucketName, $anywhereCacheId);

    $request = new PauseAnywhereCacheRequest([
        'name' => $formattedName,
    ]);

    $response = $storageControlClient->pauseAnywhereCache($request);

    printf('Paused anywhere cache: %s', $response->getName());
}

Ruby

詳情請參閱「Cloud Storage Ruby API 參考文件」。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

require "google/cloud/storage/control"

# Pauses a specific Anywhere Cache instance in a Cloud Storage bucket.
# This operation does not delete the cache; it can be resumed later using
# the `resume_anywhere_cache` method.
#
# @param bucket_name [String] The name of the Cloud Storage bucket.
# @param anywhere_cache_id [String] The ID of the Anywhere Cache instance to pause.
#   This is often the zone where the cache is located, e.g., "us-east1-b".
#
# @example
#   pause_anywhere_cache(
#     bucket_name: "your-unique-bucket-name",
#     anywhere_cache_id: "us-east1-b"
#   )
#
def pause_anywhere_cache bucket_name:, anywhere_cache_id:
  # Create a client object. The client can be reused for multiple calls.
  storage_control_client = Google::Cloud::Storage::Control.storage_control
  # Set project to "_" to signify global bucket
  parent = "projects/_/buckets/#{bucket_name}"
  name = "#{parent}/anywhereCaches/#{anywhere_cache_id}"

  # Create a request.
  request = Google::Cloud::Storage::Control::V2::PauseAnywhereCacheRequest.new(
    name: name
  )
  # The request pauses the cache, but does not delete it.
  # The cache can be resumed later.
  # The cache is paused in the specified bucket.
  begin
    result = storage_control_client.pause_anywhere_cache request
    puts "Successfully #{result.state&.downcase} anywhereCache - #{result.name}."
  rescue Google::Cloud::Error => e
    puts "Failed to pause AnywhereCache. Error: #{e.message}"
  end
end

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 瀏覽器範例