InitiateJob与 Amazon SDK 或 CLI 配合使用 - Amazon S3 Glacier
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

此页面仅适用于使用 Vaults 和 2012 年原始 REST API 的 S3 Glacier 服务的现有客户。

如果您正在寻找档案存储解决方案,我们建议您在亚马逊 S3、S3 Glacier 即时检索、S3 Glacier 灵活检索和 S3 Glacier Deep Archive Dee p Archive 中使用 S3 Glacier 存储类。要了解有关这些存储选项的更多信息,请参阅 Amazon S3 用户指南中的 S3 Glacier 存储类和使用 S3 Glacier 存储类的长期数据存储。这些存储类别使用 Amazon S3 API,适用于所有区域,并且可以在 Amazon S3 控制台中进行管理。它们提供存储成本分析、存储镜头、包括多种加密选项在内的安全功能等功能。

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

InitiateJob与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 InitiateJob

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

从文件库中检索档案。此示例使用 ArchiveTransferManager 类。有关 API 的详细信息,请参阅ArchiveTransferManager

/// <summary> /// Download an archive from an Amazon S3 Glacier vault using the Archive /// Transfer Manager. /// </summary> /// <param name="vaultName">The name of the vault containing the object.</param> /// <param name="archiveId">The Id of the archive to download.</param> /// <param name="localFilePath">The local directory where the file will /// be stored after download.</param> /// <returns>Async Task.</returns> public async Task<bool> DownloadArchiveWithArchiveManagerAsync(string vaultName, string archiveId, string localFilePath) { try { var manager = new ArchiveTransferManager(_glacierService); var options = new DownloadOptions { StreamTransferProgress = Progress!, }; // Download an archive. Console.WriteLine("Initiating the archive retrieval job and then polling SQS queue for the archive to be available."); Console.WriteLine("When the archive is available, downloading will begin."); await manager.DownloadAsync(vaultName, archiveId, localFilePath, options); return true; } catch (AmazonGlacierException ex) { Console.WriteLine(ex.Message); return false; } } /// <summary> /// Event handler to track the progress of the Archive Transfer Manager. /// </summary> /// <param name="sender">The object that raised the event.</param> /// <param name="args">The argument values from the object that raised the /// event.</param> static void Progress(object sender, StreamTransferProgressArgs args) { if (args.PercentDone != _currentPercentage) { _currentPercentage = args.PercentDone; Console.WriteLine($"Downloaded {_currentPercentage}%"); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NET API 参考InitiateJob中的。

CLI
Amazon CLI

以下命令启动任务以获取文件my-vault库清单:

aws glacier initiate-job --account-id - --vault-name my-vault --job-parameters '{"Type": "inventory-retrieval"}'

输出:

{ "location": "/0123456789012/vaults/my-vault/jobs/zbxcm3Z_3z5UkoroF7SuZKrxgGoDc3RloGduS7Eg-RO47Yc6FxsdGBgf_Q2DK5Ejh18CnTS5XW4_XqlNHS61dsO4CnMW", "jobId": "zbxcm3Z_3z5UkoroF7SuZKrxgGoDc3RloGduS7Eg-RO47Yc6FxsdGBgf_Q2DK5Ejh18CnTS5XW4_XqlNHS61dsO4CnMW" }

Amazon Glacier 在执行操作时需要一个账户 ID 参数,但您可以使用连字符来指定正在使用的账户。

以下命令启动从文件库my-vault中取回档案的任务:

aws glacier initiate-job --account-id - --vault-name my-vault --job-parameters file://job-archive-retrieval.json

job-archive-retrieval.json是本地文件夹中的一个 JSON 文件,用于指定任务类型、档案 ID 和一些可选参数:

{ "Type": "archive-retrieval", "ArchiveId": "kKB7ymWJVpPSwhGP6ycSOAekp9ZYe_--zM_mw6k76ZFGEIWQX-ybtRDvc2VkPSDtfKmQrj0IRQLSGsNuDp-AJVlu2ccmDSyDUmZwKbwbpAdGATGDiB3hHO0bjbGehXTcApVud_wyDw", "Description": "Retrieve archive on 2015-07-17", "SNSTopic": "arn:aws:sns:us-west-2:0123456789012:my-topic" }

档案 ID 可在aws glacier upload-archive和的输出中找到aws glacier get-job-output

输出:

{ "location": "/011685312445/vaults/mwunderl/jobs/l7IL5-EkXyEY9Ws95fClzIbk2O5uLYaFdAYOi-azsX_Z8V6NH4yERHzars8wTKYQMX6nBDI9cMNHzyZJO59-8N9aHWav", "jobId": "l7IL5-EkXy2O5uLYaFdAYOiEY9Ws95fClzIbk-azsX_Z8V6NH4yERHzars8wTKYQMX6nBDI9cMNHzyZJO59-8N9aHWav" }

有关任务参数格式的详细信息,请参阅 Amazon Glacier API 参考中的启动任务。

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考InitiateJob中的。

Java
适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

检索文件库库存。

