取得 Cloud Storage 服務代理

本頁面說明如何找出專案的 Cloud Storage 服務代理電子郵件地址。Cloud Storage 服務代理是由 Cloud Storage 建立及管理的特殊服務帳戶。如要概略瞭解 Cloud Storage 服務代理,包括建立時間和使用方式,請參閱「Cloud Storage 服務帳戶」。如要概略瞭解 Google Cloud中的服務帳戶,請參閱「服務帳戶」。

事前準備

如要取得必要權限,找出專案服務代理的電子郵件地址,請要求管理員授予您專案的「查看服務帳戶」(roles/iam.serviceAccountViewer) 角色。

這個預先定義的角色具備 resourcemanager.projects.get 權限,可存取專案的服務代理。您也可以透過其他預先定義的角色取得這項權限。如要查看哪些角色與哪些權限相關聯,請參閱「Cloud Storage 的 IAM 角色」。

如要瞭解如何使用角色控管專案存取權,請參閱「管理存取權」。

取得專案 Cloud Storage 服務代理的電子郵件地址

控制台

  1. 前往 Google Cloud 控制台的 Cloud Storage「Settings」(設定) 頁面。

    前往設定頁面

  2. 在「Project Access」(專案存取權) 分頁中,電子郵件地址會顯示在「Cloud Storage Service Account」(Cloud Storage 服務帳戶) 部分。

指令列

使用 gcloud storage service-agent 指令:

gcloud storage service-agent --project=PROJECT_IDENTIFIER

其中 PROJECT_IDENTIFIER 是相關專案的 ID 或編號。例如:my-project。

用戶端程式庫

C++

詳情請參閱 Cloud Storage C++ API 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

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

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

C#

詳情請參閱 Cloud Storage C# API 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。


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

public class GetStorageServiceAccountSample
{
	public string GetStorageServiceAccount(string projectId = "your-project-id")
	{
		var storage = StorageClient.Create();

		var serviceAccountEmail = storage.GetStorageServiceAccountEmail(projectId);

		Console.WriteLine($"The GCS service account for project {projectId} is: {serviceAccountEmail}.");
		return serviceAccountEmail;
	}
}

Go

詳情請參閱 Cloud Storage Go API 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

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 {
	// projectID := "my-project-id"

	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("storage.NewClient: %w", 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: %w", err)
	}

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

Java

詳情請參閱 Cloud Storage Java API 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

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 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

/**
 * 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);

PHP

詳情請參閱 Cloud Storage PHP API 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

use Google\Cloud\Storage\StorageClient;

/**
 * Get the current service account email.
 *
 * @param string $projectId The ID of your Google Cloud Platform project.
 *        (e.g. 'my-project-id')
 */
function get_service_account(string $projectId): void
{
    $storage = new StorageClient([
        'projectId' => $projectId,
    ]);

    $serviceAccountEmail = $storage->getServiceAccount();

    printf('The GCS service account email for project %s is %s', $projectId, $serviceAccountEmail);
}

Python

詳情請參閱 Cloud Storage Python API 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

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(
        f"The GCS service account for project {storage_client.project} is: {email} "
    )

Ruby

詳情請參閱 Cloud Storage Ruby API 參考文件。

如要向 Cloud Storage 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證機制」。

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. 安裝並初始化 gcloud CLI,以便為 Authorization 標頭產生存取權杖。

  2. 使用 cURL 透過 GET serviceAccount 要求呼叫 JSON API:

    curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://storage.googleapis.com/storage/v1/projects/PROJECT_ID/serviceAccount"

    其中 PROJECT_ID 是相關專案的 ID 或編號。例如:my-project。

後續步驟