访问冷归档数据前需要先恢复数据。本文介绍如何通过 TOS Java SDK 恢复冷归档(Cold Archive)对象。
RestoreObject
权限。以下代码展示如何使用 SDK 的 restoreObject
接口恢复桶 bucket-example 中 object-example 对象。
import com.volcengine.tos.TOSV2; import com.volcengine.tos.TOSV2ClientBuilder; import com.volcengine.tos.TosClientException; import com.volcengine.tos.TosServerException; import com.volcengine.tos.comm.common.TierType; import com.volcengine.tos.model.object.RestoreJobParameters; import com.volcengine.tos.model.object.RestoreObjectInput; import com.volcengine.tos.model.object.RestoreObjectOutput; public class RestoreObjectExample { 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"; // 对象的解冻天数 int days = 1; // 标准取回 TierType type = TierType.TIER_STANDARD; // 快速取回 // TierType type = TierType.TIER_EXPEDITED; // 批量取回 // TierType type = TierType.TIER_BULK; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try{ RestoreObjectInput input = new RestoreObjectInput().setBucket(bucketName).setKey(objectKey) .setRestoreJobParameters(new RestoreJobParameters().setTier(type)) .setDays(days); RestoreObjectOutput output = tos.restoreObject(input); System.out.println(output.getRequestInfo().getRequestId()); System.out.println("restoreObject succeed."); } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送 System.out.println("restoreObject failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息 System.out.println("restoreObject failed"); System.out.println("StatusCode: " + e.getStatusCode()); System.out.println("RequestID: " + e.getRequestID()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里 System.out.println("restoreObject failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于恢复对象的更多说明,请参见 RestoreObject。