Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用 AWS SDK for Java 还原存档对象

下面的任务将指导您完成使用AWS SDK for Java启动存档对象的还原操作的过程。

下载对象

1

创建 AmazonS3Client 类的实例。

2

通过提供存储桶名称、用于恢复的对象键和您要用于恢复对象副本的天数,创建 RestoreObjectRequest 类的实例。

3

执行 AmazonS3.RestoreObject 方法之一,以启动存档恢复。

以下 Java 代码示例演示了上述任务。

Copy
String bucketName = "examplebucket"; String objectkey = "examplekey"; AmazonS3Client s3Client = new AmazonS3Client(); RestoreObjectRequest request = new RestoreObjectRequest(bucketName, objectkey, 2); s3Client.restoreObject(request);

Amazon S3 将在对象元数据中保存还原状态。您可以检索对象元数据并检查 RestoreInProgress 属性的值,如下面的 Java 代码段所示。

Copy
String bucketName = "examplebucket"; String objectkey = "examplekey"; AmazonS3Client s3Client = new AmazonS3Client(); client = new AmazonS3Client(); GetObjectMetadataRequest request = new GetObjectMetadataRequest(bucketName, objectKey); ObjectMetadata response = s3Client.getObjectMetadata(request); Boolean restoreFlag = response.getOngoingRestore(); System.out.format("Restoration status: %s.\n", (restoreFlag == true) ? "in progress" : "finished");

下面的 Java 代码示例为指定的存档对象启动了恢复请求。您必须更新代码并提供存储桶名称和存档的对象键名称。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

Copy
import java.io.IOException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.AmazonS3Exception; import com.amazonaws.services.s3.model.GetObjectMetadataRequest; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.RestoreObjectRequest; public class RestoreArchivedObject { public static String bucketName = "*** Provide bucket name ***"; public static String objectKey = "*** Provide object key name ***"; public static AmazonS3Client s3Client; public static void main(String[] args) throws IOException { AmazonS3Client s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); try { RestoreObjectRequest requestRestore = new RestoreObjectRequest(bucketName, objectKey, 2); s3Client.restoreObject(requestRestore); GetObjectMetadataRequest requestCheck = new GetObjectMetadataRequest(bucketName, objectKey); ObjectMetadata response = s3Client.getObjectMetadata(requestCheck); Boolean restoreFlag = response.getOngoingRestore(); System.out.format("Restoration status: %s.\n", (restoreFlag == true) ? "in progress" : "finished"); } catch (AmazonS3Exception amazonS3Exception) { System.out.format("An Amazon S3 error occurred. Exception: %s", amazonS3Exception.toString()); } catch (Exception ex) { System.out.format("Exception: %s", ex.toString()); } } }