Carica un archivio in un vault in Amazon Glacier utilizzando AWS SDK per Java - Amazon Glacier

Questa pagina è riservata ai clienti esistenti del servizio Amazon Glacier che utilizzano Vaults e l'API REST originale del 2012.

Se stai cercando soluzioni di archiviazione, ti consigliamo di utilizzare le classi di storage Amazon Glacier in Amazon S3, S3 Glacier Instant Retrieval, S3 Glacier Flexible Retrieval e S3 Glacier Deep Archive. Per ulteriori informazioni su queste opzioni di storage, consulta le classi di storage di Amazon Glacier.

Amazon Glacier (servizio autonomo originale basato su vault) non accetterà più nuovi clienti a partire dal 15 dicembre 2025, senza alcun impatto sui clienti esistenti. Amazon Glacier è un servizio APIs autonomo che archivia i dati in vault ed è distinto dalle classi di storage Amazon S3 e Amazon S3 Glacier. I dati esistenti rimarranno sicuri e accessibili in Amazon Glacier a tempo indeterminato. Non è richiesta alcuna migrazione. Per uno storage di archiviazione a lungo termine a basso costo, AWS consiglia le classi di storage Amazon S3 Glacier, che offrono un'esperienza cliente superiore con disponibilità Regione AWS completa, costi inferiori e integrazione dei servizi APIs basata su bucket S3. AWS Se desideri funzionalità avanzate, prendi in considerazione la migrazione alle classi di storage Amazon S3 Glacier utilizzando la AWS nostra Solutions Guidance per il trasferimento di dati dai vault Amazon Glacier alle classi di storage Amazon S3 Glacier.

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Carica un archivio in un vault in Amazon Glacier utilizzando AWS SDK per Java

Il seguente esempio di codice Java utilizza l'API di alto livello di AWS SDK per Java per caricare un archivio di esempio nel vault. Nell'esempio di codice, nota quanto segue:

  • L'esempio crea un'istanza della classe AmazonGlacierClient.

  • Nell'esempio viene utilizzata l'operazione API upload della classe ArchiveTransferManager dell'API di alto livello dell' AWS SDK per Java.

  • L'esempio utilizza la regione (us-west-2) Stati Uniti occidentali (Oregon).

Per step-by-step istruzioni su come eseguire questo esempio, vedere. Esecuzione di esempi Java per Amazon Glacier con Eclipse Devi aggiornare il codice con il nome dell'archivio di file che intendi caricare come indicato.

Nota

Amazon Glacier conserva un inventario di tutti gli archivi nei tuoi vault. Quando carichi l'archivio nel seguente esempio, non sarà visualizzato in un vault nella console di gestione fino a quando l'inventario vault non viene aggiornato. Questo aggiornamento viene in genere eseguito una volta al giorno.

SDK per Java 2.x
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.glacier.GlacierClient; import software.amazon.awssdk.services.glacier.model.UploadArchiveRequest; import software.amazon.awssdk.services.glacier.model.UploadArchiveResponse; import software.amazon.awssdk.services.glacier.model.GlacierException; import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class UploadArchive { static final int ONE_MB = 1024 * 1024; public static void main(String[] args) { final String usage = """ Usage: <strPath> <vaultName>\s Where: strPath - The path to the archive to upload (for example, C:\\AWS\\test.pdf). vaultName - The name of the vault. """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String strPath = args[0]; String vaultName = args[1]; File myFile = new File(strPath); Path path = Paths.get(strPath); GlacierClient glacier = GlacierClient.builder() .region(Region.US_EAST_1) .build(); String archiveId = uploadContent(glacier, path, vaultName, myFile); System.out.println("The ID of the archived item is " + archiveId); glacier.close(); } public static String uploadContent(GlacierClient glacier, Path path, String vaultName, File myFile) { // Get an SHA-256 tree hash value. String checkVal = computeSHA256(myFile); try { UploadArchiveRequest uploadRequest = UploadArchiveRequest.builder() .vaultName(vaultName) .checksum(checkVal) .build(); UploadArchiveResponse res = glacier.uploadArchive(uploadRequest, path); return res.archiveId(); } catch (GlacierException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } private static String computeSHA256(File inputFile) { try { byte[] treeHash = computeSHA256TreeHash(inputFile); System.out.printf("SHA-256 tree hash = %s\n", toHex(treeHash)); return toHex(treeHash); } catch (IOException ioe) { System.err.format("Exception when reading from file %s: %s", inputFile, ioe.getMessage()); System.exit(-1); } catch (NoSuchAlgorithmException nsae) { System.err.format("Cannot locate MessageDigest algorithm for SHA-256: %s", nsae.getMessage()); System.exit(-1); } return ""; } public static byte[] computeSHA256TreeHash(File inputFile) throws IOException, NoSuchAlgorithmException { byte[][] chunkSHA256Hashes = getChunkSHA256Hashes(inputFile); return computeSHA256TreeHash(chunkSHA256Hashes); } /** * Computes an SHA256 checksum for each 1 MB chunk of the input file. This * includes the checksum for the last chunk, even if it's smaller than 1 MB. */ public static byte[][] getChunkSHA256Hashes(File file) throws IOException, NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-256"); long numChunks = file.length() / ONE_MB; if (file.length() % ONE_MB > 0) { numChunks++; } if (numChunks == 0) { return new byte[][] { md.digest() }; } byte[][] chunkSHA256Hashes = new byte[(int) numChunks][]; FileInputStream fileStream = null; try { fileStream = new FileInputStream(file); byte[] buff = new byte[ONE_MB]; int bytesRead; int idx = 0; while ((bytesRead = fileStream.read(buff, 0, ONE_MB)) > 0) { md.reset(); md.update(buff, 0, bytesRead); chunkSHA256Hashes[idx++] = md.digest(); } return chunkSHA256Hashes; } finally { if (fileStream != null) { try { fileStream.close(); } catch (IOException ioe) { System.err.printf("Exception while closing %s.\n %s", file.getName(), ioe.getMessage()); } } } } /** * Computes the SHA-256 tree hash for the passed array of 1 MB chunk * checksums. */ public static byte[] computeSHA256TreeHash(byte[][] chunkSHA256Hashes) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[][] prevLvlHashes = chunkSHA256Hashes; while (prevLvlHashes.length > 1) { int len = prevLvlHashes.length / 2; if (prevLvlHashes.length % 2 != 0) { len++; } byte[][] currLvlHashes = new byte[len][]; int j = 0; for (int i = 0; i < prevLvlHashes.length; i = i + 2, j++) { // If there are at least two elements remaining. if (prevLvlHashes.length - i > 1) { // Calculate a digest of the concatenated nodes. md.reset(); md.update(prevLvlHashes[i]); md.update(prevLvlHashes[i + 1]); currLvlHashes[j] = md.digest(); } else { // Take care of the remaining odd chunk currLvlHashes[j] = prevLvlHashes[i]; } } prevLvlHashes = currLvlHashes; } return prevLvlHashes[0]; } /** * Returns the hexadecimal representation of the input byte array */ public static String toHex(byte[] data) { StringBuilder sb = new StringBuilder(data.length * 2); for (byte datum : data) { String hex = Integer.toHexString(datum & 0xFF); if (hex.length() == 1) { // Append leading zero. sb.append("0"); } sb.append(hex); } return sb.toString().toLowerCase(); } }
  • Per i dettagli sull'API, consulta la UploadArchivesezione AWS SDK for Java 2.x API Reference.