为 Cloud Storage 存储分区停用请求者付款状态。
包含此代码示例的文档页面
如需查看上下文中使用的代码示例,请参阅以下文档:
代码示例
C#
如需了解详情,请参阅 Cloud Storage C# API 参考文档。
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;
public class DisableRequesterPaysSample
{
public Bucket DisableRequesterPays(
string projectId = "your-project-id",
string bucketName = "your-unique-bucket-name")
{
var storage = StorageClient.Create();
var bucket = storage.GetBucket(bucketName, new GetBucketOptions
{
UserProject = projectId
});
bucket.Billing ??= new Bucket.BillingData();
bucket.Billing.RequesterPays = false;
bucket = storage.UpdateBucket(bucket, new UpdateBucketOptions
{
UserProject = projectId
});
Console.WriteLine($"Requester pays disabled for bucket {bucketName}.");
return bucket;
}
}
C++
如需了解详情,请参阅 Cloud Storage C++ API 参考文档。
namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name,
std::string const& billed_project) {
StatusOr<gcs::BucketMetadata> metadata = client.PatchBucket(
bucket_name,
gcs::BucketMetadataPatchBuilder().SetBilling(gcs::BucketBilling{false}),
gcs::UserProject(billed_project));
if (!metadata) throw std::runtime_error(metadata.status().message());
std::cout << "Billing configuration for bucket " << bucket_name
<< " is updated. The bucket now";
if (!metadata->has_billing()) {
std::cout << " has no billing configuration.\n";
} else if (metadata->billing().requester_pays) {
std::cout << " is configured to charge the caller for requests\n";
} else {
std::cout << " is configured to charge the project that owns the bucket"
<< " for requests.\n";
}
}
Go
如需了解详情,请参阅 Cloud Storage Go API 参考文档。
import (
"context"
"fmt"
"io"
"time"
"cloud.google.com/go/storage"
)
// disableRequesterPays sets requester pays flag to false.
func disableRequesterPays(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)
bucketAttrsToUpdate := storage.BucketAttrsToUpdate{
RequesterPays: false,
}
if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
return fmt.Errorf("Bucket(%q).Update: %v", bucketName, err)
}
fmt.Fprintf(w, "Requester pays disabled for bucket %v\n", bucketName)
return nil
}
Java
如需了解详情,请参阅 Cloud Storage Java API 参考文档。
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
public class DisableRequesterPays {
public static void disableRequesterPays(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().setRequesterPays(false).build().update();
System.out.println("Requester pays disabled for bucket " + 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 disableRequesterPays() {
// Disables requester-pays requests
await storage.bucket(bucketName).disableRequesterPays();
console.log(
`Requester-pays requests have been disabled for bucket ${bucketName}`
);
}
disableRequesterPays().catch(console.error);
PHP
如需了解详情,请参阅 Cloud Storage PHP API 参考文档。
use Google\Cloud\Storage\StorageClient;
/**
* Disable a bucket's requesterpays metadata.
*
* @param string $projectId Your Google Cloud project ID.
* @param string $bucketName Name of your Google Cloud Storage bucket.
*
* @return void
*/
function disable_requester_pays($projectId, $bucketName)
{
$storage = new StorageClient([
'projectId' => $projectId
]);
$bucket = $storage->bucket($bucketName);
$bucket->update([
'billing' => [
'requesterPays' => false
]
]);
printf('Requester pays has been disabled for %s' . PHP_EOL, $bucketName);
}
Python
如需了解详情,请参阅 Cloud Storage Python API 参考文档。
from google.cloud import storage
def disable_requester_pays(bucket_name):
"""Disable a bucket's requesterpays metadata"""
# bucket_name = "my-bucket"
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
bucket.requester_pays = False
bucket.patch()
print("Requester Pays has been disabled for {}".format(bucket_name))
Ruby
如需了解详情,请参阅 Cloud Storage Ruby API 参考文档。
def disable_requester_pays 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.requester_pays = false
puts "Requester pays has been disabled for #{bucket_name}"
end
后续步骤
如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器