取得值區資訊

本頁說明如何取得 Cloud Storage 值區的大小和中繼資料等相關資訊。如需值區總覽,請參閱重要詞彙。如要進一步瞭解特定值區屬性,請參閱儲存空間級別值區位置

判斷值區的大小

如要判斷值區的大小:

Stackdriver

  1. 開啟 Stackdriver Metrics Explorer。

    前往「Metrics Explorer」

    如果您之前並未使用過 Stackdriver,請按一下 [Create a workspace] (建立工作區) 按鈕,為包含值區的專案建立工作區。

  2. 在「Find resource type and metric」(尋找資源類型和指標) 文字方塊中,輸入 storage.googleapis.com/storage/total_bytes。

    關聯圖表會顯示所選專案中每個值區的大小。

您也可以使用 Metrics Explorer 測量諸如 storage.googleapis.com/storage/object_count 和 storage.googleapis.com/storage/total_byte_seconds 等其他值區指標,這兩者分別測量每日物件數和每日使用的儲存空間。如需可用指標的完整清單,請參閱 Stackdriver 指標說明文件;如要進一步瞭解如何使用 Metrics Explorer,請參閱指標、時間序列和資源一文。

gsutil

使用 gsutil du 指令並加上 -s 標記:

gsutil du -s gs://[BUCKET_NAME]/

其中 [BUCKET_NAME] 是相關值區的名稱。

回應類似下列範例:

134620      gs://[BUCKET_NAME]

在這個範例中,值區的大小為 134,620 位元組。

查看值區的位置和預設儲存空間級別

如何查看值區的地理位置和預設儲存空間級別

主控台

  1. 在 Google Cloud Platform Console 中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 在值區清單中找到要驗證的值區,然後查看其「Location」(位置) 和「Default storage class」(預設儲存空間級別)。

    驗證 Standard Storage 值區級別。

gsutil

使用下列 gsutil ls 指令:

gsutil ls -L -b gs://[BUCKET_NAME]/

其中 [BUCKET_NAME] 是相關值區的名稱。

回應類似下列範例:

gs://[BUCKET_NAME]/ :
  Storage class:         STANDARD
  Location constraint:   US
  ...

程式碼範例

C++

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

namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string bucket_name) {
  StatusOr<gcs::BucketMetadata> bucket_metadata =
      client.GetBucketMetadata(bucket_name);

  if (!bucket_metadata) {
    throw std::runtime_error(bucket_metadata.status().message());
  }

  std::cout << "The metadata for bucket " << bucket_metadata->name() << " is "
            << *bucket_metadata << "\n";
}

C#

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

private void GetBucketMetadata(string bucketName)
{
    var storage = StorageClient.Create();
    var bucket = storage.GetBucket(bucketName);
    Console.WriteLine($"Bucket:\t{bucket.Name}");
    Console.WriteLine($"Acl:\t{bucket.Acl}");
    Console.WriteLine($"Billing:\t{bucket.Billing}");
    Console.WriteLine($"Cors:\t{bucket.Cors}");
    Console.WriteLine($"DefaultEventBasedHold:\t{bucket.DefaultEventBasedHold}");
    Console.WriteLine($"DefaultObjectAcl:\t{bucket.DefaultObjectAcl}");
    Console.WriteLine($"Encryption:\t{bucket.Encryption}");
    if (bucket.Encryption != null)
    {
        Console.WriteLine($"KmsKeyName:\t{bucket.Encryption.DefaultKmsKeyName}");
    }
    Console.WriteLine($"Id:\t{bucket.Id}");
    Console.WriteLine($"Kind:\t{bucket.Kind}");
    Console.WriteLine($"Lifecycle:\t{bucket.Lifecycle}");
    Console.WriteLine($"Location:\t{bucket.Location}");
    Console.WriteLine($"LocationType:\t{bucket.LocationType}");
    Console.WriteLine($"Logging:\t{bucket.Logging}");
    Console.WriteLine($"Metageneration:\t{bucket.Metageneration}");
    Console.WriteLine($"Owner:\t{bucket.Owner}");
    Console.WriteLine($"ProjectNumber:\t{bucket.ProjectNumber}");
    Console.WriteLine($"RetentionPolicy:\t{bucket.RetentionPolicy}");
    Console.WriteLine($"SelfLink:\t{bucket.SelfLink}");
    Console.WriteLine($"StorageClass:\t{bucket.StorageClass}");
    Console.WriteLine($"TimeCreated:\t{bucket.TimeCreated}");
    Console.WriteLine($"Updated:\t{bucket.Updated}");
    Console.WriteLine($"Versioning:\t{bucket.Versioning}");
    Console.WriteLine($"Website:\t{bucket.Website}");
}

Go

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

