Bucket Manage

Last update:2025-08-18 15:54:14

Bucket Management

1. List Buckets

You can use WosClient.listBuckets to list all buckets. The following example shows how to retrieve the list of buckets:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create WosClient instance
WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

// List buckets
List<WosBucket> buckets = wosClient.listBuckets();
for (WosBucket bucket : buckets) {
    System.out.println("BucketName: " + bucket.getBucketName());
    System.out.println("CreationDate: " + bucket.getCreationDate());
    System.out.println("Endpoint: " + bucket.getEndpoint());
}

Note: The returned bucket list is sorted lexicographically (dictionary order) by bucket name.

2. Check if a Bucket Exists

You can use WosClient.headBucket to check whether a specified bucket exists. The following example demonstrates how to check for a bucket’s existence:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create WosClient instance
WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

boolean exists = wosClient.headBucket("bucketname");

Lifecycle Management

WOS allows you to configure lifecycle rules for buckets to automate storage class transitions and the deletion of expired objects. This helps you make efficient use of storage and optimize your bucket management.

You can define multiple rules for objects with different prefixes. Each rule includes the following elements:

  • Rule ID (must be unique)
  • Object prefix (only objects matching the prefix are affected by the rule)
  • Transition policy for the latest object version:
    • Set the number of days after object creation to transition to a specified storage class, or
    • Specify an exact date to transition to a specified storage class
  • Expiration policy for the latest object version:
    • Set the number of days after object creation for expiration, or
    • Specify an exact expiration date
  • Enable/disable flag

1. Set Lifecycle Rules

You can set lifecycle rules for a bucket using WosClient.setBucketLifecycleConfiguration.

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create WosClient instance
WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

final String ruleId = "delete obsoleted files";
final String matchPrefix = "obsoleted/";

LifecycleConfiguration lifecycleConfig = new LifecycleConfiguration();
LifecycleConfiguration.Rule rule = lifecycleConfig.new Rule();
rule.setEnabled(true);
rule.setId(ruleId);
rule.setPrefix(matchPrefix);
LifecycleConfiguration.Expiration expiration = lifecycleConfig.new Expiration();
expiration.setDays(10);

rule.setExpiration(expiration);
lifecycleConfig.addRule(rule);

System.out.println("Setting bucket lifecycle\n");
HeaderResponse headerResponse = wosClient.setBucketLifecycleConfiguration(bucketName, lifecycleConfig);
System.out.println(headerResponse);

2. View Lifecycle Rules

You can retrieve lifecycle rules configured for a bucket using WosClient.getBucketLifecycle. The following example demonstrates how to view lifecycle rules:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create WosClient instance
WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

LifecycleConfiguration config = wosClient.getBucketLifecycle("bucketname");

for (Rule rule : config.getRules()) {
    System.out.println(rule.getId());
    System.out.println(rule.getPrefix());
    for (Transition transition : rule.getTransitions()) {
        System.out.println(transition.getDays());
        System.out.println(transition.getStorageClass());
    }
    System.out.println(rule.getExpiration() != null ? rule.getExpiration().getDays() : "");
}

3. Delete Lifecycle Rules

You can delete all lifecycle rules for a bucket using WosClient.deleteBucketLifecycle. The following example demonstrates how to remove lifecycle rules from a bucket:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create WosClient instance
WosClient wosClient = new WosClient(ak, sk, endPoint, regionName);

wosClient.deleteBucketLifecycle("bucketname");
Is the content of this document helpful to you?
Yes
I have suggestion
Submitted successfully! Thank you very much for your feedback, we will continue to strive to do better!