/*******************************************************************************
* 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.
* *****************************************************************************
* __ _ _ ___
* ( )( \/\/ )/ __)
* /__\ \ / \__ \
* (_)(_) \/\/ (___/
*
* AWS SDK for .NET
* API Version: 2006-03-01
*
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using Amazon.S3.Model;
using Amazon.Util;
using System.Globalization;
using Amazon.Runtime.Internal;
namespace Amazon.S3.Transfer
{
///
/// Contains all the parameters
/// that can be set when making a this request with the
/// TransferUtility method.
///
public class TransferUtilityUploadDirectoryRequest : BaseUploadRequest
{
string _directory;
string _bucketname;
string _searchPattern = "*";
string _keyPrefix;
private string contentType;
private bool _uploadFilesConcurrently = false;
SearchOption _searchOption = SearchOption.TopDirectoryOnly;
S3CannedACL _cannedACL;
S3StorageClass _storageClass;
MetadataCollection metadataCollection;
ServerSideEncryptionMethod encryption;
string serverSideEncryptionKeyManagementServiceKeyId;
private ServerSideEncryptionCustomerMethod serverSideCustomerEncryption;
private string serverSideEncryptionCustomerProvidedKey;
private string serverSideEncryptionCustomerProvidedKeyMD5;
private List tagset;
private bool calculateContentMD5Header = false;
private ObjectLockLegalHoldStatus objectLockLegalHoldStatus;
private ObjectLockMode objectLockMode;
private DateTime? objectLockRetainUntilDate;
///
/// Gets or sets the directory where files are uploaded from.
///
///
/// The directory where files are uploaded from.
///
public string Directory
{
get { return this._directory; }
set { this._directory = value; }
}
///
/// Checks if Directory property is set.
///
/// true if Directory property is set.
internal bool IsSetDirectory()
{
return !System.String.IsNullOrEmpty(this._directory);
}
///
/// Gets or sets the KeyPrefix property. As object keys are generated for the
/// files being uploaded this value will prefix the key. This is useful when a directory
/// needs to be uploaded into sub directory in the S3 Bucket.
///
///
/// The directory where files are uploaded from.
///
public string KeyPrefix
{
get { return this._keyPrefix; }
set { this._keyPrefix = value; }
}
///
/// Checks if KeyPrefix property is set.
///
/// true if KeyPrefix property is set.
internal bool IsSetKeyPrefix()
{
return !System.String.IsNullOrEmpty(this._keyPrefix);
}
///
/// Gets and sets the search pattern used to determine which
/// files in the directory are uploaded.
///
///
/// The search pattern used to deterimine which
/// files in the directory are uploaded.
/// The default value is "*", specifying that all files
/// in the directory will be uploaded.
///
public string SearchPattern
{
get { return string.IsNullOrEmpty(this._searchPattern) ? "*" : this._searchPattern; }
set { this._searchPattern = value; }
}
///
/// Checks if SearchPattern property is set.
///
/// true if SearchPattern property is set.
internal bool IsSetSearchPattern()
{
return !System.String.IsNullOrEmpty(this._searchPattern);
}
///
/// Gets or sets the recursive options for the directory upload.
///
///
/// The recursive options for the directory upload.
/// Set by default to TopDirectoryOnly,
/// specifying that files will be uploaded from the root directory only.
///
public SearchOption SearchOption
{
get { return this._searchOption; }
set { this._searchOption = value; }
}
///
/// Gets or sets the name of the bucket.
///
///
/// The name of the bucket.
///
public string BucketName
{
get { return this._bucketname; }
set { this._bucketname = value; }
}
///
/// Checks if BucketName property is set.
///
/// true if BucketName property is set.
internal bool IsSetBucketName()
{
return !System.String.IsNullOrEmpty(this._bucketname);
}
///
/// Gets or sets the canned access control list (ACL)
/// for the uploaded objects.
/// Please refer to
/// for
/// information on Amazon S3 canned ACLs.
///
///
/// The canned access control list (ACL)
/// for the uploaded objects.
///
public S3CannedACL CannedACL
{
get { return this._cannedACL; }
set { this._cannedACL = value; }
}
///
/// Checks if the CannedACL property is set.
///
/// true if there is the CannedACL property is set.
internal bool IsSetCannedACL()
{
return (_cannedACL != null &&_cannedACL != S3CannedACL.NoACL);
}
///
/// Gets or sets the content type for the uploaded Amazon S3 objects.
/// The default behavior when this field is not set is to use the file
/// extension to set the content type. If this field is set to a value it
/// will be applied to all uploaded files in the directory, overriding
/// file extension inspection.
///
///
/// The content type for all the uploaded Amazon S3 objects.
///
public string ContentType
{
get { return this.contentType; }
set { this.contentType = value; }
}
///
/// Gets or sets the storage class for the uploaded Amazon S3 objects.
/// Please refer to
/// for
/// information on S3 Storage Classes.
///
///
/// The storage class for the uploaded Amazon S3 objects.
///
public S3StorageClass StorageClass
{
get { return this._storageClass; }
set
{
this._storageClass = value;
}
}
///
/// The collection of meta data for the request.
///
public MetadataCollection Metadata
{
get
{
if (this.metadataCollection == null)
this.metadataCollection = new MetadataCollection();
return this.metadataCollection;
}
internal set { this.metadataCollection = value; }
}
#region ServerSideEncryption
///
/// Gets or sets the ServerSideEncryptionMethod property.
/// Specifies the encryption used on the server to
/// store the content.
///
public ServerSideEncryptionMethod ServerSideEncryptionMethod
{
get { return this.encryption; }
set { this.encryption = value; }
}
///
/// The id of the AWS Key Management Service key that Amazon S3 should use to encrypt and decrypt the object.
/// If a key id is not specified, the default key will be used for encryption and decryption.
///
[AWSProperty(Sensitive=true)]
public string ServerSideEncryptionKeyManagementServiceKeyId
{
get { return this.serverSideEncryptionKeyManagementServiceKeyId; }
set { this.serverSideEncryptionKeyManagementServiceKeyId = value; }
}
///
/// The Server-side encryption algorithm to be used with the customer provided key.
///
public ServerSideEncryptionCustomerMethod ServerSideEncryptionCustomerMethod
{
get { return this.serverSideCustomerEncryption; }
set { this.serverSideCustomerEncryption = value; }
}
///
/// Checks if ServerSideEncryptionKeyManagementServiceKeyId property is set.
///
/// true if ServerSideEncryptionKeyManagementServiceKeyId property is set.
internal bool IsSetServerSideEncryptionKeyManagementServiceKeyId()
{
return !System.String.IsNullOrEmpty(this.serverSideEncryptionKeyManagementServiceKeyId);
}
///
/// The base64-encoded encryption key for Amazon S3 to use to encrypt the object
///
/// Using the encryption key you provide as part of your request Amazon S3 manages both the encryption, as it writes
/// to disks, and decryption, when you access your objects. Therefore, you don't need to maintain any data encryption code. The only
/// thing you do is manage the encryption keys you provide.
///
///
/// When you retrieve an object, you must provide the same encryption key as part of your request. Amazon S3 first verifies
/// the encryption key you provided matches, and then decrypts the object before returning the object data to you.
///
///
/// Important: Amazon S3 does not store the encryption key you provide.
///
///
[AWSProperty(Sensitive=true)]
public string ServerSideEncryptionCustomerProvidedKey
{
get { return this.serverSideEncryptionCustomerProvidedKey; }
set { this.serverSideEncryptionCustomerProvidedKey = value; }
}
///
/// The MD5 of the customer encryption key specified in the ServerSideEncryptionCustomerProvidedKey property. The MD5 is
/// base 64 encoded. This field is optional, the SDK will calculate the MD5 if this is not set.
///
public string ServerSideEncryptionCustomerProvidedKeyMD5
{
get { return this.serverSideEncryptionCustomerProvidedKeyMD5; }
set { this.serverSideEncryptionCustomerProvidedKeyMD5 = value; }
}
#endregion
///
/// Gets or sets whether the Content-MD5 header should be calculated for upload.
///
public bool CalculateContentMD5Header
{
get { return this.calculateContentMD5Header; }
set { this.calculateContentMD5Header = value; }
}
///
/// Gets and sets the property ObjectLockLegalHoldStatus.
///
/// Specifies whether a legal hold will be applied to this object. For more information
/// about S3 Object Lock, see Object
/// Lock.
///
///
public ObjectLockLegalHoldStatus ObjectLockLegalHoldStatus
{
get { return this.objectLockLegalHoldStatus; }
set { this.objectLockLegalHoldStatus = value; }
}
///
/// Gets and sets the property ObjectLockMode.
///
/// The Object Lock mode that you want to apply to this object.
///
///
public ObjectLockMode ObjectLockMode
{
get { return this.objectLockMode; }
set { this.objectLockMode = value; }
}
///
/// Gets and sets the property ObjectLockRetainUntilDate.
///
/// The date and time when you want this object's Object Lock to expire.
///
///
public DateTime ObjectLockRetainUntilDate
{
get { return this.objectLockRetainUntilDate.GetValueOrDefault(); }
set { this.objectLockRetainUntilDate = value; }
}
// Check to see if ObjectLockRetainUntilDate property is set
internal bool IsSetObjectLockRetainUntilDate()
{
return this.objectLockRetainUntilDate.HasValue;
}
///
/// Gets or sets the UploadFilesConcurrently property.
/// Specifies if multiple files will be uploaded concurrently.
/// The number of concurrent web requests used is controlled
/// by the TransferUtilityConfig.ConcurrencyLevel property.
///
#if BCL45 || NETSTANDARD
public
#else
internal
#endif
bool UploadFilesConcurrently
{
get { return _uploadFilesConcurrently; }
set { _uploadFilesConcurrently = value; }
}
///
/// The event for UploadDirectoryProgressEvent notifications. All
/// subscribers will be notified when a new progress
/// event is raised.
///
/// The UploadDirectoryProgressEvent is fired as data
/// is uploaded to S3. The delegates attached to the event
/// will be passed information detailing how much data
/// has been uploaded as well as how much will be uploaded.
///
///
///
/// Subscribe to this event if you want to receive
/// UploadDirectoryProgressEvent notifications. Here is how:
/// 1. Define a method with a signature similar to this one:
///
/// private void displayProgress(object sender, UploadDirectoryProgressArgs args)
/// {
/// Console.WriteLine(args);
/// }
///
/// 2. Add this method to the UploadDirectoryProgressEvent delegate's invocation list
///
/// TransferUtilityUploadDirectoryRequest request = new TransferUtilityUploadDirectoryRequest();
/// request.UploadDirectoryProgressEvent += displayProgress;
///
///
public event EventHandler UploadDirectoryProgressEvent;
///
/// The event for modifying individual TransferUtilityUploadRequest for each file
/// being uploaded.
///
public event EventHandler UploadDirectoryFileRequestEvent;
///
/// Causes the UploadDirectoryProgressEvent event to be fired.
///
/// Progress data for files currently being uploaded.
internal void OnRaiseProgressEvent(UploadDirectoryProgressArgs uploadDirectoryProgress)
{
AWSSDKUtils.InvokeInBackground(UploadDirectoryProgressEvent, uploadDirectoryProgress, this);
}
internal void RaiseUploadDirectoryFileRequestEvent(TransferUtilityUploadRequest request)
{
var targetEvent = UploadDirectoryFileRequestEvent;
if (targetEvent != null)
{
var args = new UploadDirectoryFileRequestArgs(request);
targetEvent(this, args);
}
}
///
/// Tags that will be applied to all objects in the diretory.
///
public List TagSet
{
get { return this.tagset; }
set { this.tagset = value; }
}
}
///
/// Encapsulates the information needed to provide
/// transfer progress to subscribers of the UploadDirectory
/// event.
///
public class UploadDirectoryProgressArgs : EventArgs
{
///
/// Constructs a new instance of UploadDirectoryProgressArgs.
///
///
/// The number of files uploaded.
///
///
/// The total number of files to upload.
///
///
/// The current file
///
///
/// The number of transferred bytes for current file.
///
///
/// The size of the current file in bytes.
///
public UploadDirectoryProgressArgs(int numberOfFilesUploaded, int totalNumberOfFiles,
string currentFile, long transferredBytesForCurrentFile, long totalNumberOfBytesForCurrentFile)
{
this.NumberOfFilesUploaded = numberOfFilesUploaded;
this.TotalNumberOfFiles = totalNumberOfFiles;
this.CurrentFile = currentFile;
this.TransferredBytesForCurrentFile = transferredBytesForCurrentFile;
this.TotalNumberOfBytesForCurrentFile = totalNumberOfBytesForCurrentFile;
}
///
/// Constructs a new instance of UploadDirectoryProgressArgs.
///
///
/// The number of files uploaded.
///
///
/// The total number of files to upload.
///
///
/// The bytes transferred across all files being uploaded.
///
///
/// The total number of bytes across all files being uploaded.
///
///
/// The current file being uploaded.
///
///
/// The number of transferred bytes for current file.
///
///
/// The size of the current file in bytes.
///
public UploadDirectoryProgressArgs(int numberOfFilesUploaded, int totalNumberOfFiles, long transferredBytes, long totalBytes,
string currentFile, long transferredBytesForCurrentFile, long totalNumberOfBytesForCurrentFile)
{
this.NumberOfFilesUploaded = numberOfFilesUploaded;
this.TotalNumberOfFiles = totalNumberOfFiles;
this.TransferredBytes = transferredBytes;
this.TotalBytes = totalBytes;
this.CurrentFile = currentFile;
this.TransferredBytesForCurrentFile = transferredBytesForCurrentFile;
this.TotalNumberOfBytesForCurrentFile = totalNumberOfBytesForCurrentFile;
}
///
/// Gets or sets the total number of files.
///
/// The total number of files.
public int TotalNumberOfFiles { get; set; }
///
/// Gets or sets the number of files uploaded.
///
/// The number of files uploaded.
public int NumberOfFilesUploaded { get; set; }
///
/// Gets or sets the total number of bytes across all files being uploaded.
///
/// The total number of bytes across all files being uploaded.
public long TotalBytes { get; set; }
///
/// Gets or sets the bytes transferred across all files being uploaded.
///
/// The bytes transferred across all files being uploaded.
public long TransferredBytes { get; set; }
#if BCL45 || NETSTANDARD
///
/// Gets or sets the current file.
///
///
/// This property is only valid if UploadDirectory is used without enabling concurrent file uploads (by default concurrent upload is disabled).
/// If concurrent file uploads are enabled by setting TransferUtilityUploadDirectoryRequest.UploadFilesConcurrently to true, this property
/// will return null.
///
/// The current file.
#else
///
/// Gets or sets the current file.
///
/// The current file.
#endif
public string CurrentFile { get; set; }
#if BCL45 || NETSTANDARD
///
/// Gets or sets the transferred bytes for current file.
///
///
/// This property is only valid if UploadDirectory is used without enabling concurrent file uploads (by default concurrent upload is disabled).
/// If concurrent file uploads are enabled by setting TransferUtilityUploadDirectoryRequest.UploadFilesConcurrently to true, this property
/// will return 0.
///
/// The transferred bytes for current file.
#else
///
/// Gets or sets the transferred bytes for current file.
///
/// The transferred bytes for current file.
#endif
public long TransferredBytesForCurrentFile { get; set; }
#if BCL45 || NETSTANDARD
///
/// Gets or sets the total number of bytes for current file.
///
///
/// This property is only valid if UploadDirectory is used without enabling concurrent file uploads (by default concurrent upload is disabled).
/// If concurrent file uploads are enabled by setting TransferUtilityUploadDirectoryRequest.UploadFilesConcurrently to true, this property
/// will return 0.
///
/// The total number of bytes for current file.
#else
///
/// Gets or sets the total number of bytes for current file.
///
/// The total number of bytes for current file.
#endif
public long TotalNumberOfBytesForCurrentFile { get; set; }
///
/// The string representation of this instance of UploadDirectoryProgressArgs.
///
/// The string representation of this instance of UploadDirectoryProgressArgs.
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "Total Files: {0}, Uploaded Files {1}, Total Bytes: {2}, Transferred Bytes: {3}",
this.TotalNumberOfFiles, this.NumberOfFilesUploaded, this.TotalBytes, this.TransferredBytes);
}
}
///
/// Contains a single TransferUtilityUploadRequest corresponding
/// to a single file about to be uploaded, allowing changes to
/// the request before it is executed.
///
public class UploadDirectoryFileRequestArgs : EventArgs
{
///
/// Constructs a new UploadDirectoryFileRequestArgs instance.
///
/// Request being processed.
public UploadDirectoryFileRequestArgs(TransferUtilityUploadRequest request)
{
UploadRequest = request;
}
///
/// Gets and sets the UploadRequest property.
///
public TransferUtilityUploadRequest UploadRequest { get; set; }
}
}