【最佳实践】OSS开源工具ossutil-过滤参数include/exclude

应用场景

某些用户在本地或者OSS上存放了不同类型的文件,比如jpg文件和pptx文件。用户希望在(批量)上传jpg文件的时候设置不同的meta和ACL,而(批量)上传pptx文件的时候设置另外的meta和ACL。

也有用户希望在下载的时候只下载满足某种模式的文件,比如access-2018-05.log。

还有些用户希望对已经上传到OSS上的某种模式的文件进行批量设置Meta或ACL。

为满足用户的上述需求,ossutil引入过滤参数include/exclude,并在ossutil-1.4.0版本中release。

ossutil的方案

支持的命令

目前ossutil支持过滤参数的命令如下。

  • cp(upload, download, copy)

    • 同时支持cp的同时设置meta或ACL
  • set-meta
  • set-acl

--include/--exclude参数格式

规则支持以下格式

  • *:匹配索引
  • ?:匹配单个字符
  • [sequence]:匹配sequence的任意字符
  • [!sequence]:匹配不在sequence的任意字符

注意:

  • 规则不支持带目录的格式,e.g.,--include "/usr//test/.jpg"
  • --include和--exclude可以出现多次
  • 当多个规则出现时,这些规则按从左往右的顺序应用
  • 指定--include/--exclude选项时,需同时指定--recursive选项

例如:

当前目录下包含3个文件:
testfile1.jpg
testfiel2.txt
testfile33.jpg

$ ossutil cp . oss://my-bucket/path --exclude '*.jpg'
上传testfile2.txt到oss://my-bucket/path/testfile2.txt

$ ossutil cp . oss://my-bucket/path --exclude '*.jpg' --include 'testfile*.jpg'
上传testfile1.jpg到oss://my-bucket/path/testfile1.jpg
上传testfile33.jpg到oss://my-bucket/path/testfile33.jpg
上传testfile2.txt到oss://my-bucket/path/testfile2.txt

$ ossutil cp . oss://my-bucket/path --exclude '*.jpg' --include 'testfile*.jpg' --exclude 'testfile?.jpg'
上传testfile2.txt到oss://my-bucket/path/testfile2.txt
上传testfile33.jpg到oss://my-bucket/path/testfile33.jpg

--meta选项

该选项在上传文件的同时设置object的meta信息。当指定--recursive选项时,会设置所有上传的objects的meta信息。

可选的header列表如下:

Accept-Encoding
Content-Type
Content-Disposition
Content-Encoding
Expires(time.RFC3339: 2006-01-02T15:04:05Z07:00)
X-Oss-Server-Side-Encryption
X-Oss-Object-Acl
Origin
Cache-Control
以及以X-Oss-Meta-开头的header

注意:

  • header不区分大小写,但value区分大小写。设置后将用指定的meta代替原来的meta。没有指定的HTTP HEADER将保留,没有指定的user meta将会被删除。

--acl选项

该选项在上传文件的同时设置object的acl信息。当指定--recursive选项时,会设置所有上传的objects的acl信息。

object的acl有四种:

public-read-write
public-read
private
default

acl的详细信息请参见:https://help.aliyun.com/document_detail/31867.html

例子

上传时过滤

$ ls testdir
dir1             test2.jpg        test3.jpg        testfile1020.txt testfile1021.txt testfile1022.txt

$ ls testdir/dir1
my.rtf          testfile103.txt testfile104.txt

$ ossutil cp testdir/ oss://tempb3 -rf --include '*.txt'
Succeed: Total num: 6, size: 51,200. OK num: 6(upload 5 files, 1 directories).
0.436030(s) elapsed

$ ossutil ls oss://tempb3
LastModifiedTime                   Size(B)  StorageClass   ETAG                                  ObjectName
2018-06-05 17:55:26 +0800 CST            0      Standard   D41D8CD98F00B204E9800998ECF8427E      oss://tempb3/dir1/
2018-06-05 17:55:26 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/dir1/testfile103.txt
2018-06-05 17:55:26 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/dir1/testfile104.txt
2018-06-05 17:55:26 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/testfile1020.txt
2018-06-05 17:55:26 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/testfile1021.txt
2018-06-05 17:55:26 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/testfile1022.txt
Object Number is: 6
0.045585(s) elapsed

上传时过滤并设置meta/acl

$ ossutil cp testdir/ oss://tempb3 -rf --include '*.txt' --include '*.jpg' --exclude '*2*' --meta Cache-Control:no-cache#X-Oss-Meta-Test:with-pattern --acl public-read
Succeed: Total num: 4, size: 30,720. OK num: 4(upload 3 files, 1 directories).
0.160225(s) elapsed

