Amazon S3 Glacier
开发人员指南 (API 版本 2012-06-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

使用AWS SDK for Java在 Amazon S3 Glacier 中创建文件库

该低级 API 为所有文件库操作提供了方法,包括创建和删除文件库、获取文件库描述,以及获取特定区域创建的文件库的列表。以下是使用AWS SDK for Java创建文件库的步骤。

  1. 创建 AmazonGlacierClient 类(客户端)的实例。

    您需要指定要创建文件库的 AWS 区域。您使用此客户端执行的所有操作都会应用到该区域。

  2. 通过创建一个 CreateVaultRequest 类的实例提供请求信息。

    Amazon S3 Glacier (Glacier) 要求您提供文件库名称和您的账户 ID。如果您不提供账户 ID,则系统会使用与您提供来对请求签名的证书相关联的账户 ID。有关更多信息,请参阅 将AWS SDK for Java与 Amazon S3 Glacier 结合使用

  3. 以参数形式提供请求数据元,以执行 createVault 方法。

    Glacier 返回的响应在 CreateVaultResult 对象中提供。

以下 Java 代码段说明了前面的步骤。该代码段在 us-west-2 区域创建了文件库。它打印的 Location 是文件库的相对 URI,该 URI 包括您的账户 ID、区域和文件库名称。

AmazonGlacierClient client = new AmazonGlacierClient(credentials); client.setEndpoint("https://glacier.us-west-2.amazonaws.com.cn"); CreateVaultRequest request = new CreateVaultRequest() .withVaultName("*** provide vault name ***"); CreateVaultResult result = client.createVault(request); System.out.println("Created vault successfully: " + result.getLocation());

注意

有关底层 REST API 的信息,请参阅创建文件库(设置文件库)

示例:使用AWS SDK for Java创建文件库

以下 Java 代码示例在 us-west-2 区域创建了文件库(有关区域的更多信息,请参阅 访问 Amazon S3 Glacier)。此外,该代码示例还取回了文件库信息,列出了同一区域的所有文件库,然后删除了创建的文件库。

有关如何运行以下示例的分步说明,请参阅“使用 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); } }