

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在本機區域中建立目錄儲存貯體
<a name="create-directory-bucket-LZ"></a>

在 Dedicated Local Zones 中，您可以建立目錄儲存貯體在特定資料周邊儲存和擷取物件，以協助符合您的資料落地和隔離使用案例。S3 目錄儲存貯體是 Local Zones 中唯一支援的儲存貯體類型，且包含稱為 `LocalZone` 的儲存貯體位置類型。目錄儲存貯體名稱是由您提供的基本名稱以及包含儲存貯體位置區域 ID 和 `--x-s3` 的字尾所組成。您可以使用 [DescribeAvailabilityZones](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAvailabilityZones.html) API 操作來取得本機區域 ID 清單。如需詳細資訊，請參閱[目錄儲存貯體命名規則](directory-bucket-naming-rules.md)。

**注意**  
對於 AWS 專用本機區域 （專用本機區域） 中的所有服務，包括 S3，您的管理員必須先啟用您的 ， AWS 帳戶 才能在專用本機區域中建立或存取任何資源。如需詳細資訊，請參閱[啟用 Local Zones 的帳戶](opt-in-directory-bucket-lz.md)。
對於資料落地要求，建議您只從閘道 VPC 端點存取儲存貯體。如需詳細資訊，請參閱[來自 VPC 的私有連線](connectivity-lz-directory-buckets.md)。
若要僅限在本機區域網路邊界群組內存取，您可以在 IAM 政策中使用條件索引鍵 `s3express:AllAccessRestrictedToLocalZoneGroup`。如需詳細資訊，請參閱[對本機區域中的目錄儲存貯體進行身分驗證和授權](iam-directory-bucket-LZ.md)。

以下說明如何使用 AWS 管理主控台 AWS CLI、 和 AWS SDKs 在單一本機區域中建立目錄儲存貯體。

## 使用 S3 主控台
<a name="create-directory-bucket-lz-console"></a>

1. 登入 AWS 管理主控台 ，並在 https：//[https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/) 開啟 Amazon S3 主控台。

1. 在頁面頂端的導覽列中，選擇目前顯示的名稱 AWS 區域。​接下來，選擇您要在其中建立目錄儲存貯體之本機區域的父區域。
**注意**  
如需父區域的詳細資訊，請參閱[Local Zones 中目錄儲存貯體的概念](s3-lzs-for-directory-buckets.md)。

1. 在左側導覽窗格中，選擇 **Buckets** (儲存貯體)。

1. 選擇 **Create bucket** (建立儲存貯體)。

   **Create bucket (建立儲存貯體)** 頁面隨即開啟。

1. 在**一般組態**下，檢視您要在其中建立儲存貯體的 AWS 區域 。

1.  針對**儲存貯體類型**，選擇**目錄**。
**注意**  
如果您已選擇不支援目錄儲存貯體的區域，儲存貯體類型預設為一般用途儲存貯體。若要建立目錄儲存貯體，您必須選擇支援的區域。如需支援目錄儲存貯體的區域清單，請參閱[目錄儲存貯體的地區和區域端點](s3-express-Regions-and-Zones.md)。
在您建立儲存貯體之後，便無法變更儲存貯體類型。

1. 在**儲存貯體位置**下，選擇您要使用的本機區域。
**注意**  
儲存貯體建立之後，就無法變更本機區域。

1. 在**儲存貯體位置**下，選取核取方塊，表示您確實了解在本機區域發生中斷時，您的資料可能無法使用或遺失。
**重要**  
雖然目錄儲存貯體會儲存在單一本機區域內的多部裝置，但不會以備援方式跨本機區域儲存資料。

1. 針對**儲存貯體名稱**，輸入您的目錄儲存貯體的名稱。

   如需目錄儲存貯體命名規則的詳細資訊，請參閱[一般用途儲存貯體命名規則](bucketnamingrules.md)。當您使用主控台建立目錄儲存貯體時，您提供的基本名稱會自動新增字尾。此字尾包含您所選擇本機區域的區域 ID。

   建立儲存貯體後，便無法變更其名稱。
**重要**  
請勿在儲存貯體名稱中包含敏感資訊，例如帳號。在指向儲存貯體中之物件的 URL 中，會顯示儲存貯體名稱。

