数据处理及使用

生命周期管理

BOS支持用户对Bucket设置生命周期规则,以自动将过期的文件清除,节省存储空间。针对不同前缀的文件,用户可以同时设置多条规则。
详细信息请参考生命周期管理

设置生命周期规则

可以通过如下两种方式设置生命周期规则:

// 1、以Json格式的字符串
String jsonBucketLifecylce = "{\"rule\":[{\"id\":\"yourBucketName/*-DeleteObject\""+",\"status\":\"enabled\""+",\"resource\":[\"yourBucketName/*\"]"+",\"condition\":{\"time\":{\"dateGreaterThan\":\"2018-09-07T00:00:00Z\"}}"+",\"action\":{\"name\":\"DeleteObject\"}}]}";

SetBucketLifecycleRequest request = new SetBucketLifecycleRequest("yourBucketName",jsonBucketLifecylce);
client.setBucketBucketLifecycle(request);
// 2、用户只需指定指定参数即可
List<Rule> rules = new ArrayList<Rule>();
List<String> resource = new ArrayList<String>();
Action action = new Action();
Condition condition = new Condition();

resource.add("yourBucketName/*");
action.setName("DeleteObject");

Time time = new Time();
time.setDateGreaterThan("2018-09-07T00:00:00Z");
condition.setTime(time);

Rule rule = new Rule();
rule.setId("yourBucketName/*-DeleteObject");
rule.setStatus("enabled");
rule.setResource(resource);
rule.setCondition(condition);
rule.setAction(action);

List<String> resource1 = new ArrayList<String>();
Action action1 = new Action();
Condition condition1 = new Condition();

resource1.add("yourBucketName/*");
action1.setName("Transition");
action1.setStorageClass("STANDARD_IA");

Time time1 = new Time();
time1.setDateGreaterThan("2018-09-08T00:00:00Z");
condition1.setTime(time1);

Rule rule1 = new Rule();
rule1.setId("yourBucketName/*-Transition");
rule1.setStatus("enabled");
rule1.setResource(resource1);
rule1.setCondition(condition1);
rule1.setAction(action1);

rules.add(rule1);
rules.add(rule);

SetBucketLifecycleRequest request = new SetBucketLifecycleRequest();
request.withBucketName("yourBucketName");
request.setRuleList(rules);
client.setBucketBucketLifecycle(request);

查看生命周期规则

可通过如下代码查看Bucket内的生命周期规则:

GetBucketLifecycleRequest request = new GetBucketLifecycleRequest("yourBucketName");
GetBucketLifecycleResponse response = client.getBucketLifecycle(request);

删除生命周期规则

可通过如下代码清空生命周期规则:

DeleteBucketLifecycleRequest request = new DeleteBucketLifecycleRequest("yourBucketName");
client.deleteBucketLifecycle(request);

跨域资源共享

跨域资源共享(Cross-Origin Resource Sharing),简称CORS,是HTML5提供的标准跨域解决方案,BOS目前已经支持CORS标准来实现跨域访问。关于跨域访问的介绍请参考跨域访问

设定CORS规则

可以通过如下两种方式设置跨域规则:

// 1、以Json格式的字符串
String jsonBucketCors = "{\"corsConfiguration\": [{\"allowedOrigins\":[\"http://*\""+",\"https://*\"]"+",\"allowedMethods\":[\"GET\""+",\"HEAD\""+",\"POST\""+",\"PUT\"]"+",\"allowedHeaders\":[\"*\"]"+",\"allowedExposeHeaders\":[\"ETag\""+",\"Content-Length\""+",\"x-bce-next-append-offset\""+",\"x-bce-object-type\""+",\"x-bce-request-id\"]"+",\"maxAgeSeconds\":1800}]}";

SetBucketCorsRequest request = new SetBucketCorsRequest();
request.setJsonBucketCors(jsonBucketCors);
request.withBucketName("yourBucketName");
client.setBucketBucketCors(request);
// 2、用户只需指定指定参数即可
SetBucketCorsRequest request = new SetBucketCorsRequest();
request.setBucketName("yourBucketName");
List<CorsConfiguration>  corsConfigurations = new ArrayList<CorsConfiguration>();
List<String> allowedOrigins = new ArrayList<String>();
List<AllowedMethods> allowedMethods = new ArrayList<AllowedMethods>();
List<String> allowedHeaders = new ArrayList<String>();
List<String> allowedExposeHeaders = new ArrayList<String>();

CorsConfiguration corsConfiguration = new CorsConfiguration();
CorsConfiguration corsConfiguration1 = new CorsConfiguration();
// AllowedOrigins
allowedOrigins.add("http://www.example.com");
allowedOrigins.add("http://www.example1.com");

allowedMethods.add(AllowedMethods.GET);
allowedMethods.add(AllowedMethods.POST);
allowedMethods.add(AllowedMethods.PUT);
allowedMethods.add(AllowedMethods.DELETE);

