举例说明如何获取默认基于事件的保全的状态。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C++
如需了解详情,请参阅 Cloud Storage C++ API 参考文档。
namespace gcs = ::google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name) {
StatusOr<gcs::BucketMetadata> bucket_metadata =
client.GetBucketMetadata(bucket_name);
if (!bucket_metadata) throw std::move(bucket_metadata).status();
std::cout << "The default event-based hold for objects in bucket "
<< bucket_metadata->name() << " is "
<< (bucket_metadata->default_event_based_hold() ? "enabled"
: "disabled")
<< "\n";
}
C#
如需了解详情,请参阅 Cloud Storage C# API 参考文档。
using Google.Cloud.Storage.V1;
using System;
public class GetDefaultEventBasedHoldSample
{
public bool GetDefaultEventBasedHold(string bucketName = "your-unique-bucket-name")
{
var storage = StorageClient.Create();
var bucket = storage.GetBucket(bucketName);
bool defaultEventBasedHold = bucket.DefaultEventBasedHold ?? false;
Console.WriteLine($"Default event-based hold: {defaultEventBasedHold}");
return defaultEventBasedHold;
}
}
Go
如需了解详情,请参阅 Cloud Storage Go API 参考文档。
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
attrs, err := c.Bucket(bucketName).Attrs(ctx)
if err != nil {
return nil, err
}
log.Printf("Default event-based hold enabled? %t\n",
attrs.DefaultEventBasedHold)
Java
如需了解详情,请参阅 Cloud Storage Java API 参考文档。
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageOptions;
public class GetDefaultEventBasedHold {
public static void getDefaultEventBasedHold(String projectId, String bucketName)
throws StorageException {
// 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,
Storage.BucketGetOption.fields(Storage.BucketField.DEFAULT_EVENT_BASED_HOLD));
if (bucket.getDefaultEventBasedHold() != null && bucket.getDefaultEventBasedHold()) {
System.out.println("Default event-based hold is enabled for " + bucketName);
} else {
System.out.println("Default event-based hold is not enabled for " + bucketName);
}
}
}
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';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function getDefaultEventBasedHold() {
const [metadata] = await storage.bucket(bucketName).getMetadata();
console.log(`Default event-based hold: ${metadata.defaultEventBasedHold}.`);
}
getDefaultEventBasedHold().catch(console.error);
PHP
如需了解详情,请参阅 Cloud Storage PHP API 参考文档。
use Google\Cloud\Storage\StorageClient;
/**
* Enables a default event-based hold for a bucket.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
*/
function get_default_event_based_hold(string $bucketName): void
{
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
if ($bucket->info()['defaultEventBasedHold']) {
printf('Default event-based hold is enabled for ' . $bucketName . PHP_EOL);
} else {
printf('Default event-based hold is not enabled for ' . $bucketName . PHP_EOL);
}
}
Python
如需了解详情,请参阅 Cloud Storage Python API 参考文档。
from google.cloud import storage
def get_default_event_based_hold(bucket_name):
"""Gets the default event based hold on a given bucket"""
# bucket_name = "my-bucket"
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
if bucket.default_event_based_hold:
print(f"Default event-based hold is enabled for {bucket_name}")
else:
print(
f"Default event-based hold is not enabled for {bucket_name}"
)
Ruby
如需了解详情,请参阅 Cloud Storage Ruby API 参考文档。
def get_default_event_based_hold 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
if bucket.default_event_based_hold?
puts "Default event-based hold is enabled for #{bucket_name}."
else
puts "Default event-based hold is not enabled for #{bucket_name}."
end
end
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。