访问公开数据

Cloud Storage 中存储的某些数据是已配置的,因此任何人都可以随时读取。您可以通过多种方式访问这些公开数据,具体取决于您要处理数据的方式。

  1. 获取公开对象的名称以及存储该对象的存储桶。

  2. 使用以下 URI 访问存储分区中的对象:

    https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME

例如,Google 公开存储桶 gcp-public-data-landsat 包含 Landsat 公开数据集。您可以将公开共享的对象 LC08/01/001/003/LC08_L1GT_001003_20140812_20170420_01_T2/LC08_L1GT_001003_20140812_20170420_01_T2_B3.TIF 链接到以下链接:

https://storage.googleapis.com/gcp-public-data-landsat/LC08/01/001/003/LC08_L1GT_001003_20140812_20170420_01_T2/LC08_L1GT_001003_20140812_20170420_01_T2_B3.TIF

控制台

  1. 获取公开对象的名称以及存储该对象的存储桶。

  2. 在网络浏览器中,通过以下 URI 访问该对象(如果您尚未登录,则系统会要求您登录):

    https://console.cloud.google.com/storage/browser/_details/BUCKET_NAME/OBJECT_NAME

  3. 如果公众有权列出存储桶的内容,您可以使用以下 URI 列出存储桶中的所有对象:

    https://console.cloud.google.com/storage/browser/BUCKET_NAME

例如,Google 公开存储桶 gcp-public-data-landsat 包含 Landsat 公开数据集。您可以使用以下网址访问该存储桶:

https://console.cloud.google.com/storage/browser/gcp-public-data-landsat

命令行

  1. 如果您没有 gcloud CLI,请按照相关说明进行安装

    • 安装 gcloud CLI 时,如果您不想进行身份验证,请跳过运行 gcloud init 命令的步骤,并运行以下命令:

      gcloud config set auth/disable_credentials True
  2. 获取公开对象的名称以及存储该对象的存储桶。

  3. 如果用于列出存储桶内容的列出权限被授予公众,您可以使用 ls 命令列出存储桶中包含的部分或全部对象。

    例如,Google 公开存储桶 gcp-public-data-landsat 包含 Landsat 公开数据集。您可以使用以下命令列出前缀为 LC08/01/001/003/LC 的对象:

    gcloud storage ls --recursive gs://gcp-public-data-landsat/LC08/01/001/003/LC*
  4. 使用 cp 命令下载该存储桶中包含的特定公开对象。

    例如,以下命令会将文件从存储分区 gcp-public-data-landsat 下载到您的本地目录:

    gcloud storage cp gs://gcp-public-data-landsat/LC08/01/001/003/LC08_L1GT_001003_20140812_20170420_01_T2/LC08_L1GT_001003_20140812_20170420_01_T2_B3.TIF .

客户端库

C++

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

如需向 Cloud Storage 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证

namespace gcs = ::google::cloud::storage;
[](std::string const& bucket_name, std::string const& object_name) {
  // Create a client that does not authenticate with the server.
  auto client = gcs::Client{
      google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
          google::cloud::MakeInsecureCredentials())};

  // Read an object, the object must have been made public.
  gcs::ObjectReadStream stream = client.ReadObject(bucket_name, object_name);

  int count = 0;
  std::string line;
  while (std::getline(stream, line, '\n')) {
    ++count;
  }
  if (stream.bad()) throw google::cloud::Status(stream.status());
  std::cout << "The object has " << count << " lines\n";
}

C#

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

如需向 Cloud Storage 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为客户端库设置身份验证


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