获取 Cloud Storage 服务代理

本页面介绍了如何查找项目的 Cloud Storage 服务代理的电子邮件地址,这是由 Cloud Storage 创建和管理的专用服务帐号。如需简要了解 Cloud Storage 服务代理,包括它们的创建时间以及使用方式,请参阅 Cloud Storage 服务帐号。如需大致了解 Google Cloud 中的服务帐号,请参阅服务帐号

如需获取项目的 Cloud Storage 服务代理的电子邮件地址,请执行以下操作

控制台

  1. 在 Google Cloud Console 中打开 Cloud Storage 浏览器。
    打开 Cloud Storage 浏览器
  2. 点击设置

  3. 项目访问权限标签页中,电子邮件地址会显示在 Cloud Storage 服务帐号部分。

代码示例

C++

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

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client) {
  StatusOr<gcs::ServiceAccount> account = client.GetServiceAccount();
  if (!account) throw std::runtime_error(account.status().message());

  std::cout << "The service account details are " << *account << "\n";
}

Go

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

import (
	"context"
	"fmt"
	"io"
	"time"

	"cloud.google.com/go/storage"
)

// getServiceAccount gets the default Cloud Storage service account email address.
func getServiceAccount(w io.Writer, projectID string) error {
	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()

	serviceAccount, err := client.ServiceAccount(ctx, projectID)
	if err != nil {
		return fmt.Errorf("ServiceAccount: %v", err)
	}

	fmt.Fprintf(w, "The GCS service account for project %v is: %v\n", projectID, serviceAccount)
	return nil
}

Java

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

import com.google.cloud.storage.ServiceAccount;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class GetServiceAccount {
  public static void getServiceAccount(String projectId) {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    ServiceAccount serviceAccount = storage.getServiceAccount(projectId);
    System.out.println(
        "The GCS service account for project " + projectId + " is: " + serviceAccount.getEmail());
  }
}

Node.js

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

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCP project
// const projectId = 'your-project-id';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage({
  projectId,
});

async function getServiceAccount() {
  const [serviceAccount] = await storage.getServiceAccount();
  console.log(
    `The GCS service account for project ${projectId} is: ${serviceAccount.emailAddress}`
  );
}

getServiceAccount().catch(console.error);

Python

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

from google.cloud import storage

def get_service_account():
    """Get the service account email"""
    storage_client = storage.Client()

    email = storage_client.get_service_account_email()
    print(
        "The GCS service account for project {} is: {} ".format(
            storage_client.project, email
        )
    )

Ruby

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

def get_service_account
  require "google/cloud/storage"

  storage = Google::Cloud::Storage.new
  email = storage.service_account_email

  puts "The GCS service account for project #{storage.project_id} is: #{email}"
end

JSON API

  1. OAuth 2.0 Playground 获取授权访问令牌。将 Playground 配置为使用您自己的 OAuth 凭据。
  2. 使用 cURL,通过 GET serviceAccount 请求调用 JSON API:

    curl -X GET -H "Authorization: Bearer OAUTH2_TOKEN" \
    "https://storage.googleapis.com/storage/v1/projects/PROJECT_ID/serviceAccount"

    其中:

    • OAUTH2_TOKEN 是您在第 1 步中生成的访问令牌的名称。
    • PROJECT_ID 是相关项目的 ID。例如 my-project