与 AWS SDK或InitiateJob一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或InitiateJob一起使用 CLI

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

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

.NET
AWS SDK for .NET
注意

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

从文件库中检索档案。此示例使用 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详细信息,请参阅 “AWS SDK for .NET API参考 InitiateJob” 中的。

CLI
AWS 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" }

存档文件IDs可在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 参考中的启动任务。

Java
SDK适用于 Java 2.x
注意

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

检索文件库库存。

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详细信息,请参阅 “AWS SDK for Java 2.x API参考 InitiateJob” 中的。

PowerShell
用于 PowerShell

示例 1:启动一项任务,从用户拥有的指定文件库中检索档案。可以使用 Get-GLCJob cmdlet 检查作业的状态。任务成功完成后,可以使用 Read-GCJobOutput 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详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考InitiateJob中的。

Python
SDK适用于 Python (Boto3)
注意

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

检索文件库库存。

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详细信息,请参阅InitiateJob中的 AWS SDKPython (Boto3) API 参考。