import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.glacier.GlacierClient; import software.amazon.awssdk.services.glacier.model.JobParameters; import software.amazon.awssdk.services.glacier.model.InitiateJobResponse; import software.amazon.awssdk.services.glacier.model.GlacierException; import software.amazon.awssdk.services.glacier.model.InitiateJobRequest; import software.amazon.awssdk.services.glacier.model.DescribeJobRequest; import software.amazon.awssdk.services.glacier.model.DescribeJobResponse; import software.amazon.awssdk.services.glacier.model.GetJobOutputRequest; import software.amazon.awssdk.services.glacier.model.GetJobOutputResponse; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * 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 ArchiveDownload { public static void main(String[] args) { final String usage = """ Usage: <vaultName> <accountId> <path> Where: vaultName - The name of the vault. accountId - The account ID value. path - The path where the file is written to. """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String vaultName = args[0]; String accountId = args[1]; String path = args[2]; GlacierClient glacier = GlacierClient.builder() .region(Region.US_EAST_1) .build(); String jobNum = createJob(glacier, vaultName, accountId); checkJob(glacier, jobNum, vaultName, accountId, path); glacier.close(); } public static String createJob(GlacierClient glacier, String vaultName, String accountId) { try { JobParameters job = JobParameters.builder() .type("inventory-retrieval") .build(); InitiateJobRequest initJob = InitiateJobRequest.builder() .jobParameters(job) .accountId(accountId) .vaultName(vaultName) .build(); InitiateJobResponse response = glacier.initiateJob(initJob); System.out.println("The job ID is: " + response.jobId()); System.out.println("The relative URI path of the job is: " + response.location()); return response.jobId(); } catch (GlacierException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } // Poll S3 Glacier = Polling a Job may take 4-6 hours according to the // Documentation. public static void checkJob(GlacierClient glacier, String jobId, String name, String account, String path) { try { boolean finished = false; String jobStatus; int yy = 0; while (!finished) { DescribeJobRequest jobRequest = DescribeJobRequest.builder() .jobId(jobId) .accountId(account) .vaultName(name) .build(); DescribeJobResponse response = glacier.describeJob(jobRequest); jobStatus = response.statusCodeAsString(); if (jobStatus.compareTo("Succeeded") == 0) finished = true; else { System.out.println(yy + " status is: " + jobStatus); Thread.sleep(1000); } yy++; } System.out.println("Job has Succeeded"); GetJobOutputRequest jobOutputRequest = GetJobOutputRequest.builder() .jobId(jobId) .vaultName(name) .accountId(account) .build(); ResponseBytes<GetJobOutputResponse> objectBytes = glacier.getJobOutputAsBytes(jobOutputRequest); // Write the data to a local file. byte[] data = objectBytes.asByteArray(); File myFile = new File(path); OutputStream os = new FileOutputStream(myFile); os.write(data); System.out.println("Successfully obtained bytes from a Glacier vault"); os.close(); } catch (GlacierException | InterruptedException | IOException e) { System.out.println(e.getMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考InitiateJob中的。

PowerShell
用于 PowerShell

示例 1:启动一项任务,从用户拥有的指定文件库中检索档案。可以使用 get-glcJob cmdlet 检查作业的状态。任务成功完成后,可以使用 read-GC JobOutput cmdlet 将档案的内容检索到本地文件系统。

Start-GLCJob -VaultName myvault -JobType "archive-retrieval" -JobDescription "archive retrieval" -ArchiveId "o9O9j...TX-TpIhQJw"

输出:

JobId JobOutputPath Location ----- ------------- -------- op1x...JSbthM /012345678912/vaults/test/jobs/op1xe...I4HqCHkSJSbthM
  • 有关 API 的详细信息,请参阅 Amazon Tools for PowerShell Cmdlet 参考InitiateJob中的。

Python
SDK for Python (Boto3)
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

检索文件库库存。

class GlacierWrapper: """Encapsulates Amazon S3 Glacier API operations.""" def __init__(self, glacier_resource): """ :param glacier_resource: A Boto3 Amazon S3 Glacier resource. """ self.glacier_resource = glacier_resource @staticmethod def initiate_inventory_retrieval(vault): """ Initiates an inventory retrieval job. The inventory describes the contents of the vault. Standard retrievals typically complete within 3—5 hours. When the job completes, you can get the inventory by calling get_output(). :param vault: The vault to inventory. :return: The inventory retrieval job. """ try: job = vault.initiate_inventory_retrieval() logger.info("Started %s job with ID %s.", job.action, job.id) except ClientError: logger.exception("Couldn't start job on vault %s.", vault.name) raise else: return job

从文件库中检索档案。

class GlacierWrapper: """Encapsulates Amazon S3 Glacier API operations.""" def __init__(self, glacier_resource): """ :param glacier_resource: A Boto3 Amazon S3 Glacier resource. """ self.glacier_resource = glacier_resource @staticmethod def initiate_archive_retrieval(archive): """ Initiates an archive retrieval job. Standard retrievals typically complete within 3—5 hours. When the job completes, you can get the archive contents by calling get_output(). :param archive: The archive to retrieve. :return: The archive retrieval job. """ try: job = archive.initiate_archive_retrieval() logger.info("Started %s job with ID %s.", job.action, job.id) except ClientError: logger.exception("Couldn't start job on archive %s.", archive.id) raise else: return job
  • 有关 API 的详细信息,请参阅适用InitiateJobPython 的Amazon SDK (Boto3) API 参考

有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅将 S3 Glacier 与 S Amazon DK 配。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。