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. When setting a lifecycle rule for a bucket, attention should be paid to the usage of the following parameters:
| Rule item | Description | Required or not | Remarks |
|---|---|---|---|
| id | Rule identifier | Required | Rule IDs must be unique within a bucket and cannot be duplicated. If a user does not specify an ID, the system will generate one automatically. |
| status | Rule status | Required | The value is either “enabled” or “disabled”; the rule will not take effect when it is set to “disabled”. |
| resource | Resources to which the rules apply | Required | Example: To apply to objects in “samplebucket” with the prefix “prefix/”: samplebucket/prefix/* |
| condition | The condition on which the rule depends | Required | Currently, only the “time” format is supported |
| +time | Time constraints | Required | It is implemented via the defined dateGreaterThan. |
| ++dateGreaterThan | Describe time relationships | Required | Both absolute time in "date" format and relative time in "days" are supported. The absolute time (date) format is yyyy-mm-ddThh:mm:ssZ (e.g., 2016-09-07T00:00:00Z) and is in UTC, always ending at 00:00:00 (UTC 0:00). Relative time "days" follows ISO8601, with a minimum granularity of one day. For instance, $(lastModified)+P7D represents 7 days after the object's last-modified time. |
| action | Action executed on the resource | Required | - |
| +name | Name of the executed operation | Required | The value can be Transition, DeleteObject, or AbortMultipartUpload |
| +storageClass | Storage class of the object | Optional | It can be set when the action is Transition, and the value is either STANDARD_IA or COLD, indicating a transition from the original storage class to infrequent access storage or cold storage |
Set lifecycle rules
A lifecycle rule can be set through the following code:
1// import "github.com/baidubce/bce-sdk-go/bce"
2ruleStr := `{
3 "rule": [
4 {
5 "id": "delete-rule-1",
6 "status": "enabled",
7 "resource": ["my-bucket/abc*"],
8 "condition": {
9 "time": {
10 "dateGreaterThan": "2018-01-01T00:00:00Z"
11 }
12 },
13 "action": {
14 "name": "DeleteObject"
15 }
16 }
17 ]
18}`
19 // 1. Set via the stream calling API
20body, _ := bce.NewBodyFromString(ruleStr)
21err := bosClient.PutBucketLifecycle(bucketName, body)
22 // 2. Pass in the string directly
23err := bosClient.PutBucketLifecycleFromString(bucketName, ruleStr)
View lifecycle rules
The lifecycle rules in a bucket can be viewed through the following code:
1res, err := bosClient.GetBucketLifecycle(bucketName)
2fmt.Printf("%+v\n", res.Rule)
Delete lifecycle rules
A lifecycle rule can be cleared through the following code:
1err := bosClient.DeleteBucketLifecycle(bucketName)
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
Users can set CORS rules for a bucket, supporting settings via json strings, files, streams, and objects:
1// import "github.com/baidubce/bce-sdk-go/service/bos/api"
2 // 1. Set via the streaming calling API
3err := bosClient.PutBucketCors(bucketName, body)
4 // 2. Pass in the string directly
5err := bosClient.PutBucketCorsFromString(bucketName, corsString)
6 // 3. Pass in the CORS file name
7err := bosClient.PutBucketCorsFromFile(bucketName, corsFile)
8 // 4. Pass in the object
9corsObj := api.BucketCORSType{
10 AllowedOrigins: []string{"example.com"},
11 AllowedMethods: []string{"HEAD", "GET"},
12 AllowedHeaders: []string{"*"},
13 AllowedExposeHeaders: []string{"user-custom-expose-header"},
14 MaxAgeSeconds: 3600,
15}
16err := bosClient.PutBucketCorsFromStruct(src, &api.PutBucketCorsArgs{CorsConfiguration:[]api.BucketCORSType{corsObj}})
Get CORS rules
Users can get the CORS rules of a specified bucket:
1result, err := bosClient.GetBucketCors(bucketName)
The definition of the result object is the same as the request parameters of the PutBucketCorsFromStruct API.
Delete CORS rules
The following code can be used to delete the CORS rules of a bucket. After deletion, the bucket will no longer support cross-origin access.
1err := bosClient.DeleteBucketCors(bucketName)
Manage storage classes
Each bucket has a specific storage class. If no storage class is specified when uploading an object, the object will inherit the bucket's default storage class.
Set bucket storage class
The default storage class of a bucket is standard mode, and users can set it through the following code:
1storageClass := "STANDARD_IA"
2err := bosClient.PutBucketStorageclass(bucketName, storageClass)
Get bucket storage class
The following code can be used to view the default storage class of a bucket:
1storageClass, err := bosClient.GetBucketStorageclass(bucketName)
Set access logs
The BOS GO SDK supports recording request logs when users access a bucket, and users can specify the location where the access logs of the bucket are stored. The logs will include 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
Users can enable the bucket logging feature by specifying a bucket for storing logs and setting a prefix for log files. The sample code below demonstrates how to configure the location and prefix for access logs:
1// import "github.com/baidubce/bce-sdk-go/bce"
2 // 1. Set from a JSON string
3loggingStr := `{"targetBucket": "logging-bucket", "targetPrefix": "my-log/"}`
4err := bosClient.PutBucketLoggingFromString(bucketName, loggingStr)
5 // 2. Set from a parameter object
6args := new(api.PutBucketLoggingArgs)
7args.TargetBucket = "logging-bucket"
8args.TargetPrefix = "my-log/"
9err := bosClient.PutBucketLoggingFromStruct(bucketName, args)
10 // 3. Read a json-format file for setting
11loggingStrem := bce.NewBodyFromFile("<path-to-logging-setting-file>")
12err := bosClient.PutBucketLogging(bucketName, loggingStream)
View bucket log settings
The following code shows how to get the log configuration information of a given bucket:
1res, err := bosClient.GetBucketLogging(bucketName)
2fmt.Println(res.Status)
3fmt.Println(res.TargetBucket)
4fmt.Println(res.TargetPrefix)
Disable bucket logs
To disable the log function of a bucket, simply call the deletion API:
1err := bosClient.DeleteBucketLogging(bucketName)
Server-side encryption
Set server-side encryption function
Users can enable server-side encryption for a bucket. This ensures that all data stored in the bucket is encrypted for better security.
1err := bosClient.PutBucketEncryption(bucketName, algorithm)
The algorithm parameter is the encryption algorithm, and currently only the “AES256” encryption algorithm is supported.
Get server-side encryption
Users can call the following API to get the server-side encryption function of a bucket:
1algorithm, err := bosClient.GetBucketEncryption(bucketName)
Delete server-side encryption function
Users can call the following API to delete the server-side encryption function of the specified bucket:
1err := bosClient.DeleteBucketEncryption(bucketName)
Original image protection function
Users can enable the original image protection feature for images stored in a bucket by specifying the resources to be protected.
Enable original image protection function
The original image protection function can be enabled through the following code:
1resources := []stirng{
2 "bucket/prefix/*",
3 "bucket/*/suffix",
4 }
5err := bosClient.PutBucketCopyrightProtection(bucketName, resources...)
The resources parameter indicates the scope of image protection, which can specify a prefix or suffix; "bucket/*" indicates the entire bucket.
Get original image protection settings
Users can get the resources for which original image protection is set in a bucket through the following sample code:
1resources, err := bosClient.GetBucketCopyrightProtection(bucketName)
Delete original image protection configuration
1err := bosClient.DeleteBucketCopyrightProtection(bucketName)
Static website hosting
Enable static website hosting
Users can set a bucket to enable the static website hosting function through the following sample code:
1// import "github.com/baidubce/bce-sdk-go/service/bos/api"
2 // 1. Set via the streaming calling API
3err := bosClient.PutBucketStaticWebsite(bucketName, body)
4 // 2. Pass in the json string directly
5jsonStr := `{"index": "index.html", "notFound": "404.html"}`
6err := bosClient.PutBucketStaticWebsiteFromString(bucketName, jsonStr)
7 // 3. Pass in the object
8args := &api.BucketStaticWebsiteType{
9 Index: "index.html",
10 NotFound: "404.html",
11}
12err := bosClient.PutBucketStaticWebsiteFromStruct(bucketName, (*api.PutBucketStaticWebsiteArgs)(args))
13 // 4. Set via a simple API
14err := bosClient.SimplePutBucketStaticWebsite(bucketName, "index.html", "404.html")
Get static website hosting settings
Users can get the static website hosting settings of a specified bucket through the following code:
1result, err := bosClient.GetBucketStaticWebsite(bucketName)
2fmt.Println(result.Index)
3fmt.Println(result.NotFound)
Delete static website hosting settings
The static website hosting function of a specified bucket can be deleted through the following sample code:
1err := bosClient.DeleteBucketStaticWebsite(bucketName)
Cross-region replication
BOS provides a cross-region replication function. For a bucket created by a user in a certain physical region, for data security or other purposes, users can configure to replicate the entire bucket’s data to one or more physical regions.
Enable bucket cross-region replication function
Users can enable the cross-region replication function of a bucket through the following code:
1// 1. json string
2jsonStr := `{
3 "status":"enabled",
4 "resource":[
5 "bucket/abc",
6 "bucket/cd*"
7 ],
8 "destination": {
9 "bucket":"bucket-name",
10 "storageClass":"COLD"
11 },
12 "replicateHistory": {
13 "storageClass":"COLD"
14 },
15 "replicateDeletes":"enabled",
16 "id":"sample-bucket"
17}`
18err := bosClient.PutBucketReplicationFromString(bucketName, jsonStr, "sample-bucket")
19 // 2. Use the configuration file name
20err := bosClient.PutBucketReplicationFromFile(bucketName, configFile, "sample-bucket")
21 // 3. Use the parameter object
22argsObj := &api.PutBucketReplicationArgs{
23 Id: "sample-bucket",
24 Status: "enabled",
25 Resource: []string{"bucket/abc"},
26 ReplicateDeletes: "enabled",
27 Destination: &api.BucketReplicationDescriptor{"bucket-abc", "COLD"},
28 ReplicateHistory: &api.BucketReplicationDescriptor{"bucket-abc", "COLD"},
29}
30err := bosClient.PutBucketReplicationFromStruct(bucketName, argsObj, argsObj.Id)
31 // 4. Use the stream
32err := bosClient.PutBucketReplication(bucketName, bodyStream, "sample-bucket")
Get bucket cross-region replication configuration
The following sample code allows users to retrieve a bucket’s cross-region replication configuration. The returned result aligns with the fields defined in the put API.
1result, err := bosClient.GetBucketReplication(bucketName, "sample-bucket")
Delete bucket cross-region replication configuration
Users can delete the cross-region replication function of a bucket using the following code:
1err := bosClient.DeleteBucketReplication(bucketName, "sample-bucket")
Get cross-region replication progress
Since cross-region replication requires background asynchronous replication operations, users can query the current replication progress through the following API:
1result, err := bosClient.GetBucketReplicationProgress(bucketName, "sample-bucket")
The returned result object contains the following fields:
Status(string): Current statusHistoryReplicationPercent(float64): Current replication progressLatestReplicationTime(string): The time of the latest replication execution
Get replication rule list
Users can use the following sample code to get the bucket replication rule list
1list, err := bosClient.ListBucketReplication(srcBucket)
Mirror back-to-origin
Enable mirror-back-to-origin
Users can set a bucket to enable the mirror-back-to-origin function through the following sample code:
1 mirrorRule := &api.MirrorConfigurationRule{
2 Prefix: "prefix",
3 SourceUrl: "http://bj.bcebos.com",
4 Mode: "fetch",
5 StorageClass: "STANDARD",
6 PassQueryString: false,
7 PassHeaders: []string{"header3", "header4"},
8 IgnoreHeaders: []string{"header3", "header4"},
9 Resource: "*",
10 PrefixReplace: "test1",
11 Version: "v2",
12 }
13 mirrorArgs := &api.PutBucketMirrorArgs{
14 BucketMirroringConfiguration: []api.MirrorConfigurationRule{*mirrorRule},
15 }
16 err = bosClient.PutBucketMirror(bucketName, mirrorArgs)
Get mirror-back-to-origin settings
1 result, err := bosClient.GetBucketMirror(bucketName)
Delete mirror-back-to-origin
1err = bosClient.DeleteBucketMirror(bucketName)
Tag Management
Adding tags to a bucket
Users can bind tags to a bucket through the following sample code
1 tag1 := api.Tag{
2 Tagkey: "tagKey1",
3 TagValue: "tagValue1",
4 }
5 tag2 := api.Tag{
6 Tagkey: "tagKey2",
7 TagValue: "tagValue2",
8 }
9 tags := []api.Tag{tag1, tag2}
10 putBucketTagArgs := &api.PutBucketTagArgs{
11 Tags: tags,
12 }
13 err = bosClient.PutBucketTag(bucketName, putBucketTagArgs)
Get bucket tags
1 result, err := bosClient.GetBucketTag(bucketName)
Delete bucket tags
1err = bosClient.DeleteBucketTag(bucketName)
