Help Center> Object Storage Service> Android> Object Upload> Configuring Lifecycle Management
Updated on 2024-06-21 GMT+08:00

Configuring Lifecycle Management

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

When uploading an object or initializing a multipart upload, you can directly set the expiration time for the object. Sample code is as follows:

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
 try {
      PutObjectRequest request = new PutObjectRequest();
      request.setBucketName("examplebucket");
      request.setObjectKey("objectname");
      request.setFile(new File("localfile"));
      // When uploading an object, configure it to expire 30 days after being uploaded.
      request.setExpires(30);
      obsClient.putObject(request);
      // InitiateMultipartUploadRequest request2 = new InitiateMultipartUploadRequest("bucketname", "objectname"); 
      //// When initiating a multipart upload, configure the object to expire 60 days after being created from part assembling.
      // request2.setExpires(60); 
      //obsClient.initiateMultipartUpload(request);
      System.out.println("putObject successfully");
   } catch (ObsException e) {
      System.out.println("putObject failed");
      // Request failed. Print the HTTP status code.
      System.out.println("HTTP Code:" + e.getResponseCode());
      // Request failed. Print the server-side error code.
      System.out.println("Error Code:" + e.getErrorCode());
      // Request failed. Print the error details.
      System.out.println("Error Message:" + e.getErrorMessage());
      // Request failed. Print the request ID.
      System.out.println("Request ID:" + e.getErrorRequestId());
      System.out.println("Host ID:" + e.getErrorHostId());
      e.printStackTrace();
  } catch (Exception e) {
      System.out.println("putObject failed");
      // Print other error details.
      e.printStackTrace();
  }
  • This mode specifies the time duration in days after which an object will expire. The OBS server automatically clears expired objects.
  • The object expiration time set in this mode takes precedence over the bucket lifecycle rule.