/* * 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.Text; using System.Threading; using System.Threading.Tasks; using Amazon.Runtime.Internal.Util; using Amazon.S3.Model; using Amazon.S3.Transfer.Internal; using Amazon.Util; namespace Amazon.S3.Transfer { /// <summary> /// <para> /// Provides a high level utility for managing transfers to and from Amazon S3. /// </para> /// <para> /// <c>TransferUtility</c> provides a simple API for /// uploading content to and downloading content /// from Amazon S3. It makes extensive use of Amazon S3 multipart uploads to /// achieve enhanced throughput, performance, and reliability. /// </para> /// <para> /// When uploading large files by specifying file paths instead of a stream, /// <c>TransferUtility</c> uses multiple threads to upload /// multiple parts of a single upload at once. When dealing with large content /// sizes and high bandwidth, this can increase throughput significantly. /// </para> /// </summary> /// <remarks> /// <para> /// Transfers are stored in memory. If the application is restarted, /// previous transfers are no longer accessible. In this situation, if necessary, /// you should clean up any multipart uploads that are incomplete. /// </para> /// </remarks> public partial class TransferUtility : ITransferUtility { #region UploadDirectory /// <summary> /// Uploads files from a specified directory. /// The object key is derived from the file names /// inside the directory. /// For large uploads, the file will be divided and uploaded in parts using /// Amazon S3's multipart API. The parts will be reassembled as one object in /// Amazon S3. /// </summary> /// <remarks> /// <para> /// If you are uploading large files, TransferUtility will use multipart upload to fulfill the request. /// If a multipart upload is interrupted, TransferUtility will attempt to abort the multipart upload. /// Under certain circumstances (network outage, power failure, etc.), TransferUtility will not be able /// to abort the multipart upload. In this case, in order to stop getting charged for the storage of uploaded parts, /// you should manually invoke TransferUtility.AbortMultipartUploads() to abort the incomplete multipart uploads. /// </para> /// </remarks> /// <param name="directory"> /// The source directory, that is, the directory containing the files to upload. /// </param> /// <param name="bucketName"> /// The target Amazon S3 bucket, that is, the name of the bucket to upload the files to. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task UploadDirectoryAsync(string directory, string bucketName, CancellationToken cancellationToken = default(CancellationToken)) { var request = ConstructUploadDirectoryRequest(directory, bucketName); return UploadDirectoryAsync(request, cancellationToken); } /// <summary> /// Uploads files from a specified directory. /// The object key is derived from the file names /// inside the directory. /// For large uploads, the file will be divided and uploaded in parts using /// Amazon S3's multipart API. The parts will be reassembled as one object in /// Amazon S3. /// </summary> /// <remarks> /// <para> /// If you are uploading large files, TransferUtility will use multipart upload to fulfill the request. /// If a multipart upload is interrupted, TransferUtility will attempt to abort the multipart upload. /// Under certain circumstances (network outage, power failure, etc.), TransferUtility will not be able /// to abort the multipart upload. In this case, in order to stop getting charged for the storage of uploaded parts, /// you should manually invoke TransferUtility.AbortMultipartUploads() to abort the incomplete multipart uploads. /// </para> /// </remarks> /// <param name="directory"> /// The source directory, that is, the directory containing the files to upload. /// </param> /// <param name="bucketName"> /// The target Amazon S3 bucket, that is, the name of the bucket to upload the files to. /// </param> /// <param name="searchPattern"> /// A pattern used to identify the files from the source directory to upload. /// </param> /// <param name="searchOption"> /// A search option that specifies whether to recursively search for files to upload /// in subdirectories. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task UploadDirectoryAsync(string directory, string bucketName, string searchPattern, SearchOption searchOption, CancellationToken cancellationToken = default(CancellationToken)) { var request = ConstructUploadDirectoryRequest(directory, bucketName, searchPattern, searchOption); return UploadDirectoryAsync(request, cancellationToken); } /// <summary> /// Uploads files from a specified directory. /// The object key is derived from the file names /// inside the directory. /// For large uploads, the file will be divided and uploaded in parts using /// Amazon S3's multipart API. The parts will be reassembled as one object in /// Amazon S3. /// </summary> /// <remarks> /// <para> /// If you are uploading large files, TransferUtility will use multipart upload to fulfill the request. /// If a multipart upload is interrupted, TransferUtility will attempt to abort the multipart upload. /// Under certain circumstances (network outage, power failure, etc.), TransferUtility will not be able /// to abort the multipart upload. In this case, in order to stop getting charged for the storage of uploaded parts, /// you should manually invoke TransferUtility.AbortMultipartUploads() to abort the incomplete multipart uploads. /// </para> /// </remarks> /// <param name="request"> /// The request that contains all the parameters required to upload a directory. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task UploadDirectoryAsync(TransferUtilityUploadDirectoryRequest request, CancellationToken cancellationToken = default(CancellationToken)) { CheckForBlockedArn(request.BucketName, "UploadDirectory"); validate(request); UploadDirectoryCommand command = new UploadDirectoryCommand(this, this._config, request); command.UploadFilesConcurrently = request.UploadFilesConcurrently; return command.ExecuteAsync(cancellationToken); } #endregion #region DownloadDirectory /// <summary> /// Downloads the objects in Amazon S3 that have a key that starts with the value /// specified by <c>s3Directory</c>. /// </summary> /// <param name="bucketName"> /// The name of the bucket containing the Amazon S3 objects to download. /// </param> /// <param name="s3Directory"> /// The directory in Amazon S3 to download. /// </param> /// <param name="localDirectory"> /// The local directory to download the objects to. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task DownloadDirectoryAsync(string bucketName, string s3Directory, string localDirectory, CancellationToken cancellationToken = default(CancellationToken)) { var request = ConstructDownloadDirectoryRequest(bucketName, s3Directory, localDirectory); return DownloadDirectoryAsync(request, cancellationToken); } /// <summary> /// Downloads the objects in Amazon S3 that have a key that starts with the value /// specified by the <c>S3Directory</c> /// property of the passed in <c>TransferUtilityDownloadDirectoryRequest</c> object. /// </summary> /// <param name="request"> /// Contains all the parameters required to download objects from Amazon S3 /// into a local directory. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task DownloadDirectoryAsync(TransferUtilityDownloadDirectoryRequest request, CancellationToken cancellationToken = default(CancellationToken)) { CheckForBlockedArn(request.BucketName, "DownloadDirectory"); var command = new DownloadDirectoryCommand(this._s3Client, request, this._config); command.DownloadFilesConcurrently = request.DownloadFilesConcurrently; return command.ExecuteAsync(cancellationToken); } #endregion #region Download /// <summary> /// Downloads the content from Amazon S3 and writes it to the specified file. /// </summary> /// <param name="filePath"> /// The file path where the content from Amazon S3 will be written to. /// </param> /// <param name="bucketName"> /// The name of the bucket containing the Amazon S3 object to download. /// </param> /// <param name="key"> /// The key under which the Amazon S3 object is stored. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task DownloadAsync(string filePath, string bucketName, string key, CancellationToken cancellationToken = default(CancellationToken)) { var request = ConstructDownloadRequest(filePath, bucketName, key); return DownloadAsync(request, cancellationToken); } #endregion } }