// allowedHeaders
allowedHeaders.add("head1");
allowedHeaders.add("head2");

// allowedExposeHeaders
allowedExposeHeaders.add("ETag");
allowedExposeHeaders.add("Content-Length");
allowedExposeHeaders.add("x-bce-next-append-offset");
allowedExposeHeaders.add("x-bce-object-type");

corsConfiguration.setAllowedOrigins(allowedOrigins);
corsConfiguration.setAllowedMethods(allowedMethods);
corsConfiguration.setAllowedHeaders(allowedHeaders);
corsConfiguration.setAllowedExposeHeaders(allowedExposeHeaders);
corsConfiguration.setMaxAgeSeconds(1800);

List<String> allowedOrigins1 = new ArrayList<String>();
List<AllowedMethods> allowedMethods1 = new ArrayList<AllowedMethods>();
List<String> allowedHeaders1 = new ArrayList<String>();
List<String> allowedExposeHeaders1 = new ArrayList<String>();

corsConfigurations.add(corsConfiguration);

// AllowedOrigins
allowedOrigins1.add("allowedOrigin1");
allowedOrigins1.add("allowedOrigin2");

// allowedMethods
allowedMethods1.add(AllowedMethods.GET);
allowedMethods1.add(AllowedMethods.POST);
allowedMethods1.add(AllowedMethods.PUT);
allowedMethods1.add(AllowedMethods.DELETE);

// allowedHeaders
allowedHeaders1.add("head3");
allowedHeaders1.add("head4");

// allowedExposeHeaders
allowedExposeHeaders1.add("ETag");
allowedExposeHeaders1.add("Content-Length");
allowedExposeHeaders1.add("x-bce-next-append-offset");
allowedExposeHeaders1.add("x-bce-object-type");


corsConfiguration1.setAllowedOrigins(allowedOrigins1);
corsConfiguration1.setAllowedMethods(allowedMethods1);
corsConfiguration1.setAllowedHeaders(allowedHeaders1);
corsConfiguration1.setAllowedExposeHeaders(allowedExposeHeaders1);
corsConfiguration1.setMaxAgeSeconds(3600);

corsConfigurations.add(corsConfiguration1);

request.setCorsConfigurationsList(corsConfigurations);
client.setBucketBucketCors(request);

注意:

  • 通过此接口设置CORS规则之前,Bucket的CORS权限设置为不允许跨域。
  • 每个Bucket最多只允许有一个规则文件,因此新上传的规则文件会覆盖原有的。
  • CORS规则文件大小限制为20KB,因此请求时大于20KB会返回超出大小错误(400 Bad Request: EntityTooLarge)。

获取CORS规则

可以通过如下方式获取跨域规则:

GetBucketCorsRequest request = new GetBucketCorsRequest();
request.setBucketName("yourBucketName");
GetBucketCorsResponse response = client.getBucketCros(request);

删除CORS规则

可以通过如下方式删除跨域规则:

DeleteBucketCorsRequest request = new DeleteBucketCorsRequest("yourBucketName");
client.deleteBucketCors(request);

设置访问日志

BOS SDK支持将用户访问Bucket时的请求记录记录为日志,会包括请求者、Bucket名称、请求时间和请求操作等。关于Bucket日志的详细功能说明可参见设置访问日志

开启Bucket日志

  • 请求参数
名称 描述 类型 是否必须
targetBucket 指定存放访问日志的Bucket 字符串
targetPrefix 指定最终被保存的访问日志文件前缀 字符串 否(建议填写,用以区分访问日志)
  • 目前提供两种方式供用户使用
    1、以Json格式的字符串
    2、用户只需指定指定参数即可

可以通过如下两种方式设置日志规则:

// 1、以Json格式的字符串
SetBucketLoggingRequest setBucketLoggingRequest = new SetBucketLoggingRequest();
setBucketLoggingRequest.setBucketName("yourBucketName");
String jsonPutBucketLogging = "{\"targetBucket\":\"yourTargetBucketName\""+",\"targetPrefix\":\"mylog1/\"}";
setBucketLoggingRequest.setJsonPutBucketLogging(jsonPutBucketLogging);
client.setBucketLogging(setBucketLoggingRequest);
// 2、用户只需指定指定参数即可
SetBucketLoggingRequest request = new SetBucketLoggingRequest();
request.setBucketName("yourBucketName");
request.setTargetBucket("yourTargetBucketName");
request.setTargetPrefix("mylog/");
client.setBucketLogging(request);

查看Bucket日志设置

可以通过如下方式获取日志规则:

GetBucketLoggingRequest request = new GetBucketLoggingRequest();
request.withBucketName("yourBucketName");
GetBucketLoggingResponse response = client.getBucketLogging(request);

关闭Bucket日志

可以通过如下方式关闭日志规则:

DeleteBucketLoggingRequest request = new DeleteBucketLoggingRequest("yourBucketName");
client.deleteBucketLogging(request);