S3 awscli常用命令 作者: ciniao 时间: 2021-10-20 分类: 技术 * 本文发布时间较久,请自行判断有效性 这里给大家介绍下使用命令行来进行s3的数据传输,以便将命令行写入本地代码实现自动化数据传输 ####安装方式 ``` pip install -i https://pypi.tuna.tsinghua.edu.cn/simple awscli //安装后可通过 aws --version //来确定是否成功 #aws-cli/1.24.10 Python/3.6.8 Linux/3.10.0-1160.90.1.el7.x86_64 botocore/1.26.10 ``` ####配置本地cli环境 ``` aws configure --profile xxxxxx //其中xxxxxx为别名 //执行后,根据提示配置以下信息 AWS Access Key ID [****************xxxx]: # 输入你的AK key AWS Secret Access Key [****************xxxx]: # 输入你的SK key Default region name [cn-northwest-1]: Default output format [None]: ``` ####使用aws cli **桶命令** ``` aws s3 mb s3://bucket-name #创建存储桶,存储桶名称可以包含小写字母、数字、连字符和点号。存储桶名称只能以字母或数字开头和结尾,连字符或点号后不能跟点号。 aws s3 ls #列出您的存储桶 aws s3 ls s3://bucket-name #列出存储桶下的对象和文件夹 aws s3 rb s3://bucket-name #删除空桶 aws s3 rb s3://bucket-name --force #删除无版本控制的非空桶和桶内所有内容 ``` **对象命令** ``` aws s3 cp file.txt s3://my-bucket/ #拷贝文件到S3,反之亦然 aws s3 mv file.txt s3://my-bucket/ #移动本地文件到S3,反之亦然 aws s3 rm s3://my-bucket/path/MySubdirectory/MyFile3.txt #删除S3上的文件MyFile3.txt aws s3 sync . s3://my-bucket/path #将本地目录同步外桶里 aws s3 presign s3://awsexamplebucket/test2.txt --expires-in 604800 #为某个对象创建特定时间(秒为单位,604800=1周)有效的http访问链接,以供他人访问,国外随意,国内请先开ICP #以上命令如cp、mv 或 rm用于目录或文件夹时,可以通过添加 --recursive 选项来遍历目录树,包括所有子目录 s3 rm s3://my-bucket/path --recursive #删除s3://my-bucket/path 目录和其下所有内容 ``` 如果对象很大,所有涉及向S3 存储桶(s3 cp、s3 mv 和 s3 sync)上传对象的高级命令都会自动执行分段上传。使用这些命令时,无法恢复失败的上传。如果分段上传由于超时而失败,或者通过按 Ctrl+C 手动取消,AWS CLI 将会清除创建的所有文件并中止上传。此过程可能耗时数分钟。 如果进程被 kill 命令中断或者由于系统故障而中断,则正在进行的分段上传将保留在 Amazon S3 中,必须在 AWS 管理控制台中手动清除,或者使用 s3api abort-multipart-upload 命令来清除。 ####sync命令详解 关于sync的用法,这里多说几句,因为将来我们会经常打交道,其应用主要有三类 1. 本地文件系统到 S3 2. S3 到本地文件系统 3. S3 到 Amazon S3 使用格式:`aws s3 sync [--options]` ``` $ aws s3 sync . s3://my-bucket/path #如果path不存在会自动创建 upload: MySubdirectory/MyFile3.txt to s3://my-bucket/path/MySubdirectory/MyFile3.txt upload: MyFile2.txt to s3://my-bucket/path/MyFile2.txt upload: MyFile1.txt to s3://my-bucket/path/MyFile1.txt ``` 通过添加 `--delete` 选项来从目标中删除源中不存在的文件或对象 ``` // 测试1:删除本地的文件MyFile1.txt $ rm ./MyFile1.txt // 同步不加 --delete,没变化 $ aws s3 sync . s3://my-bucket/path // 同步加了--deletion,因为源是本地,目标桶内的文件也被删除 $ aws s3 sync . s3://my-bucket/path --delete delete: s3://my-bucket/path/MyFile1.txt // 测试2:删除桶内的文件MyFile3.txt $ aws s3 rm s3://my-bucket/path/MySubdirectory/MyFile3.txt delete: s3://my-bucket/path/MySubdirectory/MyFile3.txt // 同步加--deletion,这时候源是s3,目标是本地,所以本地的文件被删除 $ aws s3 sync s3://my-bucket/path . --delete delete: MySubdirectory\MyFile3.txt ``` 通过添加 `--storage-class` 选项来设定上传数据的存储类别 ``` // 将当前目录文件传送到s3,并以 模式存储,默认是STANDARD标准。 $ aws s3 sync . s3://my-bucket/path --storage-class STANDARD $ aws s3 sync . s3://my-bucket/path --storage-class STANDARD_IA $ aws s3 sync . s3://my-bucket/path --storage-class ONE-ZONE_IA $ aws s3 sync . s3://my-bucket/path --storage-class INTELLIGENT_TIERING $ aws s3 sync . s3://my-bucket/path --storage-class GLACIER $ aws s3 sync . s3://my-bucket/path --storage-class DEEP_ARCHIVE $ aws s3 sync . s3://my-bucket/path --storage-class REDUCED_REDUNDANCY ``` 特定数据不想同步怎么办呢? 别慌,我们有 `--exclude` 和 `--include`,来筛选要在同步操作期间复制的文件或对象,记住include选项一定是和exclude同时出现的,按顺序应用 ``` 假设当前目录有3个文件: MyFile1.txt MyFile2.rtf MyFile88.txt $ aws s3 sync . s3://my-bucket/path --exclude "*.txt" upload: MyFile2.rtf to s3://my-bucket/path/MyFile2.rtf $ aws s3 sync . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt" upload: MyFile1.txt to s3://my-bucket/path/MyFile1.txt upload: MyFile88.txt to s3://my-bucket/path/MyFile88.txt upload: MyFile2.rtf to s3://my-bucket/path/MyFile2.rtf $ aws s3 sync . s3://my-bucket/path --exclude "*.txt" --include "MyFile*.txt" --exclude "MyFile?.txt" upload: MyFile2.rtf to s3://my-bucket/path/MyFile2.rtf upload: MyFile88.txt to s3://my-bucket/path/MyFile88.txt ``` 这两个选项还可以和`--delete`配合使用 ``` 假设本地目录和 s3://my-bucket/path 已经同步,并各自包含3个文件: MyFile1.txt MyFile2.rtf MyFile88.txt // 删除所有本地的 .txt文件 $ rm *.txt // 同步删除模式, 但不同步删除符合MyFile?.txt命名的文件MyFile1.txt,所以只删除了MyFile88.txt $ aws s3 sync . s3://my-bucket/path --delete --exclude "my-bucket/path/MyFile?.txt" delete: s3://my-bucket/path/MyFile88.txt // 删除s3上的MyFile2.rtf $ aws s3 rm s3://my-bucket/path/MyFile2.rtf // 同步删除模式, 但不删除MyFile2.rtf,所以只是把本地之前被删掉的MyFile1.txt恢复到了本地(因为在上一步桶里的这个文件并没有被删除) $ aws s3 sync s3://my-bucket/path . --delete --exclude "./MyFile2.rtf" download: s3://my-bucket/path/MyFile1.txt to MyFile1.txt // 同步删除模式, 本地的MyFile2.rtf被删除了 $ aws s3 sync s3://my-bucket/path . --delete delete: MyFile2.rtf //本地目录: MyFile1.txt //s3桶: MyFile1.txt ``` 标签: none