ECS:如何設定和使用 S3 AWS CLI

Summary: 使用 AWS CLI 測試客戶在使用 AWS CLI 搭配 ECS 以確認功能和相容性的情境時,請務必參閱 ECS 資料存取指南,瞭解支援 ECS AWS S3 的 API 呼叫。

This article applies to This article does not apply to This article is not tied to any specific product. Not all product versions are identified in this article.

Instructions

AWS 命令行介面 (AWS CLI) 是 AWS 的工具,可用於測試 S3 高級命令,而 s3 API 級 (s3api)

高級 aws s3 (外部連結) 命令支援常見的儲存桶操作,例如創建、列出和刪除存儲桶。如何下載和安裝工具,所有支援的高級命令都可以在命令集中使用命令取得 aws s3api help

S3Uri:表示 S3 物件、前置碼或儲存桶的位置 - 必須以表單寫入 s3://mybucket/mykey 哪裡 mybucket 是否指定 S3 儲存桶, mykey 是指定的 S3 金鑰。路徑參數必須以 s3://

API 層級的 aws s3api (外部連結) 命令包含在 s3api 建議集中。這些 S3 API 命令提供對儲存桶元數據的訪問,例如存儲桶 ACL、存儲桶策略、生命週期策略等,所有支援的 API 調用都可以使用命令在命令集中獲取 aws s3api help)

  • 默認情況下,AWS CLI 在上傳過程中自動使用最多 10 個線程來上傳檔或部分
  • AWS CLI 會自動使用multipart_threshold上傳的檔案 >= 8 MB 檔案大小,multipart_chunksize

下載並安裝說明。

  1. 版本 https://github.com/aws/aws-cli/releases (外部連結)
  2. 下載並安裝 AWS CLI 套裝
# curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
  1. 解壓縮套裝
# unzip awscli-bundle.zip
  1. 執行安裝命令
# sudo /awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
  1. 確認版本
# aws version
Output:
      aws-cli/2.0.33 Python/3.7.3 Linux/4.15.0-109-generic botocore/2.0.0dev37

設定 AWS CLI

本節說明使用 ECS 物件使用者設定 AWS CLI 以搭配 ECS S3 使用的基本步驟。本節假設已在 ECS 上建立容器和物件使用者。執行此命令時,登入資料和組態檔案隨即更新 aws configure

登入資料檔案位於此處 ~/.aws/credentials) 憑據檔存儲使用者配置檔詳細資訊(訪問金鑰 ID 和秘密存取金鑰)、配置檔儲存區域和輸出格式詳細資訊。

必填:
  • 存取金鑰 ID:ECS 物件使用者
  • 秘密存取金鑰:祕密金鑰
步驟:
  1. 此範例建立設定檔名稱mc_s3
