打印存储桶的网站配置

输出 Cloud Storage 存储桶的网站配置。

代码示例

C++

如需了解详情,请参阅 Cloud Storage C++ API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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();

  if (!bucket_metadata->has_website()) {
    std::cout << "Static website configuration is not set for bucket "
              << bucket_metadata->name() << "\n";
    return;
  }

  std::cout << "Static website configuration set for bucket "
            << bucket_metadata->name() << "\nThe main page suffix is: "
            << bucket_metadata->website().main_page_suffix
            << "\nThe not found page is: "
            << bucket_metadata->website().not_found_page << "\n";
}

PHP

如需了解详情,请参阅 Cloud Storage PHP API 参考文档

如需向 Cloud Storage 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

use Google\Cloud\Storage\StorageClient;

/**
 * Print the website configuration for a Cloud Storage bucket.
 *
 * @param string $bucketName The name of your Cloud Storage bucket.
 */
function print_bucket_website_configuration(string $bucketName): void
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $info = $bucket->info();

    if (!array_key_exists('website', $info)) {
        printf('Bucket website configuration not set' . PHP_EOL);
    } else {
        printf(
          'Index page: %s' . PHP_EOL . '404 page: %s' . PHP_EOL,
          $info['website']['mainPageSuffix'],
          $info['website']['notFoundPage'],
        );
    }
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器