设置桶清单规则(java sdk)-九游平台
功能说明
桶清单功能可以定期生成桶内对象的相关信息,保存在csv格式的文件中,并上传到您指定的桶中,方便您管理桶内对象。目标桶和源桶可以是同一个桶。指定的对象内容包括对象版本、大小、上次修改时间、存储类别、标签、加密状态等。
支持对桶清单加密,加密方式为sse-kms方式。

开发过程中,您有任何问题可以在github上,或者在中发帖求助。
接口约束
- 您必须是桶拥有者或者拥有设置桶清单权限,才能设置桶清单。建议使用iam或桶策略进行授权,如果使用iam则需授予obs:bucket:putbucketinventoryconfiguration权限,如果使用桶策略则需授予putbucketinventoryconfiguration权限。相关授权方式介绍可参见obs权限控制概述,配置方式详见、。
- obs支持的region以及region与endpoint的对应关系,详细信息请参见。
- 桶版本限制:
- 只有obs 3.0的桶支持配置清单,目标桶无obs版本限制。
- 清单数量限制:
- 一个桶最多支持10条桶清单。
- 源桶和目标桶限制:
- 桶清单配置的源桶和目标桶必须归属同一个账号。
- 桶清单配置的源桶和目标桶必须归属同一个区域。
- 桶清单中配置的目标桶不能开启桶默认加密。
- 功能限制:
- 只支持生成csv格式的清单文件。
- 桶清单筛选条件目前仅支持设置为所有对象或指定前缀的对象。
- 同一个桶中多条清单规则的筛选条件不能彼此包含:
- 如果已经存在针对桶中所有对象的规则,则无法再创建按对象名前缀筛选的规则。如需创建,要先删除针对所有对象的规则。
- 如果已经存在按对象名前缀筛选的规则,则无法再创建针对桶中所有对象的规则。如需创建,要先删除所有按对象名前缀筛选的规则。
- 如果已经存在某个按对象名前缀筛选的规则(如前缀ab),则无法再创建与其存在包含或被包含关系的规则(如前缀a或前缀abc)。如需创建,要先删除存在包含或被包含关系的规则。
- 桶清单加密方式目前只支持sse-kms。
- 权限限制:
- 清单文件使用obs系统用户上传到目标桶,目标桶必须给obs系统用户写权限。
- 其他:
- 暂不对桶清单功能收费,桶清单生成后只按照存量计费。
方法定义
obsclient.setinventoryconfiguration(setinventoryconfigurationrequest request)
请求参数说明
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
bucketname |
string |
是 |
参数解释: 桶名。 约束限制:
默认取值: 无 |
inventoryconfiguration |
是 |
参数解释: 桶清单配置规则,详见inventoryconfiguration。 |
参数名称 |
参数类型 |
是否必选 |
描述 |
---|---|---|---|
configurationid |
string |
必选 |
参数解释: 桶清单配置规则的id。 约束限制: 必须由以下字符组成"a-z"、"a-z"、"0-9"、"-"、"_"和"."。 取值范围: 长度最长为64的字符串。 默认取值: 无 |
isenabled |
boolean |
必选 |
参数解释: 桶清单配置规则是否启用。 取值范围: true:生成清单文件。 false:不生成清单文件。 默认取值: true |
objectprefix |
string |
可选 |
参数解释: 前缀过滤条件,清单文件中只生成以此前缀开头的对象列表。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: 无 |
frequency |
string |
必选 |
参数解释: 清单文件的生成周期,只支持按天和按周生成清单,第一次配置完桶清单,任务会在一个小时内启动,之后每隔一个周期启动一次。 取值范围: daily:按天生成清单 weekly:按周生成清单 默认取值: 无 |
inventoryformat |
string |
必选 |
参数解释: 生成的清单文件的格式,现只支持csv格式。 取值范围: csv 默认取值: 无 |
destinationbucket |
string |
必选 |
参数解释: 存放清单文件的目标桶的桶名。 约束限制:
默认取值: 无 |
inventoryprefix |
string |
可选 |
参数解释: 生成的清单文件对象名会以此前缀开头。 取值范围: 长度大于0且不超过1024的字符串。 默认取值: bucketinventory,即如果不配置前缀,则生成的清单文件对象名默认以"bucketinventory"字符串为前缀开头。 |
includedobjectversions |
string |
必选 |
参数解释: 清单文件中包含对象的多版本配置。 取值范围:
默认取值: 无 |
optionalfields |
arraylist |
可选 |
参数解释: 在此选项中可以添加一些额外的对象元数据字段,生成的清单文件中会包含optionalfields中配置的字段。 取值范围: size:对象大小。 lastmodifieddate:对象最后修改时间。 storageclass:对象的存储类别。 etag:对象的etag值。 ismultipartuploaded:对象是否为。 replicationstatus:对象的跨区域复制状态。 encryptionstatus:对象的加密状态。 默认取值: 无 |
返回结果说明
参数名称 |
参数类型 |
描述 |
---|---|---|
statuscode |
int |
参数解释: http状态码。 取值范围: 状态码是一组从2xx(成功)到4xx或5xx(错误)的数字代码,状态码表示了请求响应的状态。 完整的状态码列表请参见。 默认取值: 无 |
responseheaders |
map |
参数解释: http响应消息头列表,由多个元组构成。元组中string代表响应消息头的名称,object代表响应消息头的值。 默认取值: 无 |
代码示例
本示例将为example-bucket桶设置桶清单,桶清单会统计前缀为exampleobjectprefix的对象的信息。清单文件将存储在example-target-bucket桶中,清单文件的前缀为exampleinventoryprefix。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
importcom.obs.services.obsclient; importcom.obs.services.exception.obsexception; importcom.obs.services.model.headerresponse; importcom.obs.services.model.inventory.inventoryconfiguration; importcom.obs.services.model.inventory.setinventoryconfigurationrequest; publicclass setinventoryconfiguration001 { publicstaticvoidmain(string[]args){ // 您可以通过环境变量获取访问密钥ak/sk,也可以使用其他外部引入方式传入。如果使用硬编码可能会存在泄露风险。 // 您可以登录访问管理控制台获取访问密钥ak/sk stringak=system.getenv("access_key_id"); stringsk=system.getenv("secret_access_key_id"); // 【可选】如果使用临时ak/sk和securitytoken访问obs,同样建议您尽量避免使用硬编码,以降低信息泄露风险。 // 您可以通过环境变量获取访问密钥ak/sk/securitytoken,也可以使用其他外部引入方式传入。 // string securitytoken = system.getenv("security_token"); // endpoint填写桶所在的endpoint, 此处以华北-北京四为例,其他地区请按实际情况填写。 stringendpoint="https://obs.cn-north-4.myhuaweicloud.com"; // 您可以通过环境变量获取endpoint,也可以使用其他外部引入方式传入。 // string endpoint = system.getenv("endpoint"); // 创建obsclient实例 // 使用永久ak/sk初始化客户端 obsclientobsclient=newobsclient(ak,sk,endpoint); // 使用临时ak/sk和securitytoken初始化客户端 // obsclient obsclient = new obsclient(ak, sk, securitytoken, endpoint); try{ // 设置相关示例参数 stringexamplebucketname="example-bucket"; stringexampletargetbucketname="example-target-bucket"; stringexampleconfigurationid="exampleconfigid001"; stringexampleinventoryprefix="exampleinventoryprefix"; stringexampleobjectprefix="exampleobjectprefix"; // 设置桶清单配置规则详细参数 inventoryconfigurationexampleconfiguration=newinventoryconfiguration(); exampleconfiguration.setdestinationbucket(exampletargetbucketname); exampleconfiguration.setconfigurationid(exampleconfigurationid); exampleconfiguration.setinventoryformat(inventoryconfiguration.inventoryformatoptions.csv); exampleconfiguration.setfrequency(inventoryconfiguration.frequencyoptions.daily); exampleconfiguration.setenabled(true); exampleconfiguration.setincludedobjectversions( inventoryconfiguration.includedobjectversionsoptions.current); exampleconfiguration.setinventoryprefix(exampleinventoryprefix); exampleconfiguration.setobjectprefix(exampleobjectprefix); // 设置清单文件中会包含的额外的对象元数据字段 exampleconfiguration .getoptionalfields() .add(inventoryconfiguration.optionalfieldoptions.is_multipart_uploaded); exampleconfiguration.getoptionalfields().add(inventoryconfiguration.optionalfieldoptions.etag); exampleconfiguration .getoptionalfields() .add(inventoryconfiguration.optionalfieldoptions.replication_status); setinventoryconfigurationrequestrequest= newsetinventoryconfigurationrequest(examplebucketname,exampleconfiguration); // 设置桶清单配置规则 headerresponseresponse=obsclient.setinventoryconfiguration(request); system.out.println("setinventoryconfiguration succeeded"); system.out.println("http code: "response.getstatuscode()); }catch(obsexceptione){ system.out.println("setinventoryconfiguration failed"); // 请求失败,打印http状态码 system.out.println("http code: "e.getresponsecode()); // 请求失败,打印服务端错误码 system.out.println("error code:"e.geterrorcode()); // 请求失败,打印详细错误信息 system.out.println("error message:"e.geterrormessage()); // 请求失败,打印请求id system.out.println("request id:"e.geterrorrequestid()); system.out.println("host id:"e.geterrorhostid()); }catch(exceptione){ system.out.println("setinventoryconfiguration failed"); // 其他异常信息打印 e.printstacktrace(); } } } |
相关链接
- 关于设置桶清单的api说明,请参见设置桶清单。
- 设置桶清单过程中返回的错误码含义、问题原因及处理措施可参考obs错误码。
- 桶和对象相关常见问题请参见桶和对象相关常见问题。
相关文档
意见反馈
文档内容是否对您有帮助?
如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