import (
	"context"
	"fmt"
	"io"
	"log"

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

func getBucketMetadata(w io.Writer, client *storage.Client, bucketName string) (*storage.BucketAttrs, error) {
	// bucketName := "bucket-name"
	ctx := context.Background()

	// Initialize client.
	client, err := storage.NewClient(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close() // Closing the client safely cleans up background resources.

	attrs, err := client.Bucket(bucketName).Attrs(ctx)
	if err != nil {
		return nil, err
	}

	fmt.Fprintf(w, "BucketName: %v\n", attrs.Name)
	fmt.Fprintf(w, "Location: %v\n", attrs.Location)
	fmt.Fprintf(w, "LocationType: %v\n", attrs.LocationType)
	fmt.Fprintf(w, "StorageClass: %v\n", attrs.StorageClass)
	fmt.Fprintf(w, "TimeCreated: %v\n", attrs.Created)
	fmt.Fprintf(w, "Metageneration: %v\n", attrs.MetaGeneration)
	fmt.Fprintf(w, "PredefinedACL: %v\n", attrs.PredefinedACL)
	if attrs.Encryption != nil {
		fmt.Fprintf(w, "DefaultKmsKeyName: %v\n", attrs.Encryption.DefaultKMSKeyName)
	}
	if attrs.Website != nil {
		fmt.Fprintf(w, "IndexPage: %v\n", attrs.Website.MainPageSuffix)
		fmt.Fprintf(w, "NotFoundPage: %v\n", attrs.Website.NotFoundPage)
	}
	fmt.Fprintf(w, "DefaultEventBasedHold: %v\n", attrs.DefaultEventBasedHold)
	if attrs.RetentionPolicy != nil {
		fmt.Fprintf(w, "RetentionEffectiveTime: %v\n", attrs.RetentionPolicy.EffectiveTime)
		fmt.Fprintf(w, "RetentionPeriod: %v\n", attrs.RetentionPolicy.RetentionPeriod)
		fmt.Fprintf(w, "RetentionPolicyIsLocked: %v\n", attrs.RetentionPolicy.IsLocked)
	}
	fmt.Fprintf(w, "RequesterPays: %v\n", attrs.RequesterPays)
	fmt.Fprintf(w, "VersioningEnabled: %v\n", attrs.VersioningEnabled)
	if attrs.Logging != nil {
		fmt.Fprintf(w, "LogBucket: %v\n", attrs.Logging.LogBucket)
		fmt.Fprintf(w, "LogObjectPrefix: %v\n", attrs.Logging.LogObjectPrefix)
	}
	if attrs.Labels != nil {
		fmt.Fprintf(w, "\n\n\nLabels:")
		for key, value := range attrs.Labels {
			fmt.Fprintf(w, "\t%v = %v\n", key, value)
		}
	}

	return attrs, nil
}

Java

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

Storage storage = StorageOptions.getDefaultInstance().getService();

// The name of a bucket, e.g. "my-bucket"
// String bucketName = "my-bucket";

// Select all fields
// Fields can be selected individually e.g. Storage.BucketField.NAME
Bucket bucket = storage.get(bucketName, BucketGetOption.fields(Storage.BucketField.values()));

// Print bucket metadata
System.out.println("BucketName: " + bucket.getName());
System.out.println("DefaultEventBasedHold: " + bucket.getDefaultEventBasedHold());
System.out.println("DefaultKmsKeyName: " + bucket.getDefaultKmsKeyName());
System.out.println("Id: " + bucket.getGeneratedId());
System.out.println("IndexPage: " + bucket.getIndexPage());
System.out.println("Location: " + bucket.getLocation());
System.out.println("LocationType: " + bucket.getLocationType());
System.out.println("Metageneration: " + bucket.getMetageneration());
System.out.println("NotFoundPage: " + bucket.getNotFoundPage());
System.out.println("RetentionEffectiveTime: " + bucket.getRetentionEffectiveTime());
System.out.println("RetentionPeriod: " + bucket.getRetentionPeriod());
System.out.println("RetentionPolicyIsLocked: " + bucket.retentionPolicyIsLocked());
System.out.println("RequesterPays: " + bucket.requesterPays());
System.out.println("SelfLink: " + bucket.getSelfLink());
System.out.println("StorageClass: " + bucket.getStorageClass().name());
System.out.println("TimeCreated: " + bucket.getCreateTime());
System.out.println("VersioningEnabled: " + bucket.versioningEnabled());
if (bucket.getLabels() != null) {
  System.out.println("\n\n\nLabels:");
  for (Map.Entry<String, String> label : bucket.getLabels().entrySet()) {
    System.out.println(label.getKey() + "=" + label.getValue());
  }
}

Node.js

詳情請參閱 Cloud Storage Node.js API 參考說明文件

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

// Creates a client
const storage = new Storage();

async function getBucketMetadata() {
  // Get bucket metadata.
  /**
   * TODO(developer): Uncomment the following line before running the sample.
   */
  // const bucketName = 'Name of a bucket, e.g. my-bucket';

  // Get Bucket Metadata
  const [metadata] = await storage.bucket(bucketName).getMetadata();

  for (const [key, value] of Object.entries(metadata)) {
    console.log(`${key}: ${value}`);
  }
}

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Get bucket metadata.
 *
 * @param string $bucketName the name of your Cloud Storage bucket.
 *
 * @return void
 */
function get_bucket_metadata($bucketName)
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);
    $info = $bucket->info();

    printf("Bucket Metadata: %s" . PHP_EOL, print_r($info));
}

