/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Amazon.Glacier.Model;
using Amazon.Glacier.Transfer.Internal;
using Amazon.Runtime;
using Amazon.Util;
namespace Amazon.Glacier.Transfer
{
public partial class ArchiveTransferManager : IDisposable
{
#region Public members
///
/// Creates a vault.
///
/// The name of the vault to create.
public void CreateVault(string vaultName)
{
CreateVaultRequest request = new CreateVaultRequest() { VaultName = vaultName };
((Amazon.Runtime.Internal.IAmazonWebServiceRequest)request).AddBeforeRequestHandler(new UserAgentPostFix("CreateVault").UserAgentRequestEventHandlerSync);
this.glacierClient.CreateVault(request);
}
///
/// Deletes the specified vault. Before deletion, the vault must be empty of all archives.
///
/// The name of the vault to delete.
public void DeleteVault(string vaultName)
{
DeleteVaultRequest request = new DeleteVaultRequest() { VaultName = vaultName };
((Amazon.Runtime.Internal.IAmazonWebServiceRequest)request).AddBeforeRequestHandler(new UserAgentPostFix("DeleteVault").UserAgentRequestEventHandlerSync);
this.glacierClient.DeleteVault(request);
}
///
/// Deletes an archive specified by vault name and archive ID.
///
/// The name of the vault containing the archive.
/// The archive ID of the archive to delete.
public void DeleteArchive(string vaultName, string archiveId)
{
DeleteArchiveRequest request = new DeleteArchiveRequest() { VaultName = vaultName, ArchiveId = archiveId };
((Amazon.Runtime.Internal.IAmazonWebServiceRequest)request).AddBeforeRequestHandler(new UserAgentPostFix("DeleteArchive").UserAgentRequestEventHandlerSync);
this.glacierClient.DeleteArchive(request);
}
#endregion
#region Upload
///
/// Uploads the specified file to Amazon Glacier for archival storage in the
/// specified vault in the specified user's account. For small archives, this
/// method uploads the archive directly to Glacier. For larger archives,
/// this method uses Glacier's multipart upload API to split the upload
/// into multiple parts for better error recovery if any errors are
/// encountered while streaming the data to Amazon Glacier.
///
/// The name of the vault to upload the file to.
/// A description for the archive.
/// The file path to the file to upload.
/// The results of the upload including the archive ID.
public UploadResult Upload(string vaultName, string archiveDescription, string filepath)
{
return Upload(vaultName, archiveDescription, filepath, new UploadOptions());
}
///
///
/// Uploads the specified file to Amazon Glacier for archival storage in the
/// specified vault in the specified user's account. For small archives, this
/// method uploads the archive directly to Glacier. For larger archives,
/// this method uses Glacier's multipart upload API to split the upload
/// into multiple parts for better error recovery if any errors are
/// encountered while streaming the data to Amazon Glacier.
///
///
/// The name of the vault to download the archive from.
/// A description for the archive.
/// The file path to the file to upload.
/// Additional options that can be used for the upload.
/// The results of the upload including the archive ID.
public UploadResult Upload(string vaultName, string archiveDescription, string filepath, UploadOptions options)
{
FileInfo fi = new FileInfo(filepath);
BaseUploadCommand command;
if (fi.Length > MULTIPART_UPLOAD_SIZE_THRESHOLD)
command = new MultipartUploadCommand(this, vaultName, archiveDescription, filepath, options);
else
command = new SinglepartUploadCommand(this, vaultName, archiveDescription, filepath, options);
command.Execute();
return command.UploadResult;
}
#endregion
#region Download
///
///
/// Downloads an archive from Amazon Glacier from the specified vault for the
/// current user's account. Saves the archive to the specified file.
///
///
///
/// This method creates an Amazon SNS topic, and an Amazon SQS queue that is subscribed
/// to that topic. It then initiates the archive retrieval job and polls the queue
/// for the archive to be available. This polling takes about 4 hours. Once the archive
/// is available, download will begin.
///
///
///
/// Additional options can be set using the UploadDirectoryOptions object. For example, you
/// can set the FilesTransferProgress property to a delegate to track progress.
///
///
/// The file path to save the archive at.
/// The name of the vault to download the archive from.
/// The unique ID of the archive to download.
public void Download(string vaultName, string archiveId, string filePath)
{
Download(vaultName, archiveId, filePath, new DownloadOptions());
}
///
///
/// Downloads an archive from Amazon Glacier from the specified vault for the
/// current user's account. Saves the archive to the specified file.
///
///
///
/// This method creates an Amazon SNS topic, and an Amazon SQS queue that is subscribed
/// to that topic. It then initiates the archive retrieval job and polls the queue
/// for the archive to be available. This polling takes about 4 hours. Once the archive
/// is available, download will begin.
///
///
///
/// Additional options can be set using the UploadDirectoryOptions object. For example, you
/// can set the FilesTransferProgress property to a delegate to track progress.
///
///
/// The file path to save the archive at.
/// The name of the vault to download the archive from.
/// The unique ID of the archive to download.
/// Additional options that can be used for the download.
public void Download(string vaultName, string archiveId, string filePath, DownloadOptions options)
{
var command = new DownloadFileCommand(this, vaultName, archiveId, filePath, options);
command.Execute();
}
#endregion
#region DownloadJob
///
///
/// Downloads the results from a completed archive retrieval. Saves the job output
/// to the specified file.
///
///
/// If there is an error during download the download will be retried from the last point read.
/// Once the download is complete the checksum will be compared.
///
///
/// The name of the vault to download the job output from.
/// The unique job ID for an archive retrieval job.
/// The file path to save the job output at.
public void DownloadJob(string vaultName, string jobId, string filePath)
{
DownloadJob(vaultName, jobId, filePath, new DownloadOptions());
}
///
///
/// Downloads the results from a completed archive retrieval. Saves the job output
/// to the specified file.
///
///
/// If there is an error during download the download will be retried from the last point read.
/// Once the download is complete the checksum will be compared.
///
///
/// The name of the vault to download the job output from.
/// The unique job ID for an archive retrieval job.
/// The file path to save the job output at.
/// Additional options that can be used for the download.
public void DownloadJob(string vaultName, string jobId, string filePath, DownloadOptions options)
{
var command = new DownloadJobCommand(this, vaultName, jobId, filePath, options);
command.Execute();
}
#endregion
#region Initiate Archive Retieval
///
/// This method initiates an archive retieval job for the specified archive and returns back the job id.
/// Once the job is complete
///
/// The name of the vault that contains the archive to initiate the job for.
/// The archive id that the download job will retrieve.
/// The job id for the initiated job.
public string InitiateArchiveRetrievalJob(string vaultName, string archiveId)
{
return InitiateArchiveRetrievalJob(vaultName, archiveId, null);
}
///
/// This method initiates an archive retieval job for the specified archive and returns back the job id.
/// Once the job is complete
///
/// The name of the vault that contains the archive to initiate the job for.
/// The archive id that the download job will retrieve.
/// Additional options that can be used for initiating archive retrieval.
/// The job id for the initiated job.
public string InitiateArchiveRetrievalJob(string vaultName, string archiveId, InitiateArchiveRetrievalOptions options)
{
InitiateJobRequest jobRequest = new InitiateJobRequest()
{
VaultName = vaultName,
JobParameters = new JobParameters()
{
ArchiveId = archiveId,
Type = "archive-retrieval"
}
};
if (options != null)
{
jobRequest.AccountId = options.AccountId;
jobRequest.JobParameters.SNSTopic = options.SNSTopic;
jobRequest.JobParameters.Tier = options.Tier;
}
var jobId = glacierClient.InitiateJob(jobRequest).JobId;
return jobId;
}
#endregion
}
}