You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage Object
Configure Object-Level Expiration Time (Java SDK)
Copy page
Download PDF
Configure Object-Level Expiration Time (Java SDK)
You can set the expiration time for an uploaded object using the setObjectExpires interface of the TOS Java SDK. After expiration, TOS will automatically delete the object.​
Notes​
  • To set an object-level expiration time, you must have the tos:SetObjectExpires permission. For detailed instructions, refer to the Permissions Configuration Guide.​
  • Set object-level expiration time using the Java SDK; the Java SDK version must be 2.9.4 or above.​
Sample Code​
​
import com.volcengine.tos.TOSV2;​
import com.volcengine.tos.TOSV2ClientBuilder;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.TosServerException;​
import com.volcengine.tos.model.object.*;​
​
import java.io.ByteArrayInputStream;​
import java.util.HashMap;​
import java.util.Map;​
​
public class SetObjectExpiresExample {​
public static void main(String[] args) {​
String endpoint = "your endpoint";​
String region = "your region";​
String accessKey = System.getenv("TOS_ACCESS_KEY");​
String secretKey = System.getenv("TOS_SECRET_KEY");​
​
String bucketName = "bucket-example";​
​
String objectKey = "example_dir/example_object.txt";​
​
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try{​
SetObjectExpiresInput setInput = SetObjectExpiresInput.builder().bucket(bucketName).key(objectKey).objectExpires(1).build();​
tos.setObjectExpires(setInput);​
​
HeadObjectV2Output headRes = tos.headObject(HeadObjectV2Input.builder().bucket(bucketName).key(objectKey).build());​
String expiration = headRes.getRequestInfo().getHeader().get("x-tos-expiration");​
System.out.println("object: " + objectKey + "expiration: " + expiration);​
​
setInput = SetObjectExpiresInput.builder().bucket(bucketName).key(objectKey).objectExpires(0).build();​
tos.setObjectExpires(setInput);​
​
headRes = tos.headObject(HeadObjectV2Input.builder().bucket(bucketName).key(objectKey).build());​
expiration = headRes.getRequestInfo().getHeader().get("x-tos-expiration");​
System.out.println("object: " + objectKey + "expiration: " + expiration);​
} catch (TosClientException e) {​
System.out.println("setObjectExpires failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (TosServerException e) {​
System.out.println("setObjectExpires failed");​
System.out.println("StatusCode: " + e.getStatusCode());​
System.out.println("Code: " + e.getCode());​
System.out.println("Message: " + e.getMessage());​
System.out.println("RequestID: " + e.getRequestID());​
} catch (Throwable t) {​
System.out.println("setObjectExpires failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
Related documentation​
For API documentation on configuring object-level expiration time, refer to SetObjectExpires.​
Last updated: 2025.09.22 11:34:18