1. 在**物件擁有權**下，會自動啟用**儲存貯體擁有者強制執行**設定，並停用所有存取控制清單 (ACL)。對於目錄儲存貯體，ACL 已停用且無法啟用。

   啟用**儲存貯體擁有者強制執行**時，儲存貯體擁有者會自動擁有並完全控制儲存貯體中的每個物件。ACL 不再影響 S3 儲存貯體中資料的存取許可。儲存貯體單獨使用政策來定義存取控制。Amazon S3 中的大多數新式使用案例不再需要使用 ACL。如需詳細資訊，請參閱[控制物件的擁有權並停用儲存貯體的 ACL](about-object-ownership.md)。

1. 在**此儲存貯體的「封鎖公開存取」設定**下，會自動啟用您目錄儲存貯體的所有封鎖公開存取設定。您無法修改目錄儲存貯體的這些設定。如需封鎖公開存取的詳細資訊，請參閱 [封鎖對 Amazon S3 儲存體的公開存取權](access-control-block-public-access.md)。

1. 在**預設加密**下，目錄儲存貯體預設會**使用 Amazon S3 受管金鑰的伺服器端加密 (SSE-S3)** 來加密資料。您也可以選擇使用 ** AWS Key Management Service 金鑰的伺服器端加密 (SSE-KMS) 來加密**目錄儲存貯體中的資料。

1. 選擇**建立儲存貯體**。

   建立儲存貯體之後，您可以新增檔案和資料夾至儲存貯體。如需詳細資訊，請參閱[使用目錄儲存貯體中的物件](directory-buckets-objects.md)。

## 使用 AWS CLI
<a name="create-directory-bucket-lz-cli"></a>

此範例示範如何使用 AWS CLI在本機區域中建立目錄儲存貯體。若要使用此命令，請以您自己的資訊取代*使用者輸入預留位置*。

建立目錄儲存貯體時，您必須提供組態詳細資訊，並使用下列命名慣例：`bucket-base-name--zone-id--x-s3`。

```
aws s3api create-bucket
--bucket bucket-base-name--zone-id--x-s3
--create-bucket-configuration 'Location={Type=LocalZone,Name=local-zone-id},Bucket={DataRedundancy=SingleLocalZone,Type=Directory}'
--region parent-region-code
```

如需本機區域 ID 和父區域代碼的詳細資訊，請參閱[Local Zones 中目錄儲存貯體的概念](s3-lzs-for-directory-buckets.md)。如需 AWS CLI 命令的詳細資訊，請參閱AWS CLI 命令參考**中的 [create-bucket](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/create-bucket.html)。

## 使用 AWS SDKs
<a name="create-directory-bucket-lz-sdks"></a>

------
#### [ SDK for Go ]

此範例示範如何使用 適用於 Go 的 AWS SDK在本機區域中建立目錄儲存貯體。

**Example**  

```
var bucket = "bucket-base-name--zone-id--x-s3" // The full directory bucket name

func runCreateBucket(c *s3.Client) {
    resp, err := c.CreateBucket(context.Background(), &s3.CreateBucketInput{
        Bucket: &bucket,
        CreateBucketConfiguration: &types.CreateBucketConfiguration{
            Location: &types.LocationInfo{
                Name: aws.String("local-zone-id"),
                Type: types.LocationTypeLocalZone,
            },  
            Bucket: &types.BucketInfo{
                DataRedundancy: types.DataRedundancySingleLocalZone,
                Type:           types.BucketTypeDirectory,
            },  
        },  
    })  
    var terr *types.BucketAlreadyOwnedByYou
    if errors.As(err, &terr) {
        fmt.Printf("BucketAlreadyOwnedByYou: %s\n", aws.ToString(terr.Message))
        fmt.Printf("noop...\n") // No operation performed, just printing a message
        return
    }   
    if err != nil {
        log.Fatal(err)
    }   

    fmt.Printf("bucket created at %s\n", aws.ToString(resp.Location))
}
```

------
#### [ SDK for Java 2.x ]

此範例示範如何使用 AWS SDK for Java 2.x在本機區域中建立目錄儲存貯體。

**Example**  

```
public static void createBucket(S3Client s3Client, String bucketName) {

    //Bucket name format is {base-bucket-name}--{local-zone-id}--x-s3
    //example: doc-example-bucket--local-zone-id--x-s3 is a valid name for a directory bucket created in a Local Zone.

    CreateBucketConfiguration bucketConfiguration = CreateBucketConfiguration.builder()
             .location(LocationInfo.builder()
                     .type(LocationType.LOCAL_ZONE)
                     .name("local-zone-id").build()) //this must match the Local Zone ID in your bucket name
             .bucket(BucketInfo.builder()
                    .type(BucketType.DIRECTORY)
                    .dataRedundancy(DataRedundancy.SINGLE_LOCAL_ZONE)
                    .build()).build();
    try {
    
             CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).createBucketConfiguration(bucketConfiguration).build();
             CreateBucketResponse response = s3Client.createBucket(bucketRequest);
             System.out.println(response);
    } 
    
    catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
         }
    }
```

