Data Processing and Usage
Lifecycle management
BOS allows users to set lifecycle rules for buckets to automatically clear expired files and save storage space. Users can set multiple rules simultaneously for files with different prefixes. For details, please refer to [Lifecycle Management](BOS/API Reference/Bucket-Related Interface/Lifecycle/PutBucketLifecycle.md)
Set lifecycle rules
Lifecycle rules can be set in the following two ways:
1// 1. In the form of a Json string
2String jsonBucketLifecylce = "{\"rule\":[{\"id\":\"yourBucketName/*-DeleteObject\""+",\"status\":\"enabled\""+",\"resource\":[\"yourBucketName/*\"]"+",\"condition\":{\"time\":{\"dateGreaterThan\":\"2018-09-07T00:00:00Z\"}}"+",\"action\":{\"name\":\"DeleteObject\"}}]}";
3SetBucketLifecycleRequest request = new SetBucketLifecycleRequest("yourBucketName",jsonBucketLifecylce);
4client.setBucketBucketLifecycle(request);
1// 2. Users only need to specify the parameters
2List<Rule> rules = new ArrayList<Rule>();
3List<String> resource = new ArrayList<String>();
4Action action = new Action();
5Condition condition = new Condition();
6resource.add("yourBucketName/*");
7action.setName("DeleteObject");
8Time time = new Time();
9time.setDateGreaterThan("2018-09-07T00:00:00Z");
10condition.setTime(time);
11Rule rule = new Rule();
12rule.setId("yourBucketName/*-DeleteObject");
13rule.setStatus("enabled");
14rule.setResource(resource);
15rule.setCondition(condition);
16rule.setAction(action);
17List<String> resource1 = new ArrayList<String>();
18Action action1 = new Action();
19Condition condition1 = new Condition();
20resource1.add("yourBucketName/*");
21action1.setName("Transition");
22action1.setStorageClass("STANDARD_IA");
23Time time1 = new Time();
24time1.setDateGreaterThan("2018-09-08T00:00:00Z");
25condition1.setTime(time1);
26Rule rule1 = new Rule();
27rule1.setId("yourBucketName/*-Transition");
28rule1.setStatus("enabled");
29rule1.setResource(resource1);
30rule1.setCondition(condition1);
31rule1.setAction(action1);
32rules.add(rule1);
33rules.add(rule);
34SetBucketLifecycleRequest request = new SetBucketLifecycleRequest();
35request.withBucketName("yourBucketName");
36request.setRuleList(rules);
37client.setBucketBucketLifecycle(request);
View lifecycle rules
The lifecycle rules in a bucket can be viewed through the following code:
1GetBucketLifecycleRequest request = new GetBucketLifecycleRequest("yourBucketName");
2GetBucketLifecycleResponse response = client.getBucketLifecycle(request);
Delete lifecycle rules
A lifecycle rule can be cleared through the following code:
1DeleteBucketLifecycleRequest request = new DeleteBucketLifecycleRequest("yourBucketName");
2client.deleteBucketLifecycle(request);
Cross-origin resource sharing
Cross-origin resource sharing (CORS for short) is a standard cross-origin solution provided by HTML5. BOS currently supports the CORS standard to achieve cross-origin access. For an introduction to cross-origin access, please refer to [Cross-Origin Access](BOS/Developer Guide/Bucket Basic Operations/Setting Cross-Origin Resource Sharing (CORS).md).
Set CORS rules
Cross-domain rules can be set in the following two ways:
1// 1. In the form of a Json string
2String 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}]}";
3SetBucketCorsRequest request = new SetBucketCorsRequest();
4request.setJsonBucketCors(jsonBucketCors);
5request.withBucketName("yourBucketName");
6client.setBucketBucketCors(request);
1// 2. Users only need to specify the parameters
2SetBucketCorsRequest request = new SetBucketCorsRequest();
3request.setBucketName("yourBucketName");
4List<CorsConfiguration> corsConfigurations = new ArrayList<CorsConfiguration>();
5List<String> allowedOrigins = new ArrayList<String>();
6List<AllowedMethods> allowedMethods = new ArrayList<AllowedMethods>();
7List<String> allowedHeaders = new ArrayList<String>();
8List<String> allowedExposeHeaders = new ArrayList<String>();
9CorsConfiguration corsConfiguration = new CorsConfiguration();
10CorsConfiguration corsConfiguration1 = new CorsConfiguration();
11// AllowedOrigins
12allowedOrigins.add("http://www.example.com");
13allowedOrigins.add("http://www.example1.com");
14allowedMethods.add(AllowedMethods.GET);
15allowedMethods.add(AllowedMethods.POST);
16allowedMethods.add(AllowedMethods.PUT);
17allowedMethods.add(AllowedMethods.DELETE);
18// allowedHeaders
19allowedHeaders.add("head1");
20allowedHeaders.add("head2");
21// allowedExposeHeaders
22allowedExposeHeaders.add("ETag");
23allowedExposeHeaders.add("Content-Length");
24allowedExposeHeaders.add("x-bce-next-append-offset");
25allowedExposeHeaders.add("x-bce-object-type");
26corsConfiguration.setAllowedOrigins(allowedOrigins);
27corsConfiguration.setAllowedMethods(allowedMethods);
28corsConfiguration.setAllowedHeaders(allowedHeaders);
29corsConfiguration.setAllowedExposeHeaders(allowedExposeHeaders);
30corsConfiguration.setMaxAgeSeconds(1800);
31List<String> allowedOrigins1 = new ArrayList<String>();
32List<AllowedMethods> allowedMethods1 = new ArrayList<AllowedMethods>();
33List<String> allowedHeaders1 = new ArrayList<String>();
34List<String> allowedExposeHeaders1 = new ArrayList<String>();
35corsConfigurations.add(corsConfiguration);
36// AllowedOrigins
37allowedOrigins1.add("allowedOrigin1");
38allowedOrigins1.add("allowedOrigin2");
39// allowedMethods
40allowedMethods1.add(AllowedMethods.GET);
41allowedMethods1.add(AllowedMethods.POST);
42allowedMethods1.add(AllowedMethods.PUT);
43allowedMethods1.add(AllowedMethods.DELETE);
44// allowedHeaders
45allowedHeaders1.add("head3");
46allowedHeaders1.add("head4");
47// allowedExposeHeaders
48allowedExposeHeaders1.add("ETag");
49allowedExposeHeaders1.add("Content-Length");
50allowedExposeHeaders1.add("x-bce-next-append-offset");
51allowedExposeHeaders1.add("x-bce-object-type");
52corsConfiguration1.setAllowedOrigins(allowedOrigins1);
53corsConfiguration1.setAllowedMethods(allowedMethods1);
54corsConfiguration1.setAllowedHeaders(allowedHeaders1);
55corsConfiguration1.setAllowedExposeHeaders(allowedExposeHeaders1);
56corsConfiguration1.setMaxAgeSeconds(3600);
57corsConfigurations.add(corsConfiguration1);
58request.setCorsConfigurationsList(corsConfigurations);
59client.setBucketBucketCors(request);
Note:
- Before using this API to set CORS rules, the bucket's CORS permission is configured to disallow cross-origin operations.
- Each bucket permits only a single rule file. Therefore, uploading a new rule file will overwrite any existing rules.
- The size limit for a CORS rule file is 20KB. Requests exceeding this size will result in a 400 Bad Request error with the EntityTooLarge code.
Get CORS rules
Cross-domain rules can be got in the following way:
1GetBucketCorsRequest request = new GetBucketCorsRequest();
2request.setBucketName("yourBucketName");
3GetBucketCorsResponse response = client.getBucketCros(request);
Delete CORS rules
Cross-domain rules can be deleted in the following way:
1DeleteBucketCorsRequest request = new DeleteBucketCorsRequest("yourBucketName");
2client.deleteBucketCors(request);
Set access logs
The BOS SDK supports recording request logs when users access the bucket, including information such as the requester, bucket name, request time, and request operation. For a detailed functional description of bucket logs, please refer to [Setting Access Logs](BOS/Developer Guide/Bucket Basic Operations/Set access logs.md)
Enable bucket logs
- Request parameters
| Name | Description | Types | Required or not |
|---|---|---|---|
| targetBucket | Specified bucket for storing access logs | String | Yes |
| targetPrefix | Specify the prefix of the ultimately saved access log file. | String | No (it is recommended that this field should be filled in for distinguishing access logs) |
- Currently, there are two ways for users to use it
- In the form of a JSON string
- Users only need to specify the parameters
Log rules can be set in the following two ways:
1// 1. In the form of a Json string
2SetBucketLoggingRequest setBucketLoggingRequest = new SetBucketLoggingRequest();
3setBucketLoggingRequest.setBucketName("yourBucketName");
4String jsonPutBucketLogging = "{\"targetBucket\":\"yourTargetBucketName\""+",\"targetPrefix\":\"mylog1/\"}";
5setBucketLoggingRequest.setJsonPutBucketLogging(jsonPutBucketLogging);
6client.setBucketLogging(setBucketLoggingRequest);
1// 2. Users only need to specify the parameters
2SetBucketLoggingRequest request = new SetBucketLoggingRequest();
3request.setBucketName("yourBucketName");
4request.setTargetBucket("yourTargetBucketName");
5request.setTargetPrefix("mylog/");
6client.setBucketLogging(request);
View bucket log settings
Log rules can be got in the following way:
1GetBucketLoggingRequest request = new GetBucketLoggingRequest();
2request.withBucketName("yourBucketName");
3GetBucketLoggingResponse response = client.getBucketLogging(request);
Disable bucket logs
Log rules can be turned off in the following way:
1DeleteBucketLoggingRequest request = new DeleteBucketLoggingRequest("yourBucketName");
2client.deleteBucketLogging(request);
Multimedia cloud processing
Currently, uploading files in mp4 format is supported, and the system automatically saves them in hls format for reading by m3u8-style players. For detailed rules and constraints, see [Audio and Video Processing API](BOS/API Reference/Audio and Video Processing Interface.md).
Set up audio and video processing
The BOS multimedia cloud processing API is defined as follows:
- Format conversion parameter
| Parameter name | Abbreviation | Types | Value | Command description | Required or not |
|---|---|---|---|---|---|
| format | f | string | [hls] | Convert the video to a specified format | Yes |
| time | t | int | [6,600] | Segment duration, defaulting to 10 s | No |
- Persistent storage parameter
| Parameter name | Abbreviation | Types | Value | Command description | Required or not |
|---|---|---|---|---|---|
| object | o | string | - | The object name encoded with base64 is limited by the length of the object, which is no more than 1,024 characters (here is the name of the m3u8 file after transcoding, such as xxx.m3u8) | Yes |
Set it in the following way through the SDK:
1File file = new File("vedioProcessTest.mp4");
2String bucketName = "yourBucketName";
3String mp4ObjectKey = "vedioProcessTest.mp4";
4String m3u8Path = "video/demo.m3u8";
5String encodeToString = Base64.getEncoder().encodeToString(m3u8Path.getBytes(Charset.defaultCharset()));
6PutObjectRequest request = new PutObjectRequest(bucketName, mp4ObjectKey, file);
7request.setVideoProcess("video/format,f_hls|system/save,o_" + encodeToString);
8PutObjectResponse response = client.putObject(request);