# aws configure --profile mc_s3
Prompt:

    AWS Access Key ID [None]: mc_s3
    AWS Secret Access Key [None]: 1guFemRLSgqFau6uKzIVAZJu5+PS+S8qO7rvEsi7
    Default region name [None]: 
    Default output format [None]: json
  1. 列出設定檔所有設定檔 (AWS CLI v2 中可用的清單屬性
# aws configure list-properties
Output:
    mc_s3
    ad_uid1
# aws configure list
Output:
		  Name                    Value             Type    Location
		  ----                    -----             ----    --------
	   profile                <not set>             None    None
	access_key     ****************c_s3 shared-credentials-file    
	secret_key     ****************Esi7 shared-credentials-file    
		region                               config-file    ~/.aws/config
  1. 特定於清單的設定檔
# aws configure get aws_access_key_id --profile mc_s3
Output:
      mc_s3
Dealing with SSL connections and error on self signed certificate.  ( [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1056))
  1. 如果在 9021 上使用命令,AWS CLI 選項 --no-verify-ssl 是略過自我簽署憑證錯誤所必需的。
# aws --profile mc_s3 --endpoint=https://ecshop:9021 s3api list-buckets
Output:
     SSL validation failed for https://ecshop:9021/mc_s3_bkt?acl [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1056)
  • 執行命令 --no-verify 略過此錯誤的選項
# aws --profile mc_s3 --endpoint=https://ecshop:9021 s3api list-buckets --no-verify-ssl --output text
Output:
      
    /usr/local/aws-cli/v2/2.0.33/dist/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'ecshop'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings

    BUCKETS 2020-02-24T21:24:29.440000+00:00        mc_s3_bkt
    BUCKETS 2020-05-06T17:23:48.870000+00:00        mc_s3_bkt2_backup
    BUCKETS 2020-05-06T21:16:13.344000+00:00        mc_s3_bkt_nfs
    OWNER   mc_s3   mc_s3
  • 第二個選項是下載儲存為 PEM 格式的 ECS 自我簽署憑證,並使用 --ca-bundle 選項
  • 使用 OpenSSL 取得 ECS 憑證,以存取連接埠 9021 上的物件資料,並使用您最愛的編輯器複製憑證內容,並儲存包括憑證標頭,每個範例如下。
# openssl s_client -connect ecshop:9021
其中 ecshop 是 ECS 節點的 IP 或主機名稱,xxxxxx 應該是環境中的完整憑證。
Output:

    -----BEGIN CERTIFICATE----- 
    Xxxxxxxxx
    xxxxxxxxx
    ...
    ...
    -----END CERTIFICATE-----
  1. 在表格中列出貯體,使用 ca-bundle 提供人性化選項,其中憑證會儲存在 PEM 檔案名稱中 ecshop_cert_pem 
# aws --profile mc_s3 --endpoint=https://ecshop:9021 s3api list-buckets  --ca-bundle ecshop_cert.pem  --output table
Output:
    -------------------------------------------------------------
    |                        ListBuckets                        |
    +-----------------------------------------------------------+
    ||                         Buckets                         ||
    |+-----------------------------------+---------------------+|
    ||           CreationDate            |        Name         ||
    |+-----------------------------------+---------------------+|
    ||  2020-02-24T21:24:29.440000+00:00 |  mc_s3_bkt          ||
    ||  2020-05-06T17:23:48.870000+00:00 |  mc_s3_bkt2_backup  ||
    ||  2020-05-06T21:16:13.344000+00:00 |  mc_s3_bkt_nfs      ||
    |+-----------------------------------+---------------------+|
    ||                          Owner                          ||
    |+-----------------------------------+---------------------+|
    ||            DisplayName            |         ID          ||
    |+-----------------------------------+---------------------+|
    ||  mc_s3                            |  mc_s3              ||
    |+-----------------------------------+---------------------+|

基本 S3 作業

https://docs.aws.amazon.com/cli/latest/userguide/cli-services-s3-commands.html (外部連結)
效能調整
  • max_concurrent_requests - 最大併發請求數
  • max_queue_size - 工作佇列中的最大工作數
  • multipart_threshold - CLI 用於多部分傳輸單個檔的大小閾值。
  • multipart_chunksize - 使用多部分傳輸時,這是 CLI 用於個別檔案的多部分傳輸的區塊大小。
  • max_bandwidth - 向 Amazon S3 上傳和從 Amazon S3 上傳和下載資料所使用的最大頻寬。
儲存在設定檔中作為範例:
# cat .aws/config
Output:
    [profile mc_s3]
    output = json
    s3 =
            endpoint_url = http://ecshop:9020

              max_concurrent_requests = 20
              max_queue_size = 1000
              multipart_threshold = 64MB
              multipart_chunksize = 16MB
              max_bandwidth = 50MB/s
              addressing_style = auto 
使用 aws configure set 命令以設定參數以調整上傳速度
$ aws configure set default.s3.max_concurrent_requests 20
$ aws configure set default.s3.max_queue_size 10000
$ aws configure set default.s3.multipart_threshold 64MB
$ aws configure set default.s3.multipart_chunksize 16MB
$ aws configure set default.s3.max_bandwidth 50MB/s
$ aws configure set default.s3.addressing_style auto
步驟: 
  1. 建立儲存桶,使用 s3 mb command
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 mb s3://s3_bkt
Output:
    make_bucket: s3_bkt
  1. 列出儲存桶使用 s3 lss3api list-buckets 命令集列出儲存桶,用於儲存桶中的任何其他操作(在 S3 中稱為“前綴”)(外部連結)
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 ls
Output:
   2020-07-29 20:34:24 s3_bkt

  #  aws --profile mc_s3 --endpoint http://ecshop:9020 s3api list-buckets
#  aws --profile mc_s3 --endpoint http://ecshop:9020 s3api list-buckets
Output:		
	 aws --profile mc_s3 --endpoint http://ecshop:9020 s3api list-buckets
	{
		"Buckets": [
			{
				"Name": "mc_s3_bkt",
				"CreationDate": "2020-02-24T21:24:29.440000+00:00"
			},
			{
				"Name": "mc_s3_bkt2_backup",
				"CreationDate": "2020-05-06T17:23:48.870000+00:00"
			},
			{
				"Name": "mc_s3_bkt_nfs",
				"CreationDate": "2020-05-06T21:16:13.344000+00:00"
			},
			{
				"Name": "s3_bkt",
				"CreationDate": "2020-07-30T00:34:24.147000+00:00"
			},
  1.  建立與上傳用於測試的檔案 
  • 建立 1 MB 和 4 MB 檔案範例
# dd if=/dev/zero of=1MB_output.file bs=1024 count=1024
# dd if=/dev/zero of=4MB_output.file bs=1024 count=4096
  • 建立大型檔案 2GB 的範例
# dd if=/dev/urandom of=bigfile_2GB bs=1024k count=2048
  • 上傳 1MB 檔案至儲存桶 s3_bkt
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 cp 1MB_output.file s3://s3_bkt
Output:
    upload: ./1MB_output.file to s3://s3_bkt/1MB_output.file
  1. 列出存儲桶中的檔, s3_bkt 使用設定檔名稱mc_s3使用 s3 ls,或 s3api list-objects 命令集 
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 ls s3://s3_bkt
Output:
2020-07-29 20:39:43   16.0 MiB 16MB_output.file
2020-07-29 20:37:25    1.0 MiB 1MB_output.file
2020-07-29 20:39:23    4.0 MiB 4MB_output.file
 # aws --profile mc_s3 --endpoint http://ecshop:9020 s3api list-objects --bucket s3_bkt
Output:
	{
		"Contents": [
			{
				"Key": "16MB_output.file",
				"LastModified": "2020-07-30T00:39:43.125000+00:00",
				"ETag": "\"3a2d20e2e504fe056bbaae5b4c2351fd-2\"",
				"Size": 16777216,
				"StorageClass": "STANDARD",
				"Owner": {
					"DisplayName": "mc_s3",
					"ID": "mc_s3"
				}
			},
			{
				"Key": "1MB_output.file",
				"LastModified": "2020-07-30T00:37:25.033000+00:00",
				"ETag": "\"b6d81b360a5672d80c27430f39153e2c\"",
				"Size": 1048576,
				"StorageClass": "STANDARD",
				"Owner": {
					"DisplayName": "mc_s3",
					"ID": "mc_s3"
				}
			},
  1. 列出物件版本
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api list-object-versions --bucket s3_bkt --output text
  1. 請從以下位置下載檔案,1MB_output.file s3_bkt 到本地電腦上的 /tmp 目錄。
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 cp s3://s3_bkt/1MB_output.file /tmp/
Output:
     download: s3://s3_bkt/1MB_output.file to ../../tmp/1MB_output.file
  1. 檢查 YAML 格式的檔案 1MB_output.file 的 ACL 以取得輸出的可讀性
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api get-object-acl --bucket s3_bkt --key 1MB_output.file  --output yaml
Output:
        
    Grants:
    - Grantee:
        DisplayName: mc_s3
        ID: mc_s3
        Type: CanonicalUser
      Permission: FULL_CONTROL
    Owner:
      DisplayName: mc_s3
      ID: mc_s3
  1. 檢查儲存器上的容器 ACL s3_bkt
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api get-bucket-acl --bucket s3_bkt --output yaml
Output:      
    Grants:
    - Grantee:
        DisplayName: mc_s3
        ID: mc_s3
        Type: CanonicalUser
      Permission: FULL_CONTROL
    Owner:
      DisplayName: mc_s3
      ID: mc_s3
  1. 檢查貯體版本或啟用貯體版本控制
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api get-bucket-versioning --bucket s3_bkt
Output:

    {
        "Status": "Enabled"
    }
  1. 使用 s3api API 層級啟動大型檔案的分段上傳 
  1. 建立 5GB 檔案:
# time dd if=/dev/urandom of=bigfile_5GB bs=1024k count=5096
  1. 開始上傳
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api create-multipart-upload --bucket s3_bkt --key bigfile_5GB
Output:

    {
        "Bucket": "s3_bkt",
        "Key": "bigfile_5GB",
        "UploadId": "27cb6c45ab5c4c838fb5893263d871d3"
    }
  1. 列出不完整的分段檔案上傳 https://aws.amazon.com/premiumsupport/knowledge-center/s3-multipart-upload-cli/ (外部連結)
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api list-multipart-uploads --bucket s3_bkt
Output:

	{
	    "Uploads": [
	        {
	            "UploadId": "27cb6c45ab5c4c838fb5893263d871d3",
	            "Key": "bigfile_5GB",
	            "Initiated": "2020-07-31T01:10:56.323000+00:00",
	            "StorageClass": "STANDARD",
	            "Owner": {
	                "DisplayName": "mc_s3",
	                "ID": "mc_s3"
	            }
	        }
	    ]
	}
  1. 改善 s3 cp 大檔的命令或存儲桶到存儲桶性能建議 要啟動分段上傳和更改併發線程,請參閱上文的性能調優部分。
  1. 執行貯體對貯體複製以複製file.txt mc_s3_bkt 的輸出傳送至 s3_bkt 使用 S3api 
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api copy-object --copy-source mc_s3_bkt/file.txt --key file.txt --bucket s3_bkt
Output:
{
    "VersionId": "1596159769267",
    "CopyObjectResult": {
        "ETag": "\"c789e490a90359de2bd3b09d7e957cfd-128\"",
        "LastModified": "2020-07-31T01:42:49.267000+00:00"
    }
}
  1. 列出儲存桶中的檔案(物件或金鑰) 
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 ls s3://s3_bkt/file.txt --human-readable  summarize
Output:
      2020-07-30 21:42:49    1.0 GiB file.txt
  1. 從以下位置複製檔案: S3_bkt 的輸出傳送至 mc_s3_bkt 使用 S3 CP 高階命令集
以下 cp 命令將單個 s3 物件複製到指定的儲存桶和金鑰:
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 cp s3://s3_bkt/file.txt s3://mc_s3_bkt/file2.txt
       Output:
         copy: s3://s3_bkt/file.txt to s3://mc_s3_bkt/file2.txt
  1. 檢查儲存器是否已啟用版本控制 s3_bkt
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api get-bucket-versioning --bucket s3_bkt
Output: 

      {
          "Status": "Enabled"
      }
  1. 刪除儲存桶使用 s3 rb 命令 (命令沒有輸出)
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3 rm s3://s3_bkt2
  1. 以下範例刪除儲存桶中的所有物件和子資料夾,然後刪除儲存桶。(如果啟用了版本控制,則此命令不會刪除版本物件(查看LDS,生命週期策略)
# aws s3 rb s3://bucket-name --force
  1. 啟用儲存器的版本控制 s3_bkt 使用 s3api API 層級命令集 (命令尚未輸出)
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api put-bucket-versioning --bucket s3_bkt --versioning-configuration Status=Enabled 
  1. 套用生命週期原則產生範本 (參考 ECS 資料存取指南) AWS CLI 生命週期原則需要 json 格式
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api put-bucket-lifecycle-configuration --generate-cli-skeleton input --bucket s3_bkt
  1. 在儲存器上套用儲存器使用時間原則,如需更多詳細資料,請參閱 ECS 資料存取指南
建立原則 AbortIncompleteMultipartUpload, NoncurrentVersionExpiration, and ExpiredObjectDeleteMarker and NoncurrentDays 7天后到期。
#  aws --profile mc_s3 --endpoint http://ecshop:9020 s3api put-bucket-lifecycle --bucket s3_bkt --lifecycle-configuration file://s3_bkt_lifecycle.json
Output:

{
   "Rules": [
   {
      "ID": "expire-non-current-and-dmarkers-and-mpu",
      "Status": "Enabled",
      "Prefix": "/",
      "Expiration": {
         "ExpiredObjectDeleteMarker": true
      },
      "AbortIncompleteMultipartUpload": {
         "DaysAfterInitiation": 7
      },
      "NoncurrentVersionExpiration": {
         "NoncurrentDays": 7
      }
   }
   ]
}
  1. GET 生命週期原則
# aws --profile mc_s3 --endpoint http://ecshop:9020 s3api get-bucket-lifecycle --bucket s3_bkt
Output:

{
    "Rules": [
        {
            "Expiration": {
                "Days": 10
            },
            "ID": "DeleteVersion-musa",
            "Prefix": "",
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 10
            }
        }
    ]
}
  1. 使用鍵值對進行物件標記 
  1. 寫一個檔。 
$ aws s3 cp file.txt s3://mc_s3_bkt/aws/tag.txt  --profile mc_s3_emea --endpoint  http://emea:9020
upload: ./file.txt to s3://mc_s3_bkt/aws/tag.txt
  1. 新增標籤 (如果成功,此命令尚未輸出。
    單行命令:
$ aws s3api put-object-tagging --profile mc_s3_emea --bucket mc_s3_bkt --key aws/tag.txt --tagging '{"TagSet": [{ "Key": "Product", "Value": "ECS" }]}' --endpoint-url http://emea:9020

多行複製和貼上: 

$ aws s3api put-object-tagging \
	 --profile mc_s3_emea \
     --bucket mc_s3_bkt \
     --key aws/tag.txt \
     --tagging '{"TagSet": [{ "Key": "Product", "Value": "ECS" }]}' \
    --endpoint-url http://emea:9020
  1. 獲取物件標記。
$ aws s3api get-object-tagging --profile mc_s3_emea --bucket mc_s3_bkt --key aws/tag.txt --endpoint http://emea:9020

{
    "TagSet": [
        {
            "Key": "Product",
            "Value": "ECS"
        }
    ]
}
  1. 新增多個標籤值 'key:value' - 簡寫形式
$ aws s3api put-object-tagging --profile mc_s3_emea --bucket mc_s3_bkt --key aws/tag5.txt --tagging '{"TagSet": [{ "Key": "Product", "Value": "ECS 3.6.1.2" },{"Key": "Company", "Value": "Dell Technologies"}]}' --endpoint-url http://emea:9020
  1. 獲取物件標記。
$ aws s3api get-object-tagging --profile mc_s3_emea --bucket mc_s3_bkt --key aws/tag5.txt
{
    "TagSet": [
        {
            "Key": "Product",
            "Value": "ECS 3.6.1.2"
        },
        {
            "Key": "Company",
            "Value": "Dell Technologies"
        }
    ]
}
  1. S3 物件鎖定
ECS 允許您透過 S3 物件鎖定使用單寫多讀 (WORM) 模型來儲存物件。此功能可防止物件在指定時間內或無限期地被刪除或覆蓋。
 
注意:
  • ECS S3 物件鎖定功能僅支援啟用了版本控制的容器。
  • 物件鎖定沒有 ECS 使用者介面。可透過 ECS 物件鎖定 API 存取。
  • 鎖定的物件受到生命周期刪除保護
  • 容器無法啟用 檔案系統
  • 物件鎖定適用於 IAM 使用者,而非舊版使用者 
  • 在貯體上啟用物件鎖定,自動啟用版本控制 
  • 如果 ADO 已啟用,則需要修補程式,並以 ECS 3.7 中解決。請參閱文章 ECS:停用 ECS 上的 IBM Content Manager 物件鎖定功能


在儲存桶上設置物件鎖定。
使用 PUT-object-lock-configuration 選項會搭配 AWS CLI 使用。

請參閱:https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-lock-overview.html
put-object-lock-configuration — AWS CLI 1.22.24 命令參考 (amazon.com) (外部連結)

  • 在儲存桶上設定物件鎖定設定
The following put-object-lock-configuration example sets a 1-day object lock on the specified bucket.

$ aws s3api put-object-lock-configuration \
	--profile iam1 \
    --bucket iam-bucket \
    --object-lock-configuration '{ "ObjectLockEnabled": "Enabled", "Rule": { "DefaultRetention": { "Mode": "COMPLIANCE", "Days": 1 }}}'

此命令不產生輸出。

  • put-object-legal-hold
$ aws s3api put-object-legal-hold --endpoint http://<hostname>:<port_number> --profile iam1 --bucket iam-bucket --key file1.txt --legal-hold "Status=ON"
where:
	--profile: name of profile is profile confirmed in .aws credentials file
	--bucket: Bucket name
	--key: Object name
	--legal-hold: ON or OFF
	--version-id: specifies the version id of the object for Versioned 
	--endpoint: ECS IP and port 9020 or 90201
  • get-object-legal-hold
$ aws s3api get-object-legal-hold --endpoint http://<hostname>:<port_number> --profile iam1 --bucket iam-bucket --key file1.txt
where:
	--bucket: Bucket name
	--key: Object name
	--version-id: specifies the version id of the object
	--endpoint: ECS IP and port 9020 or 90201

Affected Products

Elastic Cloud Storage

Products

ECS Appliance, ECS Appliance Hardware Series, ECS Appliance Software with Encryption, Elastic Cloud Storage
Article Properties
Article Number: 000020564
Article Type: How To
Last Modified: 02 Oct 2025
Version:  6
Find answers to your questions from other Dell users
Support Services
Check if your device is covered by Support Services.