Amazon.S3.Transfer.Internal.DownloadDirectoryCommand.Execute C# (CSharp) Method

Execute() public method

public Execute ( ) : void
return void
        public override void Execute()
        {
            if (!this._request.IsSetBucketName())
            {
                throw new InvalidOperationException("The bucketName Specified is null or empty!");
            }
            if (!this._request.IsSetS3Directory())
            {
                throw new InvalidOperationException("The S3Directory Specified is null or empty!");
            }
            if (!this._request.IsSetLocalDirectory())
            {
                throw new InvalidOperationException("The LocalDirectory Specified is null or empty!");
            }

            if (File.Exists(this._request.S3Directory))
            {
                throw new InvalidOperationException("A file already exists with the same name indicated by LocalDirectory!");
            }

            EnsureDirectoryExists(new DirectoryInfo(this._request.LocalDirectory));

            ListObjectsRequest listRequest = new ListObjectsRequest();
            listRequest.BucketName = this._request.BucketName;
            listRequest.Prefix = this._request.S3Directory;

            listRequest.Prefix = listRequest.Prefix.Replace('\\', '/');
            if (!listRequest.Prefix.EndsWith("/", StringComparison.Ordinal))
                listRequest.Prefix += "/";

            if (listRequest.Prefix.StartsWith("/", StringComparison.Ordinal))
            {
                if (listRequest.Prefix.Length == 1)
                    listRequest.Prefix = "";
                else
                    listRequest.Prefix = listRequest.Prefix.Substring(1);
            }

            List<S3Object> objs = new List<S3Object>();
            do
            {
                ListObjectsResponse listResponse = this._s3Client.ListObjects(listRequest);
                foreach (S3Object s3o in listResponse.S3Objects)
                {
					if (this._request.IsSetModifiedSinceDate() && this._request.ModifiedSinceDate < s3o.LastModified)
						continue;
					if (this._request.IsSetUnmodifiedSinceDate() && s3o.LastModified < this._request.UnmodifiedSinceDate)
						continue;

					objs.Add(s3o);
                }
                listRequest.Marker = listResponse.NextMarker;
            } while (!string.IsNullOrEmpty(listRequest.Marker));
			
            this._totalNumberOfFilesToDownload = objs.Count;

            foreach (S3Object s3o in objs)
            {
                if (s3o.Key.EndsWith("/", StringComparison.Ordinal))
                    continue;

                this._currentFile = s3o.Key.Substring(listRequest.Prefix.Length);

                TransferUtilityDownloadRequest downloadRequest = new TransferUtilityDownloadRequest();
                downloadRequest.BucketName = this._request.BucketName;
                downloadRequest.Key = s3o.Key;
                downloadRequest.FilePath = Path.Combine(this._request.LocalDirectory, this._currentFile);
                downloadRequest.WriteObjectProgressEvent += downloadedProgressEventCallback;


                DownloadCommand command = new DownloadCommand(this._s3Client, downloadRequest);
                command.Execute();

                this._numberOfFilesDownloaded++;
            }
        }

Usage Example

Example #1
0
 /// <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>
 public void DownloadDirectory(TransferUtilityDownloadDirectoryRequest request)
 {
     BaseCommand command = new DownloadDirectoryCommand(this._s3Client, request);
     command.Execute();
 }