建立儲存空間值區

本頁說明如何建立 Cloud Storage 值區。如需值區總覽,請參閱重要詞彙。如果未在要求中特別指定,則系統會在 us 多地區位置建立值區,且預設的儲存空間級別為 Standard Storage

如要建立新的儲存空間值區:

主控台

  1. 在 Google Cloud Platform 主控台中開啟 Cloud Storage 瀏覽器。
    開啟 Cloud Storage 瀏覽器
  2. 按一下 [Create Bucket] (建立值區)
  3. 指定「Name」(名稱),名稱必須遵守值區命名規定
  4. 選取值區的「Default storage class」(預設儲存空間級別)。根據預設,系統會指派預設的儲存空間級別給所有上傳到值區的物件。

    注意:按一下 [Compare storage classes] (比較儲存空間級別),即可比較儲存空間級別和每月預估費用。

  5. 指定要儲存值區資料的「Location」(位置)
  6. 新的值區。

  7. 按一下 [Create] (建立)。

gsutil

使用 gsutil mb 指令,請將 [VALUES_IN_BRACKETS] 替換為適當的值:

gsutil mb gs://[BUCKET_NAME]/

設定下列的選用標記,進一步控管值區的建立作業:

  • -p:指定要與您的值區建立關聯的專案。
  • -c:指定值區的預設儲存空間級別
  • -l:指定值區的位置。

例如:

  gsutil mb -p [PROJECT_NAME] -c [STORAGE_CLASS] -l [BUCKET_LOCATION] gs://[BUCKET_NAME]/

程式碼範例

C++

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

namespace gcs = google::cloud::storage;
[](gcs::Client client, std::string bucket_name) {
  gcs::BucketMetadata meta =
      client.CreateBucket(bucket_name, gcs::BucketMetadata());
  std::cout << "Bucket created.  The metadata is " << meta << std::endl;
}

C#

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

private void CreateBucket(string bucketName)
{
    var storage = StorageClient.Create();
    storage.CreateBucket(s_projectId, bucketName);
    Console.WriteLine($"Created {bucketName}.");
}

Go

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

bucket := client.Bucket(bucketName)
if err := bucket.Create(ctx, projectID, &storage.BucketAttrs{
	StorageClass: "COLDLINE",
	Location:     "asia",
}); err != nil {
	return err
}

Java

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

Bucket bucket =
    storage.create(
        BucketInfo.newBuilder(bucketName)
            // See here for possible values: http://g.co/cloud/storage/docs/storage-classes
            .setStorageClass(StorageClass.COLDLINE)
            // Possible values: http://g.co/cloud/storage/docs/bucket-locations#location-mr
            .setLocation("asia")
            .build());

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

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

// Creates a new bucket
await storage.createBucket(bucketName, {
  location: 'ASIA',
  storageClass: 'COLDLINE',
});

console.log(`Bucket ${bucketName} created.`);

PHP

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

use Google\Cloud\Storage\StorageClient;

/**
 * Create a Cloud Storage Bucket.
 *
 * @param string $bucketName name of the bucket to create.
 * @param string $options options for the new bucket.
 *
 * @return Google\Cloud\Storage\Bucket the newly created bucket.
 */
function create_bucket($bucketName, $options = [])
{
    $storage = new StorageClient();
    $bucket = $storage->createBucket($bucketName, $options);
    printf('Bucket created: %s' . PHP_EOL, $bucket->name());
}

Python

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

def create_bucket(bucket_name):
    """Creates a new bucket."""
    storage_client = storage.Client()
    bucket = storage_client.create_bucket(bucket_name)
    print('Bucket {} created'.format(bucket.name))

Ruby

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

# project_id    = "Your Google Cloud project ID"
# bucket_name   = "Name of Google Cloud Storage bucket to create"
# location      = "Location of where to create Cloud Storage bucket"
# storage_class = "Storage class of Cloud Storage bucket"

require "google/cloud/storage"

storage = Google::Cloud::Storage.new project_id: project_id
bucket  = storage.create_bucket bucket_name,
                                location:      location,
                                storage_class: storage_class

puts "Created bucket #{bucket.name} in #{location}" +
     " with #{storage_class} class"

REST API

JSON API

如要進一步瞭解可設為值區預設值的儲存空間級別,請參閱儲存空間級別

  1. OAuth 2.0 Playground 取得授權存取憑證,並將 Playground 設為使用自己的 OAuth 憑證。
  2. 建立包含下列資訊的 .json 檔案,請將 [VALUES_IN_BRACKETS] 替換為適當的值:
  3. {
      "name": "[BUCKET_NAME]",
      "location": "[BUCKET_LOCATION]",
      "storageClass": "[STORAGE_CLASS]"
    }
  4. 使用 cURL 呼叫 JSON API,請將 [VALUES_IN_BRACKETS] 替換為適當的值:
    curl -X POST --data-binary @[JSON_FILE_NAME].json \
         -H "Authorization: Bearer [OAUTH2_TOKEN]" \
         -H "Content-Type: application/json" \
         "https://www.googleapis.com/storage/v1/b?project=[PROJECT_ID]"

XML API

如要進一步瞭解可設為值區預設值的儲存空間級別,請參閱儲存空間級別

  1. OAuth 2.0 Playground 取得授權存取憑證,並將 Playground 設為使用自己的 OAuth 憑證。
  2. 建立包含下列資訊的 .xml 檔案,請將 [VALUES_IN_BRACKETS] 替換為適當的值:
  3. <CreateBucketConfiguration>
       <LocationConstraint>[BUCKET_LOCATION]</LocationConstraint>
       <StorageClass>[STORAGE_CLASS]</StorageClass>
    </CreateBucketConfiguration>
  4. 使用 cURL 呼叫 XML API,請將 [VALUES_IN_BRACKETS] 替換為適當的值:
    curl -X PUT --data-binary @[XML_FILE_NAME].xml \
         -H "Authorization: Bearer [OAUTH2_TOKEN]" \
         -H "x-goog-project-id: [PROJECT_ID]" \
         "https://storage.googleapis.com/[BUCKET_NAME]"

後續步驟

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

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

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