$ ossutil ls oss://tempb3
LastModifiedTime                   Size(B)  StorageClass   ETAG                                  ObjectName
2018-06-05 17:54:19 +0800 CST            0      Standard   D41D8CD98F00B204E9800998ECF8427E      oss://tempb3/dir1/
2018-06-05 17:54:19 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/dir1/testfile103.txt
2018-06-05 17:54:19 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/dir1/testfile104.txt
2018-06-05 17:54:19 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/test3.jpg
Object Number is: 4
0.047468(s) elapsed

$ ossutil stat oss://tempb3/test3.jpg
ACL                         : public-read   ===> As expected
Accept-Ranges               : bytes
Cache-Control               : no-cache
Content-Length              : 10240
Content-Md5                 : EnZIEQLyGMmB4DJBgLr9nw==
Content-Type                : image/jpeg
Etag                        : 1276481102F218C981E0324180BAFD9F
Last-Modified               : 2018-06-05 17:57:00 +0800 CST
Owner                       : 1207396368585152
X-Oss-Hash-Crc64ecma        : 3732451854539430063
X-Oss-Meta-Test             : with-pattern   ===> As expected
X-Oss-Object-Type           : Normal
X-Oss-Storage-Class         : Standard
0.065338(s) elapsed

设置Meta时过滤

$ ossutil ls oss://tempb3
LastModifiedTime                   Size(B)  StorageClass   ETAG                                  ObjectName
2018-06-05 17:59:33 +0800 CST            0      Standard   D41D8CD98F00B204E9800998ECF8427E      oss://tempb3/dir1/
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/dir1/my.rtf
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/dir1/testfile103.txt
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/dir1/testfile104.txt
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/test2.jpg
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/test3.jpg
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/testfile1020.txt
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/testfile1021.txt
2018-06-05 17:59:33 +0800 CST        10240      Standard   1276481102F218C981E0324180BAFD9F      oss://tempb3/testfile1022.txt
Object Number is: 9
0.049015(s) elapsed

$ ossutil set-meta oss://tempb3 'X-Oss-Meta-Test:With-Pattern' -rf --include "*.jpg" --include "*.txt" --exclude "*2*"
Succeed: Total 3 objects. Setted meta on 3 objects.
0.138041(s) elapsed

$ ossutil stat oss://tempb3/test3.jpg
ACL                         : default
Accept-Ranges               : bytes
Content-Length              : 10240
Content-Md5                 : EnZIEQLyGMmB4DJBgLr9nw==
Content-Type                : image/jpeg
Etag                        : 1276481102F218C981E0324180BAFD9F
Last-Modified               : 2018-06-05 18:01:48 +0800 CST
Owner                       : 1207396368585152
X-Oss-Hash-Crc64ecma        : 3732451854539430063
X-Oss-Meta-Test             : With-Pattern   ===> As expected
X-Oss-Object-Type           : Normal
X-Oss-Storage-Class         : Standard
0.203471(s) elapsed

$ ossutil stat oss://tempb3/dir1/testfile103.txt
ACL                         : default
Accept-Ranges               : bytes
Content-Length              : 10240
Content-Md5                 : EnZIEQLyGMmB4DJBgLr9nw==
Content-Type                : text/plain; charset=utf-8
Etag                        : 1276481102F218C981E0324180BAFD9F
Last-Modified               : 2018-06-05 18:01:48 +0800 CST
Owner                       : 1207396368585152
Vary                        : Accept-Encoding
X-Oss-Hash-Crc64ecma        : 3732451854539430063
X-Oss-Meta-Test             : With-Pattern   ===> As expected
X-Oss-Object-Type           : Normal
X-Oss-Storage-Class         : Standard
0.168456(s) elapsed

$ ossutil stat oss://tempb3/dir1/testfile104.txt
ACL                         : default
Accept-Ranges               : bytes
Content-Length              : 10240
Content-Md5                 : EnZIEQLyGMmB4DJBgLr9nw==
Content-Type                : text/plain; charset=utf-8
Etag                        : 1276481102F218C981E0324180BAFD9F
Last-Modified               : 2018-06-05 18:01:48 +0800 CST
Owner                       : 1207396368585152
Vary                        : Accept-Encoding
X-Oss-Hash-Crc64ecma        : 3732451854539430063
X-Oss-Meta-Test             : With-Pattern   ===> As expected
X-Oss-Object-Type           : Normal
X-Oss-Storage-Class         : Standard
0.164251(s) elapsed

总结

本文简单叙述了ossutil的--include/--exclude参数,目前cp、set-meta、set-acl命令支持该参数,同时cp上传的同时支持设置meta、ACL。该参数能帮助用户上传、下载、拷贝满足特定模式的文件、对象。

Reference

上一篇:Java 并发——多线程基础


下一篇:[转]mysql 导入导出数据库以及函数、存储过程的介绍