

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

# 將物件上傳至目錄儲存貯體
<a name="directory-buckets-objects-upload"></a>

建立 Amazon S3 目錄儲存貯體之後，您可以將物件上傳至其中。下列範例示範如何使用 S3 主控台和 AWS SDK 將物件上傳至目錄儲存貯體。如需使用 S3 Express One Zone 執行大量物件上傳操作的資訊，請參閱[物件管理](directory-bucket-high-performance.md#s3-express-features-object-management)。

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

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

1. 在左側導覽窗格中，選擇**目錄儲存貯體**。

1. 選擇您要上傳資料夾和檔案的目的地儲存貯體名稱。

1. 在**物件**清單中，選擇**上傳**。

1. 在**上傳**頁面上，執行下列任一步驟：
   + 將檔案和資料夾拖放至上傳區域。
   + 選擇**新增檔案**或**新增資料夾**，選擇要上傳的檔案或資料夾，然後選擇**開啟**或**上傳**。

1. 在**檢查總和**下，選擇您要使用的**檢查總和函數**。

   (選用) 如果您上傳的單一物件大小小於 16 MB，您也可以指定預先計算的檢查總和值。當您提供預先計算的值時，Amazon S3 會將該值與使用所選檢查總和函數計算的值進行比較。如果值不相符，則不會開始上傳。

1. **許可**和**屬性**區段中的選項會自動設定為預設設定，且無法修改。封鎖公開存取會自動啟用，且無法針對目錄儲存貯體啟用 S3 版本控制和 S3 物件鎖定。

   (選用) 如果您要以金鑰/值對的形式將中繼資料新增至物件，請展開**屬性**區段，然後在**中繼資料**區段中選擇**新增中繼資料**。

1. 若要上傳列出的檔案和資料夾，請選擇**上傳**。

   Amazon S3 會上傳您的物件和資料夾。上傳完成後，您可以在**上傳：狀態**頁面上看到成功訊息。

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

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

**Example**  

```
public static void putObject(S3Client s3Client, String bucketName, String objectKey, Path filePath) {
       //Using File Path to avoid loading the whole file into memory
       try {
           PutObjectRequest putObj = PutObjectRequest.builder()
                   .bucket(bucketName)
                   .key(objectKey)
                   //.metadata(metadata)
                   .build();
           s3Client.putObject(putObj, filePath);               
           System.out.println("Successfully placed " + objectKey +" into bucket "+bucketName);
                                              
       }
       
       catch (S3Exception e) {
           System.err.println(e.getMessage());
           System.exit(1);
       }
}
```

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

**Example**  

```
import boto3
import botocore
from botocore.exceptions import ClientError
    
    
def put_object(s3_client, bucket_name, key_name, object_bytes):
    """  
    Upload data to a directory bucket.
    :param s3_client: The boto3 S3 client
    :param bucket_name: The bucket that will contain the object
    :param key_name: The key of the object to be uploaded
    :param object_bytes: The data to upload
    """
    try:
        response = s3_client.put_object(Bucket=bucket_name, Key=key_name,
                             Body=object_bytes)
        print(f"Upload object '{key_name}' to bucket '{bucket_name}'.") 
        return response
    except ClientError:    
        print(f"Couldn't upload object '{key_name}' to bucket '{bucket_name}'.")
        raise

def main():
    # Share the client session with functions and objects to benefit from S3 Express One Zone auth key
    s3_client = boto3.client('s3')
    # Directory bucket name must end with --zone-id--x-s3
    resp = put_object(s3_client, 'doc-bucket-example--use1-az5--x-s3', 'sample.txt', b'Hello, World!')
    print(resp)

if __name__ == "__main__":
    main()
```

------

## 使用 AWS CLI
<a name="directory-upload-object-cli"></a>

下列 `put-object` 範例命令示範如何使用 AWS CLI 將物件上傳至 Amazon S3。若要執行此命令，請以您自己的資訊取代 `user input placeholders`。

```
aws s3api put-object --bucket bucket-base-name--zone-id--x-s3 --key sampleinut/file001.bin --body bucket-seed/file001.bin
```

如需詳細資訊，請參閱《AWS CLI 命令參考》**中的 [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/put-object.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/s3api/put-object.html)。

**Topics**
+ [搭配目錄儲存貯體使用分段上傳](s3-express-using-multipart-upload.md)