You need to enable JavaScript to run this app.
导航
设置对象级过期时间(Java SDK)
最近更新时间:2025.09.02 16:41:28首次发布时间:2025.09.02 16:41:28
复制全文
我的收藏
有用
有用
无用
无用

您可以通过 TOS Java SDK 的 setObjectExpires 接口设置已上传对象的过期时间,过期后,TOS 将自动删除该对象。

注意事项

  • 设置对象级过期时间,您必须具备 tos:SetObjectExpires 权限,具体操作,请参见权限配置指南
  • 使用 Java SDK 设置对象级过期时间,要求 Java SDK 为 2.9.4 或以上版本。

示例代码

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{
            // 设置对象的过期时间,取值为正整数或0;为0时表示永不过期。
            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());
        }
    }
}

相关文档

关于设置对象级过期时间的 API 文档,请参见 SetObjectExpires