本文档 AWS CLI 仅适用于版本 1。 有关版本 2 的文档 AWS CLI,请参阅 版本 2 用户指南 。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在中使用高级别 (s3) 命令 AWS CLI
本主题介绍一些命令,可用于在 AWS CLI中通过
aws
s3
命令管理 Amazon S3 存储桶和对象。有关本主题未涵盖的命令和其他命令示例,请参阅
《AWS CLI 参考》中的
aws
s3
命令。
高级别
aws s3
命令简化了 Amazon S3 对象管理。使用这些命令,您能够在 Amazon S3 自身中管理其内容以及使用本地目录管理这些内容。
要运行
s3
命令,您需要:
-
安装和配置 AWS CLI。有关更多信息,请参阅 安装、更新和卸载 AWS CLI 和 的身份验证和访问凭证 AWS CLI 。
-
您使用的配置文件必须具有允许示例执行 AWS 操作的权限。
需了解如下 Amazon S3 术语:
大型对象上载
当您使用
aws s3
命令将大型对象上传到 Amazon S3 存储桶时, AWS CLI 会自动执行分段上传。使用这些aws s3
命令时,您无法恢复失败的上传操作。如果由于超时而导致分段上传失败,或者如果您在中手动取消 AWS CLI,则 AWS CLI 会停止上传并清理所有已创建的文件。此过程可能耗时数分钟。
如果使用 kill 命令或者由于系统故障而取消了分段上传或清理过程,则创建的文件将保留在 Amazon S3 存储桶中。
分段复制中的文件属性和标签
当您在
aws s3
命名空间中使用 AWS CLI 版本 1 版本的命令将文件从一个 Amazon S3 存储桶位置复制到另一个 Amazon S3 存储桶位置,并且该操作使用分 段复制 时,源对象中的任何文件属性都不会复制到目标对象。创建存储桶
使用
s3 mb
命令创建存储桶。存储桶名称必须是 全球 唯一的(在所有 Amazon S3 中都是唯一的),并且应该DNS合规。存储桶名称可以包含小写字母、数字、连字符和点号。存储桶名称只能以字母或数字开头和结尾,连字符或点号后不能跟点号。
$
aws s3 mb <target> [--options]
以下示例将创建
s3://amzn-s3-demo-bucket
存储桶。$
aws s3 mb s3://
amzn-s3-demo-bucket
列出存储桶和对象
要列出存储桶、文件夹或对象,请使用
s3 ls
命令。使用不带目标或选项的命令时,将会列出所有存储桶。$
aws s3 ls <target> [--options]
有关与此命令一起使用的一些常见选项以及相关示例,请参阅 s3 命令的常用选项 。有关可用选项的完整列表,请参阅 《AWS CLI 命令参考》中的
s3
删除存储桶
要删除存储桶,请使用
s3 rb
命令。$
aws s3 rb <target> [--options]
以下示例删除
s3://amzn-s3-demo-bucket
存储桶。$
aws s3 rb
s3://amzn-s3-demo-bucket
默认情况下,存储桶必须为空,此操作才能成功。要删除不为空的存储桶,您必须包含
--force
选项。如果您使用的是受版本控制的存储桶,即其中包含以前删除“但仍保留”的对象,则此命令 不 允许您删除该存储桶。您必须先删除所有内容。以下示例命令删除存储桶中的所有对象和前缀,然后删除存储桶本身。
$
aws s3 rb
s3://amzn-s3-demo-bucket
--force要删除存储桶或本地目录中的对象,请使用
s3 rm
命令。$
aws s3 rm <target> [--options]
有关与此命令一起使用的一些常见选项以及相关示例,请参阅 s3 命令的常用选项 。有关选项的完整列表,请参阅 《AWS CLI 命令参考》中的
s3
使用
s3 mv
命令可从存储桶或本地目录中移动对象。该s3 mv
命令将源对象或文件复制到指定的目标,然后删除源对象或文件。$
aws s3 mv <source> <target> [--options]
有关与此命令一起使用的一些常见选项以及相关示例,请参阅 s3 命令的常用选项 。有关可用选项的完整列表,请参阅 《AWS CLI 命令参考》中的
s3
以下示例将所有对象从
s3://amzn-s3-demo-bucket/example
移动到s3://amzn-s3-demo-bucket/
。$
aws s3 mv s3://amzn-s3-demo-bucket/example s3://amzn-s3-demo-bucket/
以下示例使用
s3 mv
命令,将本地文件从当前工作目录移动到 Amazon S3 存储桶。$
aws s3 mv filename.txt s3://amzn-s3-demo-bucket
以下示例将文件从 Amazon S3 存储桶移动到当前工作目录,其中
./
指定当前的工作目录。$
aws s3 mv s3://amzn-s3-demo-bucket/filename.txt ./
使用
s3 cp
命令可从存储桶或本地目录复制对象。$
aws s3 cp <source> <target> [--options]
您可以使用短划线参数,将文件流式传输到标准输入 (
stdin
) 或标准输出 (stdout
)。警告
如果你使用的是 PowerShell,shell 可能会更改 a 的编码,CRLF或者在管道输入或输出或重定向输出中添加 a CRLF。
s3 cp
命令使用以下语法,将文件流从stdin
上传到指定的存储桶。$
aws s3 cp - <target> [--options]
s3 cp
命令使用以下语法下载stdout
的 Amazon S3 文件流。$
aws s3 cp <target> [--options] -
有关与此命令一起使用的一些常见选项以及相关示例,请参阅 s3 命令的常用选项 。有关选项的完整列表,请参阅 《AWS CLI 命令参考》中的
s3
sync
命令同步一个存储桶与一个目录中的内容,或者同步两个存储桶中的内容。通常,s3 sync
在源和目标之间复制缺失或过时的文件或对象。不过,您还可以提供--delete
选项来从目标中删除源中不存在的文件或对象。$
aws s3 sync <source> <target> [--options]
有关与此命令一起使用的一些常见选项以及相关示例,请参阅 s3 命令的常用选项 。有关选项的完整列表,请参阅 《AWS CLI 命令参考》中的
s3 sync
。以下示例将名为 amzn-s3-demo-bucket 的存储桶中名为 p ath 的 Amazon S 3 前缀的内容与当前工作目录同步。
s3 sync
将更新与目标位置中同名文件的大小或修改时间不同的任何文件。输出显示在同步期间执行的特定操作。请注意,该操作将与MySubdirectory
递归地同步子目录s3://amzn-s3-demo-bucket/path/MySubdirectory
及其内容。$
aws s3 sync . s3://mamzn-s3-demo-bucket/path
upload: MySubdirectory\MyFile3.txt to s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt upload: MyFile2.txt to s3://amzn-s3-demo-bucket/path/MyFile2.txt upload: MyFile1.txt to s3://amzn-s3-demo-bucket/path/MyFile1.txt
下面的示例对上一示例进行了扩展,显示了如何使用
--delete
选项。// Delete local file
$
rm ./MyFile1.txt
// Attempt sync without --delete option - nothing happens
$
aws s3 sync . s3://amzn-s3-demo-bucket/path
// Sync with deletion - object is deleted from bucket
$
aws s3 sync . s3://amzn-s3-demo-bucket/path --delete
delete: s3://amzn-s3-demo-bucket/path/MyFile1.txt // Delete object from bucket
$
aws s3 rm s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt
delete: s3://amzn-s3-demo-bucket/path/MySubdirectory/MyFile3.txt // Sync with deletion - local file is deleted
$
aws s3 sync s3://amzn-s3-demo-bucket/path . --delete
delete: MySubdirectory\MyFile3.txt // Sync with Infrequent Access storage class
$
aws s3 sync . s3://amzn-s3-demo-bucket/path --storage-class STANDARD_IA
使用
--delete
选项时,--exclude
和--include
选项可以筛选要在s3 sync
操作过程中删除的文件或对象。在这种情况下,参数字符串必须指定要在目标目录或存储桶上下文中包含或排除在删除操作中的文件。下面是一个示例。Assume local directory and s3://amzn-s3-demo-bucket/path currently in sync and each contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt // Sync with delete, excluding files that match a pattern. MyFile88.txt is deleted, while remote MyFile1.txt is not.
$
aws s3 sync . s3://amzn-s3-demo-bucket/path --delete --exclude "path/MyFile?.txt"
delete: s3://amzn-s3-demo-bucket/path/MyFile88.txt // Sync with delete, excluding MyFile2.rtf - local file is NOT deleted
$
aws s3 sync s3://amzn-s3-demo-bucket/path . --delete --exclude "./MyFile2.rtf"
download: s3://amzn-s3-demo-bucket/path/MyFile1.txt to MyFile1.txt // Sync with delete, local copy of MyFile2.rtf is deleted
$
aws s3 sync s3://amzn-s3-demo-bucket/path . --delete
delete: MyFile2.rtf
s3 命令的常用选项
以下选项经常用于本主题中所述的命令。有关可在命令上使用的选项的完整列表,请参阅参考指南 参考指南 中的特定命令。
s3 sync
和s3 cp
可以使用--acl
选项。这样您能够为复制到 Amazon S3 的文件设置访问权限。--acl
选项接受private
、public-read
和public-read-write
值。有关更多信息,请参阅 Amazon S3 用户指南ACL 中的 罐装 。$
aws s3 sync . s3://amzn-s3-demo-bucket/path --acl public-read
- exclude
使用
s3 cp
、s3 mv
、s3 sync
或s3 rm
命令时,可以使用--exclude
或--include
选项筛选结果。--exclude
选项将规则设置为仅从命令中排除对象,并且系统将按照指定的顺序应用这些选项。如下例所示。Local directory contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt
// Exclude all .txt files, resulting in only MyFile2.rtf being copied
$
aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt"
// Exclude all .txt files but include all files with the "MyFile*.txt" format, resulting in, MyFile1.txt, MyFile2.rtf, MyFile88.txt being copied
$
aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt" --include "MyFile*.txt"
// Exclude all .txt files, but include all files with the "MyFile*.txt" format, but exclude all files with the "MyFile?.txt" format resulting in, MyFile2.rtf and MyFile88.txt being copied
$
aws s3 cp . s3://amzn-s3-demo-bucket/path --exclude "*.txt" --include "MyFile*.txt" --exclude "MyFile?.txt"
使用
s3 cp
、s3 mv
、s3 sync
或s3 rm
命令时,可以使用--exclude
或--include
选项筛选结果。--include
选项将规则设置为仅包括为命令指定的对象,并且系统将按照指定的顺序应用这些选项。如下例所示。Local directory contains 3 files: MyFile1.txt MyFile2.rtf MyFile88.txt
// Include all .txt files, resulting in MyFile1.txt and MyFile88.txt being copied
$
aws s3 cp . s3://amzn-s3-demo-bucket/path --include "*.txt"
// Include all .txt files but exclude all files with the "MyFile*.txt" format, resulting in no files being copied
$
aws s3 cp . s3://amzn-s3-demo-bucket/path --include "*.txt" --exclude "MyFile*.txt"
// Include all .txt files, but exclude all files with the "MyFile*.txt" format, but include all files with the "MyFile?.txt" format resulting in MyFile1.txt being copied
$
aws s3 cp . s3://amzn-s3-demo-bucket/path --include "*.txt" --exclude "MyFile*.txt" --include "MyFile?.txt"
s3 cp
、s3 mv
和s3 sync
命令包括一个--grants
选项,可用来向指定的用户或组授予对对象的权限。使用以下语法对权限列表设置--grants
选项。将Permission
、Grantee_Type
和Grantee_ID
替换为您自己的值。--grants
Permission
=Grantee_Type
=Grantee_ID
[Permission
=Grantee_Type
=Grantee_ID
...]每个值都包含以下元素:
Permission
— 指定授予的权限。可以设置为read
、readacl
、writeacl
或full
。Grantee_Type
— 指定如何识别被授权者。可以设置为uri
、emailaddress
或id
。Grantee_ID
— 根据以下条件指定被授予者Grantee_Type
.有关 Amazon S3 访问控制的更多信息,请参阅 访问控制 。
下面的示例将一个对象复制到一个存储桶中。它授予所有人对对象的
read
权限,向full
的关联账户授予read
权限(readacl
、writeacl
和[email protected]
)。$
aws s3 cp file.txt s3://amzn-s3-demo-bucket/ --grants
read=uri=http://acs.amazonaws.com/groups/global/AllUsers [email protected]
还可以为上传到 Amazon S3 的对象指定非默认存储类(
REDUCED_REDUNDANCY
或STANDARD_IA
)。为此,请使用--storage-class
选项。$
aws s3 cp file.txt s3://amzn-s3-demo-bucket/
--storage-class REDUCED_REDUNDANCY
- recursive
使用此选项时,系统针对所指定目录或前缀下的所有文件或对象执行该命令。以下示例删除
s3://amzn-s3-demo-bucket/path
及其所有内容。$
aws s3 rm s3://amzn-s3-demo-bucket/path --recursive
AWS CLI 参考: