此页面仅适用于使用保管库的 S3 Glacier 服务的现有客户以及 2012 年以RESTAPI来的原始客户。
如果您正在寻找档案存储解决方案,我们建议您在亚马逊 S3、S3 Glacier 即时检索、S3 Glacier 灵活检索和 S3 Glacier Deep Archive Dee p Archive 中使用 S3 Glacier 存储类。要了解有关这些存储选项的更多信息,请参阅 Amazon S3 用户指南中的 S3 Glacier 存储类
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon SDK for Java 在 Amazon S3 Glacier 中创建文件库
该低级 API 为所有文件库操作提供了方法,包括创建和删除文件库、获取文件库描述,以及获取特定 Amazon Web Services 区域创建的文件库的列表。以下是使用Amazon SDK for Java创建文件库的步骤。
-
创建
AmazonGlacierClient
类(客户端)的实例。您需要指定要创建文件库的 Amazon Web Services 区域。您使用此客户端执行的所有操作都会应用到该 Amazon Web Services 区域。
-
通过创建一个
CreateVaultRequest
类的实例提供请求信息。Amazon S3 Glacier (S3 Glacier) 要求您提供文件库名称和您的账户 ID。如果您不提供账户 ID,则系统会使用与您提供来对请求签名的证书相关联的账户 ID。有关更多信息,请参阅将 Amazon SDK for Java 与 Amazon S3 Glacier 结合使用。
-
以参数形式提供请求对象,运行
createVault
方法。S3 Glacier 返回的响应在
CreateVaultResult
对象中提供。
以下 Java 代码段说明了前面的步骤。该代码段在 us-west-2
区域创建了文件库。它打印的 Location
是文件库的相对 URI,该 URI 包括您的账户 ID、Amazon Web Services 区域和文件库名称。
AmazonGlacierClient client = new AmazonGlacierClient(credentials); client.setEndpoint("https://glacier.us-west-2.amazonaws.com"); CreateVaultRequest request = new CreateVaultRequest() .withVaultName("*** provide vault name ***"); CreateVaultResult result = client.createVault(request); System.out.println("Created vault successfully: " + result.getLocation());
注意
有关底层 REST API 的信息,请参阅创建文件库(设置文件库)。
示例:使用 Amazon SDK for Java 创建文件库
以下 Java 代码示例在 us-west-2
区域创建了文件库(有关 Amazon Web Services 区域的更多信息,请参阅访问 Amazon S3 Glacier)。此外,该代码示例还检索了文件库信息,列出了同一 Amazon Web Services 区域的所有文件库,然后删除了创建的文件库。
有关如何运行以下示例的分步说明,请参阅“使用 Eclipse 运行 Amazon S3 Glacier 的 Java 示例”。
import java.io.IOException; import java.util.List; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.glacier.AmazonGlacierClient; import com.amazonaws.services.glacier.model.CreateVaultRequest; import com.amazonaws.services.glacier.model.CreateVaultResult; import com.amazonaws.services.glacier.model.DeleteVaultRequest; import com.amazonaws.services.glacier.model.DescribeVaultOutput; import com.amazonaws.services.glacier.model.DescribeVaultRequest; import com.amazonaws.services.glacier.model.DescribeVaultResult; import com.amazonaws.services.glacier.model.ListVaultsRequest; import com.amazonaws.services.glacier.model.ListVaultsResult; public class AmazonGlacierVaultOperations { public static AmazonGlacierClient client; public static void main(String[] args) throws IOException { ProfileCredentialsProvider credentials = new ProfileCredentialsProvider(); client = new AmazonGlacierClient(credentials); client.setEndpoint("https://glacier.us-east-1.amazonaws.com/"); String vaultName = "examplevaultfordelete"; try { createVault(client, vaultName); describeVault(client, vaultName); listVaults(client); deleteVault(client, vaultName); } catch (Exception e) { System.err.println("Vault operation failed." + e.getMessage()); } } private static void createVault(AmazonGlacierClient client, String vaultName) { CreateVaultRequest createVaultRequest = new CreateVaultRequest() .withVaultName(vaultName); CreateVaultResult createVaultResult = client.createVault(createVaultRequest); System.out.println("Created vault successfully: " + createVaultResult.getLocation()); } private static void describeVault(AmazonGlacierClient client, String vaultName) { DescribeVaultRequest describeVaultRequest = new DescribeVaultRequest() .withVaultName(vaultName); DescribeVaultResult describeVaultResult = client.describeVault(describeVaultRequest); System.out.println("Describing the vault: " + vaultName); System.out.print( "CreationDate: " + describeVaultResult.getCreationDate() + "\nLastInventoryDate: " + describeVaultResult.getLastInventoryDate() + "\nNumberOfArchives: " + describeVaultResult.getNumberOfArchives() + "\nSizeInBytes: " + describeVaultResult.getSizeInBytes() + "\nVaultARN: " + describeVaultResult.getVaultARN() + "\nVaultName: " + describeVaultResult.getVaultName()); } private static void listVaults(AmazonGlacierClient client) { ListVaultsRequest listVaultsRequest = new ListVaultsRequest(); ListVaultsResult listVaultsResult = client.listVaults(listVaultsRequest); List<DescribeVaultOutput> vaultList = listVaultsResult.getVaultList(); System.out.println("\nDescribing all vaults (vault list):"); for (DescribeVaultOutput vault : vaultList) { System.out.println( "\nCreationDate: " + vault.getCreationDate() + "\nLastInventoryDate: " + vault.getLastInventoryDate() + "\nNumberOfArchives: " + vault.getNumberOfArchives() + "\nSizeInBytes: " + vault.getSizeInBytes() + "\nVaultARN: " + vault.getVaultARN() + "\nVaultName: " + vault.getVaultName()); } } private static void deleteVault(AmazonGlacierClient client, String vaultName) { DeleteVaultRequest request = new DeleteVaultRequest() .withVaultName(vaultName); client.deleteVault(request); System.out.println("Deleted vault: " + vaultName); } }