在 Amazon S3 冰川中使用Amazon SDK for Java - Amazon S3 Glacier
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在 Amazon S3 冰川中使用Amazon SDK for Java

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

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

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

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

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

  3. 运行createVault方法,以参数形式提供请求对象。

    S3 Glacier 返回的响应在CreateVaultResult对象。

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

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区域,请参阅访问 Amazon S3 Glacier er)。此外,该代码示例还取回了文件库信息,列出了同一个Amazon区域,然后删除创建的文件库。

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