1. 使用CLI管理对象存储

本节介绍如何使用CLI管理对象存储。

在使用CLI管理对象存储前,需完成CLI的安装与配置。安装请参阅《安装CLI管理工具》,配置请参阅《配置CLI管理工具》

1.1. 命令结构

使用CLI管理对象存储的命令结构如下:

 ycloud s3 <command> [--options]

其中command为必选项,--options为可选项。 ycloud s3命令支持以下可选项:

可选项 含义 适用的命令
--recursive 递归操作,如列出、删除目录时同时列出、删除子目录下Object ls、cp、mv、rm、sync
--include 包含操作,用于筛选时包含符合条件的Object ls、cp、mv、rm、sync
--exclude 排除操作,用于筛选时排除符合条件的Object ls、cp、mv、rm、sync
--acl 访问控制,用于设定Object或目录的访问权限,接受private和public-read值 cp、mv、sync
--delete 删除内容,用于删除sync操作目标中比源中多的内容 sync

1.2. 管理Bucket

您可以使用ycloud s3命令方便地管理Bucket,包括创建、列出和删除Bucket。

注意

对象存储服务是区域级的,同一个Bucket下的所有资源存储在一个区域下。 创建Bucket和列出Bucket需要项目ID信息,在操作前需保证配置的project id信息正确无误,详情请参阅《配置CLI管理工具》

1.2.1. 创建Bucket

使用mb命令可以创建新的Bucket。Bucket名称须在所属区域下唯一,且只能包含小写字母、数字和短横线,只能以字母开头,并以字母或数字结尾,Bucket名称长度需在3-63字符内。

$ ycloud s3 mb s3://my-bucket
make_bucket: my-bucket

1.2.2. 列出Bucket

使用ls命令可以列出所属项目在该区域下的Bucket。

$ ycloud s3 ls
2019-3-11 17:08:50 my-bucket
2019-3-14 14:55:44 my-bucket2

1.2.3. 删除Bucket

使用rb命令可以删除指定的Bucket。

$ ycloud s3 rb s3://my-bucket
remove_bucket: my-bucket

 

默认情况下,只能删除空的Bucket。要删除非空Bucket,请添加 --force 选项,此选项会同时将Bucket内的所有资源清空。

$ ycloud s3 rb s3://my-bucket --force
delete: s3://my-bucket/MyFile.txt
delete: s3://my-bucket/path/MyFile.txt
remove_bucket: my-bucket

1.3. 管理Object

您可以使用ycloud s3命令方便地管理Bucket中的Object,包括列出、上传、复制(下载)和同步Object操作。

注意

如果文件或Object大于8M,所有涉及向Bucket上传的命令(cp、mv 和 sync)都会自动执行分段上传。当上传或下载路径中有同名的文件,会将同名的文件覆盖。

1.3.1. ls命令

使用ls命令列出指定Bucket中的所有Object。

$ ycloud s3 ls s3://my-bucket
                 PRE path/
2019-3-04 19:05:48 3 MyFile1.txt

 

使用ls命令时,您可以通过在命令中使用前缀来列出该路径下的所有内容,但不会展开子目录。

$ ycloud s3 ls s3://my-bucket/path/
2019-3-06 18:59:32 3 MyFile2.txt

 

ls 命令接受 --recursive 选项,该选项会遍历目录树,包括所有子目录。

$ ycloud s3 ls s3://my-bucket/ --recursive
2019-3-06 17:39:32 3 path/MyFile2.txt
2019-3-06 17:26:43 3 MyFile1.txt

1.3.2. cp命令

使用cp命令可以上传(将本地文件或目录复制到Bucket)、下载(将Object复制到本地),以及复制(将Object复制到Bucket)。

上传:将本地文件或目录复制到Bucket

$ ycloud s3 cp MyFile.txt s3://my-bucket/path/
upload: .\MyFile.txt to s3://my-bucket/path/MyFile.txt

 

下载:将Object复制到本地

$ ycloud s3 cp s3://my-bucket/MyFile.txt MyFile.txt.bak
download: s3://my-bucket/MyFile.txt to .\MyFile.txt.bak

 

复制:将Object复制到Bucket

$ ycloud s3 cp s3://my-bucket/MyFile.txt s3://my-bucket/path/MyFile.txt
copy:s3://my-bucket/MyFile.txt to s3://my-bucket/path/MyFile.txt

$ ycloud s3 cp s3://my-bucket/MyFile.txt s3://my-bucket/path/File1.txt
copy:s3://my-bucket/MyFile.txt to s3://my-bucket/path/File1.txt

 

上传、下载和复制均可对文件或Object重命名。

另外,cp命令接受一些可选项实现特有的功能。

使用 --recursive 选项,该选项会遍历目录树,允许直接复制目录或Bucket。

$ ycloud s3 cp . s3://my-bucket --recursive
upload: .\MyFile.txt.bak to s3://my-bucket/MyFile.txt.bak
upload: .\MyFile.txt to s3://my-bucket/MyFile.txt

 

使用 --acl 选项可设置复制到Bucket的Object读写权限:

$ ycloud s3 cp MyFile.txt s3://my-bucket --acl public-read
upload: .\MyFile.txt to s3://my-bucket/MyFile.txt

1.3.3. mv命令

使用mv命令可将本地文件或Bucket中的Object移动到本地或Bucket。

$ ycloud s3 mv s3://my-bucket/MyFile.txt .
move: s3://my-bucket/MyFile.txt to .\MyFile.txt

$ ycloud s3 mv s3://my-bucket/MyFile.txt.bak s3://my-bucket/MyFile.txt
move: s3://my-bucket/MyFile.txt.bak to s3://my-bucket/MyFile.txt

 

