我正在尝试将文件上传到 S3 并使用 Java SDK 为其设置过期日期。
这是我得到的代码:
Instant expiration = Instant.now().plus(3, ChronoUnit.DAYS);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setExpirationTime(Date.from(expiration));
metadata.setHeader("Expires", Date.from(expiration));
s3Client.putObject(bucketName, keyName, new FileInputStream(file), metadata);
该对象在 S3 控制台中没有过期数据。
我能做什么?
问候, 伊多
最佳答案
这是两个不相关的事情。控制台中显示的过期时间为 x-amz-expiration,由系统根据生命周期策略填充。它是只读的。
x-amz-expiration
Amazon S3 will return this header if an Expiration action is configured for the object as part of the bucket's lifecycle configuration. The header value includes an "expiry-date" component and a URL-encoded "rule-id" component.
https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectHEAD.html
Expires
是一个 header ,当在对象上设置该 header 时,该 header 会在下载对象时在响应中返回。
Expires
The date and time at which the object is no longer able to be cached. For more information, go to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21.
https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
无法告诉 S3 何时过期(删除)特定对象 - 这只能作为存储桶生命周期策略的一部分完成,如用户指南中 Object Lifecycle Management 下所述。 .
关于amazon-s3 - S3 java SDK - 设置对象过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48174550/