Baidu AI Cloud
中国站

百度智能云

Object Storage

Data Processing and Use

Lifecycle Management

BOS supports users to set lifecycle rules for bucket to clear the expired files automatically and save storage space. For files of different prefixes, users can set more rules simultaneously. When setting a lifecycle rule for bucket, pay attention to the usage of the following parameters:

Rule item Description Is it necessary to fill in Comments
id Identifier of rule Required Rule id in the same bucket must be unique and cannot be repeated. If the user does not fill in, the system will automatically generate one for the user.
status State of rule Required The value is enabled or disabled, and when the rule is disabled, it is invalid.
resource Rule is effective to which resources Required For example: Effective for object prefixed with prefix/in samplebucket: samplebucket/prefix/*
condition Conditions on which the rule relies Required Currently, only time form is supported
+time Time constraints Required Realized via the defined dateGreaterThan
++dateGreaterThan Describe time relationship Required Support absolute time date and relative time days. The format of absolute time date is yyyy-mm-ddThh: mm: ssZ, eg. 2016-09-07T00: 00: 00Z. The absolute time is UTC time and must end with 0:00:00 (UTC 0 o'clock); the description of relative time days follows ISO8601, and the minimum time granularity supported is day, such as: $ (lastModified) + P7D represents the time of object 7 days after last-modified.
action Action to perform on resource. Required -
+name Name of operation executed Required The values are Transition, Deleteobject, AbortMultipartUpload.
+storageClass Storage type of object Optional It is settable when action is Transition with value of STANDARD_IA or COLD, indicating the conversion from original storage type to infrequency storage or cold storage.

Set Lifecycle Rule

A lifecycle rule can be set via the following codes:

// import "github.com/baidubce/bce-sdk-go/bce"

ruleStr := `{
    "rule": [
        {
            "id": "delete-rule-1",
            "status": "enabled",
            "resource": ["my-bucket/abc*"],
            "condition": {
                "time": {
                    "dateGreaterThan": "2018-01-01T00:00:00Z"
                }
            },
            "action": {
                "name": "DeleteObject"
            }
        }
    ]
}`

// 1. Set via stream calling interface
body, _ := bce.NewBodyFromString(ruleStr)
err := bosClient.PutBucketLifecycle(bucketName, body)

// 2. Introduce string directly
err := bosClient.PutBucketLifecycleFromString(bucketName, ruleStr)

View Lifecycle Rule

Lifecycle rules in bucket can be viewed via the following codes

res, err := bosClient.GetBucketLifecycle(bucketName)
fmt.Printf("%+v\n", res.Rule)

Delete Lifecycle Rule

Lifecycle rules can be cleared via the following codes:

err := bosClient.DeleteBucketLifecycle(bucketName)

Cross-origin Resource Sharing

Cross-Origin Resource Sharing (CORS) is a standard cross-origin solution provided by HTML5, and BOS supports CORS standard for cross-origin access currently. For the introduction of cross-origin access, please see Cross-origin Access.

Set CORS Rule

You can set CORS rules for bucket, and the rules support setting via json string, file, stream and object:

// import "github.com/baidubce/bce-sdk-go/service/bos/api"

// 1. Set via stream calling interface 
err := bosClient.PutBucketCors(bucketName, body)

// 2. Introduce string directly
err := bosClient.PutBucketCorsFromString(bucketName, corsString)

// 3. Introduce CORS file name
err := bosClient.PutBucketCorsFromFile(bucketName, corsFile)

// 4. Introduce object 
corsObj := api.BucketCORSType{
	corsObj := api.BucketCORSType{
    AllowedOrigins: []string{"example.com"},
    AllowedMethods: []string{"HEAD", "GET"},
    AllowedHeaders: []string{"*"},
    AllowedExposeHeaders: []string{"user-custom-expose-header"},
    MaxAgeSeconds: 3600,
}
err := bosClient.PutBucketCorsFromStruct(src, &api.PutBucketCorsArgs{CorsConfiguration:[]api.BucketCORSType{corsObj}})

Get CORS Rule

You can obtain the CORS rules of specified bucket.

result, err := bosClient.GetBucketCors(bucketName)

The definition of result object is the same with the request parameter of PutbucketCorsFromStruct interface.

Delete CORS Rule

CORS rules of bucket can be deleted by reference the following codes, and the deleted bucket cannot be cross-origin accessed.

err := bosClient.DeleteBucketCors(bucketName)

Type of Management Storage

Each bucket has its own storage type, and if no storage type is specified during upload of object under the bucket, the storage type of that bucket will be inherited by default.

Set Storage Type of Bucket

The default storage type of bucket is standard mode, and users can set the type via the following codes:

storageClass := "STANDARD_IA"
err := bosClient.PutBucketStorageclass(bucketName, storageClass)

Get Bucket Storage Type

The following codes enable it to view the default storage type of a bucket:

storageClass, err := bosClient.GetBucketStorageclass(bucketName)

Set Access Log

BOS GO SDK supports recording request record when users access to bucket as log, and users can specify the log storage location of bucket. Logs include requester, bucket name, request time, request time, etc. For the detailed function of bucket log, see Set Access Log.

Enable Bucket Log

You can enable bucket log function by setting bucket used to place log and prefix of log file. The following sample codes enable it to set the location and prefix of access log:

// import "github.com/baidubce/bce-sdk-go/bce"

// 1. Set from JSON string 
loggingStr := `{"targetBucket": "logging-bucket", "targetPrefix": "my-log/"}`
err := bosClient.PutBucketLoggingFromString(bucketName, loggingStr)

// 2. Set from parameter object
args := new(api.PutBucketLoggingArgs)
args.TargetBucket = "logging-bucket"
args.TargetPrefix = "my-log/"
err := bosClient.PutBucketLoggingFromStruct(bucketName, args)

// 3. Set from the read file of json format 
loggingStrem := bce.NewBodyFromFile("<path-to-logging-setting-file>")
err := bosClient.PutBucketLogging(bucketName, loggingStream)

View Setting of Bucket Log

The following codes give the information on how to obtain the log configuration information assigned to bucket:

res, err := bosClient.GetBucketLogging(bucketName)
fmt.Println(res.Status)
fmt.Println(res.TargetBucket)
fmt.Println(res.TargetPrefix)

Disable Bucket Log

To disable the log function of bucket, it is only needed to delete interface:

err := bosClient.DeleteBucketLogging(bucketName)

Encryption of Server

Set Server Encryption Function

You can set the function of enabling service encryption for a bucket, and all data stored in that bucket are encrypted to ensure data security.

err := bosClient.PutBucketEncryption(bucketName, algorithm)

algorithm parameter is an encryption algorithm, which only supports "AES256" encryption algorithm currently.

Get Encryption of Server

You can call the following interfaces to obtain server encryption function of bucket:

algorithm, err := bosClient.GetBucketEncryption(bucketName)

Delete Server Encryption Function

You can call the following interfaces to delete the server encryption function of bucket:

err := bosClient.DeleteBucketEncryption(bucketName)

Original Photo Protection

You can set original photo protection for the pictures stored under bucket, and users need to specify the resources to be protected.

Enable Original Photo Protection

The original photo protection is enabled via the following codes:

err := bosClient.PutBucketCopyrightProtection(bucket, resources)

resources parameter is variable, and more parameters can be specified.

Get Setting of Original Photo Protection

You can obtain the original photo protection resources set by bucket via the following sample codes:

resources, err := bosClient.GetBucketCopyrightProtection(bucketName)

Delete Original Photo Protection Configuration

err := bosClient.DeleteBucketCopyrightProtection(bucketName)

Static Website Hosting

Enable Static Website Hosting

You can set a bucket to enable static website hosting through the following sample codes:

// import "github.com/baidubce/bce-sdk-go/service/bos/api"

// 1. Set via stream calling interface
err := bosClient.PutBucketStaticWebsite(bucketName, body)

// 2. Introduce json string directly 
jsonStr := `{"index": "index.html", "notFound": "404.html"}`
err := bosClient.PutBucketStaticWebsiteFromString(bucketName, jsonStr)

// 3. Introduce object
args := &api.BucketStaticWebsiteType{
	Index: "index.html",
	NotFound: "404.html",
}
err := bosClient.PutBucketStaticWebsiteFromStruct(bucketName, args)

// 4. Simple interface setting 
err := bosClient.SimplePutBucketStaticWebsite(bucketName, "index.html", "404.html")

Get the Setting of Static Website Hosting

You obtain the setting of static website hosting of specified bucket via the following codes:

result, err := bosClient.SimplePutBucketStaticWebsite(bucketName)
fmt.Println(result.Index)
fmt.Println(result.NotFound)

Delete Setting of Static Website Hosting

Delete the static website hosting function of specified bucket via the following sample codes:

err := bosClient.DeleteBucketStaticWebsite(bucketName)

Cross-origin Replication

BOS provides a cross-origin replication function, and for the bucket created by users in a physical origin, for security and other purposes, you can configure the replication of all bucket data to another physical origin.

Enable Bucket Cross-origin Replication Function

You can enable the cross-origin replication function of bucket via the following codes:

// 1. json string
jsonStr := `{
  "status":"enabled",
  "resource":[
    "bucket/abc",
    "bucket/cd*",
  ],
  "destination": {
    "bucket":"bucket-name",
    "storageClass":"COLD"
  },
  "replicateHistory": {
    "bucket":"bucket-name",
    "storageClass":"COLD"
  },
  "replicateDeletes":"enabled",
  "id":"sample-bucket-replication-config"
}`
err := bosClient.PutBucketReplicationFromString(bucketName, jsonStr)

// 2. Use configuration file name
err := bosClient.PutBucketReplicationFromFile(bucketName, configFile)

// 3. Use parameter object
argsObj := &api.PutBucketReplicationArgs{
	Id: "sample-bucket-replication-config",
	Status: "enabled",
	Resource: []string{"bucket/abc"},
	ReplicateDeletes: "enabled",
	Destination: &api.BucketReplicationDescriptor{"bucket-abc", "COLD"},
	ReplicateHistory: &api.BucketReplicationDescriptor{"bucket-abc", "COLD"},
}
err := bosClient.PutBucketReplicationFromStruct(bucketName, argsObj)

// 4. Use stream 
err := bosClient.PutBucketReplication(bucketName, bodyStream)

Get the Configuration of Bucket Cross-origin Replication

You can obtain the configuration of bucket cross-origin replication with the following sample codes, and the result returned is the same with Put interface field

result, err := bosClient.GetBucketReplication(bucketName)

Delete Bucket Cross-origin Replication Configuration

You can delete bucket cross-origin replication function with the following sample codes:

err := bosClient.DeleteBucketReplication(bucketName)

Get Cross-origin Replication to Progress

As cross-origin replication requires the background for asynchronous replication, users can inquire the current replication to function via the following interfaces:

result, err := bosClient.GetBucketReplicationProgress(bucketName)

The returned result object contains the following fields

  • Status(string): current status
  • HistoryReplicationPercent(float64): current replication to progress
  • LatestReplicationTime(string): time of the latest replication

Get the list of replication rules

Users can use the following example code to get a list of bucket replication rules

list, err := bosClient.ListBucketReplication(srcBucket)
Previous
File Management
Next
Error Processing