此頁面僅適用於使用 Vault 和 REST API 2012 年原始版本的 S3 Glacier 服務的現有客戶。
如果您要尋找封存儲存解決方案,建議您在 Amazon S3、S3 Glacier S3 Instant Retrieval、S3 Glacier Flexible Retrieval 和 S3 Glacier Deep Archive 中使用 S3 Glacier 儲存類別。若要進一步了解這些儲存選項,請參閱 Amazon S3
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS SDK for Java 在 Amazon S3 Glacier 中建立文件庫
低階 API 為所有文件庫操作提供了方法,包括建立和刪除文件庫、取得文件庫描述以及取得在特定 AWS 區域 中建立的文件庫清單。以下是使用 AWS SDK for Java 建立文件庫的步驟。
-
建立
AmazonGlacierClient
類別的執行個體 (用戶端)。您需要指定要在其中建立文件庫的 AWS 區域。所有您使用此用戶端執行的操作均會套用到該 AWS 區域。
-
您可以透過建立
CreateVaultRequest
類別的執行個體來提供請求資訊。Amazon S3 Glacier (S3 Glacier) 要求您提供文件庫名稱和帳戶 ID。如果您不提供帳戶 ID,則會使用與您提供來簽署請求之登入資料關聯的帳戶 ID。如需更多詳細資訊,請參閱 將 AWS SDK for Java 與 Amazon S3 Glacier 搭配使用。
-
以參數形式提供請求物件,以便執行
createVault
方法。S3 Glacier 傳回的回應在
CreateVaultResult
物件中是可用的。
下列 Java 程式碼片段描述前述步驟。該程式碼片段會在 us-west-2
區域中建立文件庫。列印的 Location
是文件庫的相對 URI,其中包含帳戶 ID、AWS 區域 和文件庫名稱的。
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 的資訊,請參閱 建立文件庫 (PUT 文件庫)。
範例:使用 AWS SDK for Java 建立文件庫
以下 Java 程式碼範例在 us-west-2
區域中建立文件庫 (如需關於 AWS 區域 的詳細資訊,請參閱存取 Amazon S3 Glacier)。此外,程式碼範例擷取文件庫資訊, 列出同一 AWS 區域 中的所有文件庫,然後刪除所建立的文件庫。
如需如何執行下列範例的逐步說明,請參閱 使用 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); } }