Python

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

storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)

print('ID: {}'.format(bucket.id))
print('Name: {}'.format(bucket.name))
print('Storage Class: {}'.format(bucket.storage_class))
print('Location: {}'.format(bucket.location))
print('Location Type: {}'.format(bucket.location_type))
print('Cors: {}'.format(bucket.cors))
print('Default Event Based Hold: {}'
      .format(bucket.default_event_based_hold))
print('Default KMS Key Name: {}'.format(bucket.default_kms_key_name))
print('Metageneration: {}'.format(bucket.metageneration))
print('Retention Effective Time: {}'
      .format(bucket.retention_policy_effective_time))
print('Retention Period: {}'.format(bucket.retention_period))
print('Retention Policy Locked: {}'.format(bucket.retention_policy_locked))
print('Requester Pays: {}'.format(bucket.requester_pays))
print('Self Link: {}'.format(bucket.self_link))
print('Time Created: {}'.format(bucket.time_created))
print('Versioning Enabled: {}'.format(bucket.versioning_enabled))
print('Labels:')
pprint.pprint(bucket.labels)

Ruby

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

# project_id  = "Your Google Cloud project ID"
# bucket_name = "Your Google Cloud Storage bucket name"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.bucket bucket_name

puts "ID:                       #{bucket.id}"
puts "Name:                     #{bucket.name}"
puts "Storage Class:            #{bucket.storage_class}"
puts "Location:                 #{bucket.location}"
puts "Location Type:            #{bucket.location_type}"
puts "Cors:                     #{bucket.cors}"
puts "Default Event Based Hold: #{bucket.default_event_based_hold?}"
puts "Default KMS Key Name:     #{bucket.default_kms_key}"
puts "Logging Bucket:           #{bucket.logging_bucket}"
puts "Logging Prefix:           #{bucket.logging_prefix}"
puts "Metageneration:           #{bucket.metageneration}"
puts "Retention Effective Time: #{bucket.retention_effective_at}"
puts "Retention Period:         #{bucket.retention_period}"
puts "Retention Policy Locked:  #{bucket.retention_policy_locked?}"
puts "Requester Pays:           #{bucket.requester_pays}"
puts "Self Link:                #{bucket.api_url}"
puts "Time Created:             #{bucket.created_at}"
puts "Versioning Enabled:       #{bucket.versioning?}"
puts "Index Page:               #{bucket.website_main}"
puts "Not Found Page:           #{bucket.website_404}"
puts "Labels:"
bucket.labels.each do |key, value|
  puts " - #{key} = #{value}"
end

REST API

JSON API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用自己的 OAuth 憑證。
  2. 使用 cURL 來透過包含所需 fieldsGET 值區要求呼叫 JSON API

    curl -X GET \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      "https://www.googleapis.com/storage/v1/b/[BUCKET_NAME]?fields=location%2CstorageClass"

    其中:

    • [OAUTH2_TOKEN] 是您在步驟 1 產生的存取憑證的名稱。
    • [BUCKET_NAME] 是相關值區的名稱。例如:my-bucket

回應類似下列範例:

{
  "location": "US",
  "storageClass": "STANDARD"
}

XML API

  1. OAuth 2.0 Playground 取得授權存取憑證。將 Playground 設為使用自己的 OAuth 憑證。
  2. 使用 cURL 來透過 GET 值區要求呼叫 XML API

    curl -X GET \
      -H "Authorization: Bearer [OAUTH2_TOKEN]" \
      "https://storage.googleapis.com/[BUCKET_NAME]?[QUERY_PARAMETER]"

    其中:

    • [OAUTH2_TOKEN] 是您在步驟 1 產生的存取憑證的名稱。
    • [BUCKET_NAME] 是相關值區的名稱。例如:my-bucket
    • [QUERY_PARAMETER] 是:

      • location 用於取得值區位置。回應類似下列範例:<LocationConstraint>US</LocationConstraint>
      • storageClass 用於取得值區的預設儲存空間級別。回應類似下列範例:<StorageClass>NEARLINE</StorageClass>

      XML API 一次只能使用一個查詢參數。

後續步驟

本頁內容對您是否有任何幫助?請提供意見:

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Storage
需要協助嗎?請前往我們的支援網頁