ECS:如何配置和使用 S3 AWS CLI

Summary: 使用 AWS CLI 测试客户使用 AWS CLI 和 ECS 以确认功能和兼容性的情况时,请始终参考 ECS AWS S3 支持的 API 调用的 ECS 数据访问指南。

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 Command-Line Interface (AWS CLI) 是 AWS 提供的一款工具,可用于测试 S3 高级命令和 s3 API 级 (s3api)

高级命令,aws s3 (外部链接)命令支持常见的存储桶作,例如创建、列出和删除存储桶。如何下载和安装该工具,可以使用以下命令在命令集中获取所有受支持的高级命令: aws s3api help

S3Uri:表示 S3 对象、前缀或存储区的位置 — 这必须采用如下格式编写 s3://mybucket/mykey 其中有个数字 mybucket 是 t 指定 S3 存储桶, mykey 是指定的 S3 密钥。path 参数必须以 s3://

API 级 aws s3api (外部链接)命令包含在 s3api 推荐集中。这些 S3 API 命令提供对存储区元数据(如存储区 ACL、存储区策略、生命周期策略等)的访问,所有受支持的 API 调用都可以使用命令在命令集中获取 aws s3api help

  • 默认情况下,AWS CLI 在上传过程中会自动使用最多 10 个线程上传文件或分段
  • AWS CLI 自动对文件 >= 8 MB 文件大小使用multipart_threshold上传,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 中可用的 list-properties)
# 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 在端口 9021 上获取用于对象数据访问的 ECS 证书,并使用您喜欢的编辑器复制证书内容并保存,包括下面示例中的证书标头。
# 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 数据所消耗的最大带宽。
保存在配置文件中作为示例:
# 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.files3_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:为 IBM Content Manager 禁用 ECS 上的对象锁定功能


在存储区上设置对象锁定。
Use 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.