------
#### [ 適用於 JavaScript 的 AWS SDK ]

此範例示範如何使用 適用於 JavaScript 的 AWS SDK在本機區域中建立目錄儲存貯體。

**Example**  

```
// file.mjs, run with Node.js v16 or higher
// To use with the preview build, place this in a folder 
// inside the preview build directory, such as /aws-sdk-js-v3/workspace/

import { S3 } from "@aws-sdk/client-s3";

const region = "parent-region-code";
const zone = "local-zone-id";
const suffix = `${zone}--x-s3`;

const s3 = new S3({ region });

const bucketName = `bucket-base-name--${suffix}`; // Full directory bucket name

const createResponse = await s3.createBucket( 
    { Bucket: bucketName, 
      CreateBucketConfiguration: {Location: {Type: "LocalZone", Name: "local-zone-id"},
      Bucket: { Type: "Directory", DataRedundancy: "SingleLocalZone" }}
    } 
   );
```

------
#### [ 適用於 .NET 的 SDK ]

此範例示範如何使用 適用於 .NET 的 SDK在本機區域中建立目錄儲存貯體。

**Example**  

```
using (var amazonS3Client = new AmazonS3Client())
{
    var putBucketResponse = await amazonS3Client.PutBucketAsync(new PutBucketRequest
    {

       BucketName = "bucket-base-name--local-zone-id--x-s3",
       PutBucketConfiguration = new PutBucketConfiguration
       {
         BucketInfo = new BucketInfo { DataRedundancy = DataRedundancy.SingleLocalZone, Type = BucketType.Directory },
         Location = new LocationInfo { Name = "local-zone-id", Type = LocationType.LocalZone }
       }
     }).ConfigureAwait(false);
}
```

------
#### [ SDK for PHP ]

此範例示範如何使用 AWS SDK for PHP在本機區域中建立目錄儲存貯體。

**Example**  

```
require 'vendor/autoload.php';

$s3Client = new S3Client([

    'region'      => 'parent-region-code',
]);


$result = $s3Client->createBucket([
    'Bucket' => 'bucket-base-name--local-zone-id--x-s3',
    'CreateBucketConfiguration' => [
        'Location' => ['Name'=> 'local-zone-id', 'Type'=> 'LocalZone'],
        'Bucket' => ["DataRedundancy" => "SingleLocalZone" ,"Type" => "Directory"]   ],
]);
```

------
#### [ SDK for Python ]

此範例示範如何使用 適用於 Python (Boto3) 的 AWS SDK在本機區域中建立目錄儲存貯體。

**Example**  

```
import logging
import boto3
from botocore.exceptions import ClientError

def create_bucket(s3_client, bucket_name, local_zone):
    '''
    Create a directory bucket in a specified Local Zone

    :param s3_client: boto3 S3 client
    :param bucket_name: Bucket to create; for example, 'bucket-base-name--local-zone-id--x-s3'
    :param local_zone: String; Local Zone ID to create the bucket in
    :return: True if bucket is created, else False
    '''

    try:
        bucket_config = {
                'Location': {
                    'Type': 'LocalZone',
                    'Name': local_zone
                },
                'Bucket': {
                    'Type': 'Directory', 
                    'DataRedundancy': 'SingleLocalZone'
                }
            }
        s3_client.create_bucket(
            Bucket = bucket_name,
            CreateBucketConfiguration = bucket_config
        )
    except ClientError as e:
        logging.error(e)
        return False
    return True


if __name__ == '__main__':
    bucket_name = 'BUCKET_NAME'
    region = 'parent-region-code'
    local_zone = 'local-zone-id'
    s3_client = boto3.client('s3', region_name = region)
    create_bucket(s3_client, bucket_name, local_zone)
```

------
#### [ SDK for Ruby ]

此範例示範如何使用 適用於 Ruby 的 AWS SDK在本機區域中建立目錄儲存貯體。

**Example**  

```
s3 = Aws::S3::Client.new(region:'parent-region-code')
s3.create_bucket(
  bucket: "bucket-base-name--local-zone-id--x-s3",
  create_bucket_configuration: {
    location: { name: 'local-zone-id', type: 'LocalZone' },
    bucket: { data_redundancy: 'SingleLocalZone', type: 'Directory' }
  }
)
```

------