使用 --recursive 选项,可将本地目录或Bucket中的目录移动到Bucket。

$ ycloud s3 mv . s3://my-bucket
move failed: .\ to s3://my-bucket Parameter validation failed:
Invalid length for parameter Key, value: 0, valid range: 1-inf

$ ycloud s3 mv . s3://my-bucket --recursive
upload: .\MyFile.txt.bak to s3://my-bucket/MyFile.txt.bak
upload: .\MyFile.txt to s3://my-bucket/MyFile.txt

 

使用 --acl 选项可以指定移动后的操作Object的读写权限。

$ ycloud s3 mv MyFile.txt s3://my-bucket --acl public-read
move: .\MyFile.txt to s3://my-bucket/MyFile.txt

1.3.4. rm命令

使用rm命令可删除Bucket中的Object。如需删除Bucket或目录,请使用 --recursive 选项。

$ ycloud s3 rm s3://my-bucket/path/MyFile.txt
delete: s3://my-bucket/path/MyFile.txt

$ ycloud s3 rm s3://my-bucket/path --recursive

1.3.5. sync命令

sync命令使用如下语法:

ycloud s3 sync <source> <target> [--options]

可能的源-目标组合有:

  • 本地文件系统到Bucket

  • Bucket到本地文件系统

  • Bucket到Bucket

下面的示例将当前目录与Bucket中目录的内容同步。sync将更新与目标中同名的具有不同修改时间的任何文件。

请注意,此操作也会把当前目录下的子目录及其内容递归同步到Bucket中的目录。

$ ycloud s3 sync . s3://my-bucket/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

 

通常,sync仅在源和目标之间复制缺失或过时的文件或Object。当目标中存在源中不存在的文件或Object时,s3 sync并不会删除这些文件或Object。您可以使用 --delete 选项来从目标中删除源中不存在的文件或Object。

下面的示例对上一示例进行了扩展,展示其工作方式。

// Delete local file
$ rm ./MyFile1.txt

// Attempt to sync without --delete option - nothing happens
$ ycloud s3 sync . s3://my-bucket/path

// Sync with deletion,object will be deleted from bucket
$ ycloud s3 sync . s3://my-bucket/path --delete
delete: s3://my-bucket/path/MyFile1.txt

// Delete object from bucket
$ ycloud s3 rm s3://my-bucket/path/MySubdirectory/MyFile3.txt
delete: s3://my-bucket/path/MySubdirectory/MyFile3.txt

// Sync with deletion,local file will be deleted
$ ycloud s3 sync s3://my-bucket/path . --delete
delete: MySubdirectory\MyFile3.txt

 

可以使用 --exclude 和 --include 选项来筛选要在同步操作期间复制的文件或Object。默认情况下,指定目录中的所有项都包含将被同步。使用 --exclude 选项可以排除不需要的文件或Object。仅当需要指定 --exclude 选项的例外时,才需要使用 --include。这些选项按指定顺序应用,如下例所示。

// Local directory contains 4 files
$ ls
MyFile1.txt
MyFile2.rtf
MyFile88.txt
MyFile3.txt

// Sync local files to bucket, excluding *.txt files
$ ycloud s3 sync . s3://my-bucket/path --exclude *.txt
upload: MyFile2.rtf to s3://my-bucket/path/MyFile2.rtf

// Sync local files to bucket, excluding *.txt files but including MyFile1.txt and MyFile3.txt
$ ycloud s3 sync . s3://my-bucket/path --exclude *.txt --include MyFile?.txt
upload: MyFile1.txt to s3://my-bucket/path/MyFile1.txt
upload: MyFile2.rtf to s3://my-bucket/path/MyFile2.rtf
upload: MyFile3.txt to s3://my-bucket/path/MyFile3.txt

// Sync local files to bucket, excluding *.txt files but including MyFile3.txt
$ ycloud s3 sync . s3://my-bucket/path --exclude *.txt --include MyFile?.txt --exclude MyFile1.txt
upload: MyFile3.txt to s3://my-bucket/path/MyFile3.txt

 

--exclude 和 --include 选项也可以筛选要在sync操作(使用 --delete 选项)期间删除的文件或Object。在这种情况下,参数必须指定要在目标中包含或排除在删除操作中的文件或Object。

// Assume local directory and s3://my-bucket/path currently in sync and each contains 4 files
$ ls
MyFile1.txt
MyFile2.rtf
MyFile88.txt
MyFile3.txt

// Delete local *.txt files
$ rm *.txt

// Sync with deletion, excluding files that match a pattern. MyFile88.txt is deleted, while remote MyFile1.txt and MyFile3.txt is not.
$ ycloud s3 sync . s3://my-bucket/path --delete --exclude MyFile?.txt
delete: s3://my-bucket/path/MyFile88.txt

// Delete MyFile2.rtf
$ ycloud s3 rm s3://my-bucket/path/MyFile2.rtf

// Sync with deletion, excluding MyFile2.rtf , local file will not be deleted
$ ycloud s3 sync s3://my-bucket/path . --delete --exclude MyFile2.rtf
download: s3://my-bucket/path/MyFile1.txt to MyFile1.txt
download: s3://my-bucket/path/MyFile3.txt to MyFile3.txt

// Sync with deletion, local copy of MyFile2.rtf will be deleted
$ ycloud s3 sync s3://my-bucket/path . --delete
delete: MyFile2.rtf

 

使用 --acl 选项可以对同步到Bucket中的Object设置访问权限。

$ ycloud s3 sync . s3://my-bucket/path --acl public-read

results matching ""

    No results matching ""