private void UploadSingleFile(IAmazonGlacier client,
                                      string vaultName,
                                      string accountId,
                                      string startingFolder,
                                      string filePath,
                                      int thisFileIndex,
                                      int allFilesCount)
        {
            using (var transferManager = new ArchiveTransferManager(client))
            {
                var runner        = new ProgressRunner(this);
                var uploadOptions = new UploadOptions
                {
                    AccountId = accountId
                };

                var tracker = new UploadFolderProgressTracker(runner, handler => uploadOptions.StreamTransferProgress += handler, startingFolder, filePath, thisFileIndex, allFilesCount);

                var output = new CmdletOutput();
                runner.SafeRun(() =>
                {
#if DESKTOP
                    var result = transferManager.Upload(vaultName, "", filePath, uploadOptions);
#else
                    var result = transferManager.UploadAsync(vaultName, "", filePath, uploadOptions).GetAwaiter().GetResult();
#endif

                    output.PipelineOutput = new FileUploadResult(filePath, result);
                }, tracker, output);

                ProcessOutput(output);
            }
        }
        private void UploadSingleFile(IAmazonGlacier client,
                                      string vaultName,
                                      string accountId,
                                      string filePath,
                                      string description)
        {
            using (var transferManager = new ArchiveTransferManager(client))
            {
                var runner        = new ProgressRunner(this);
                var uploadOptions = new UploadOptions
                {
                    AccountId = accountId
                };

                var tracker = new UploadProgressTracker(runner, handler => uploadOptions.StreamTransferProgress += handler, filePath);

                var output = new CmdletOutput();
                runner.SafeRun(() =>
                {
#if DESKTOP
                    var result = transferManager.Upload(vaultName, description, filePath, uploadOptions);
#else
                    var result = transferManager.UploadAsync(vaultName, description, filePath, uploadOptions).GetAwaiter().GetResult();
#endif

                    output.PipelineOutput = new FileUploadResult(filePath, result);
                }, tracker, output);

                // write as we go so that any subsequent error doesn't cause the user to lose
                // the ids of archives we have successfully uploaded so far
                ProcessOutput(output);
            }
        }
Ejemplo n.º 3
0
        CmdletOutput DownloadFolderFromS3(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;
            var request       = new TransferUtilityDownloadDirectoryRequest
            {
                BucketName     = cmdletContext.BucketName,
                LocalDirectory = cmdletContext.Folder,
                S3Directory    = cmdletContext.KeyPrefix
            };

            if (cmdletContext.UtcModifiedSinceDate.HasValue)
            {
                request.ModifiedSinceDateUtc = cmdletContext.UtcModifiedSinceDate.Value;
            }
            if (cmdletContext.UtcUnmodifiedSinceDate.HasValue)
            {
                request.UnmodifiedSinceDateUtc = cmdletContext.UtcUnmodifiedSinceDate.Value;
            }
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
            if (cmdletContext.ModifiedSinceDate.HasValue)
            {
                if (cmdletContext.UtcModifiedSinceDate != null)
                {
                    throw new ArgumentException("Parameters ModifiedSinceDate and UtcModifiedSinceDate are mutually exclusive.");
                }
                request.ModifiedSinceDate = cmdletContext.ModifiedSinceDate.Value;
            }
            if (cmdletContext.UnmodifiedSinceDate.HasValue)
            {
                if (cmdletContext.UtcUnmodifiedSinceDate != null)
                {
                    throw new ArgumentException("Parameters UnmodifiedSinceDate and UtcUnmodifiedSinceDate are mutually exclusive.");
                }
                request.UnmodifiedSinceDate = cmdletContext.UnmodifiedSinceDate.Value;
            }
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute

            CmdletOutput output;
            using (var tu = new TransferUtility(Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint)))
            {
                Utils.Common.WriteVerboseEndpointMessage(this, Client.Config, "Amazon S3 object download APIs");

                var runner  = new ProgressRunner(this);
                var tracker = new DownloadFolderProgressTracker(runner, handler => request.DownloadedDirectoryProgressEvent += handler);

                output = runner.SafeRun(() => tu.DownloadDirectory(request), tracker);
                if (output.ErrorResponse == null)
                {
                    output.PipelineOutput = new DirectoryInfo(cmdletContext.Folder);
                }

                WriteVerbose(string.Format("Downloaded {0} object(s) from bucket '{1}' with keyprefix '{2}' to '{3}'",
                                           tracker.DownloadedCount,
                                           cmdletContext.BucketName,
                                           cmdletContext.OriginalKeyPrefix,
                                           cmdletContext.Folder));
            }

            return(output);
        }
Ejemplo n.º 4
0
        public object Execute(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;

            var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint);

            Utils.Common.WriteVerboseEndpointMessage(this, Client.Config, "GetJobOutput");

            using (var transferManager = new ArchiveTransferManager(client))
            {
                var runner          = new ProgressRunner(this);
                var downloadOptions = new DownloadOptions
                {
                    AccountId = cmdletContext.AccountId
                };

                var tracker = new DownloadProgressTracker(runner, handler => downloadOptions.StreamTransferProgress += handler, cmdletContext.FilePath);

                var output = runner.SafeRun(() =>
                {
#if DESKTOP
                    transferManager.DownloadJob(cmdletContext.VaultName, cmdletContext.JobId, cmdletContext.FilePath, downloadOptions);
#else
                    transferManager.DownloadJobAsync(cmdletContext.VaultName, cmdletContext.JobId, cmdletContext.FilePath, downloadOptions).Wait();
#endif
                }, tracker);

                return(output);
            }
        }
        private CmdletOutput UploadFolderToS3(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;
            var request       = new TransferUtilityUploadDirectoryRequest
            {
                Directory   = cmdletContext.Folder,
                BucketName  = cmdletContext.BucketName,
                KeyPrefix   = cmdletContext.KeyPrefix,
                ContentType = cmdletContext.ContentType
            };

            if (cmdletContext.Recurse)
            {
                request.SearchOption = SearchOption.AllDirectories;
            }
            else
            {
                request.SearchOption = SearchOption.TopDirectoryOnly;
            }
            if (!string.IsNullOrEmpty(cmdletContext.SearchPattern))
            {
                request.SearchPattern = cmdletContext.SearchPattern;
            }

            if (cmdletContext.CannedACL != null)
            {
                request.CannedACL = cmdletContext.CannedACL.Value;
            }
            if (cmdletContext.StorageClass != null)
            {
                request.StorageClass = cmdletContext.StorageClass.Value;
            }
            if (cmdletContext.ServerSideEncryptionMethod != null)
            {
                request.ServerSideEncryptionMethod = cmdletContext.ServerSideEncryptionMethod.Value;
            }
            if (cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId != null)
            {
                request.ServerSideEncryptionKeyManagementServiceKeyId = cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId;
            }
            if (cmdletContext.TagSet != null)
            {
                request.TagSet = new List <Tag>(cmdletContext.TagSet);
            }

            AmazonS3Helper.SetExtraRequestFields(request, cmdletContext);

            CmdletOutput output;

            using (var tu = new TransferUtility(Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint)))
            {
                Utils.Common.WriteVerboseEndpointMessage(this, Client.Config, "Amazon S3 object upload APIs");

                var runner  = new ProgressRunner(this);
                var tracker = new UploadFolderProgressTracker(runner, handler => request.UploadDirectoryProgressEvent += handler, cmdletContext.Folder);
                output = runner.SafeRun(() => tu.UploadDirectory(request), tracker);

                WriteVerbose(string.Format("Uploaded {0} object(s) to bucket '{1}' from '{2}' with keyprefix '{3}'",
                                           tracker.UploadedCount,
                                           cmdletContext.BucketName,
                                           cmdletContext.Folder,
                                           cmdletContext.OriginalKeyPrefix));
            }

            return(output);
        }
        private CmdletOutput UploadFileToS3(ExecutorContext context)
        {
            System.IO.Stream _Stream = null;

            try
            {
                var cmdletContext = context as CmdletContext;
                var request       = new TransferUtilityUploadRequest
                {
                    BucketName = cmdletContext.BucketName,
                    Key        = cmdletContext.Key
                };

                if (!string.IsNullOrEmpty(cmdletContext.File))
                {
                    request.FilePath = cmdletContext.File;
                }
                else if (cmdletContext.Stream != null)
                {
                    _Stream             = Amazon.PowerShell.Common.StreamParameterConverter.TransformToStream(cmdletContext.Stream);
                    request.InputStream = _Stream;
                }

                if (cmdletContext.CannedACL != null)
                {
                    request.CannedACL = cmdletContext.CannedACL.Value;
                }
                if (!string.IsNullOrEmpty(cmdletContext.ContentType))
                {
                    request.ContentType = cmdletContext.ContentType;
                }
                if (cmdletContext.StorageClass != null)
                {
                    request.StorageClass = cmdletContext.StorageClass.Value;
                }
                if (cmdletContext.ServerSideEncryptionMethod != null)
                {
                    request.ServerSideEncryptionMethod = cmdletContext.ServerSideEncryptionMethod.Value;
                }

                if (cmdletContext.ServerSideEncryptionCustomerMethod != null)
                {
                    request.ServerSideEncryptionCustomerMethod = cmdletContext.ServerSideEncryptionCustomerMethod;
                }
                if (cmdletContext.ServerSideEncryptionCustomerProvidedKey != null)
                {
                    request.ServerSideEncryptionCustomerProvidedKey = cmdletContext.ServerSideEncryptionCustomerProvidedKey;
                }
                if (cmdletContext.ServerSideEncryptionCustomerProvidedKeyMD5 != null)
                {
                    request.ServerSideEncryptionCustomerProvidedKeyMD5 = cmdletContext.ServerSideEncryptionCustomerProvidedKeyMD5;
                }
                if (cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId != null)
                {
                    request.ServerSideEncryptionKeyManagementServiceKeyId = cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId;
                }
                if (cmdletContext.TagSet != null)
                {
                    request.TagSet = new List <Tag>(cmdletContext.TagSet);
                }

                var transferUtilityConfig = new TransferUtilityConfig();
                if (cmdletContext.ConcurrentServiceRequests.HasValue)
                {
                    transferUtilityConfig.ConcurrentServiceRequests = cmdletContext.ConcurrentServiceRequests.Value;
                }

                AmazonS3Helper.SetMetadataAndHeaders(request, cmdletContext.Metadata, cmdletContext.Headers);

                CmdletOutput output;
                using (var tu = new TransferUtility(Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint), transferUtilityConfig))
                {
                    Utils.Common.WriteVerboseEndpointMessage(this, Client.Config, "Amazon S3 object upload APIs");

                    var runner = new ProgressRunner(this);

                    string fileName = string.IsNullOrEmpty(cmdletContext.File) ? cmdletContext.Key : cmdletContext.File;
                    var    tracker  = new UploadFileProgressTracker(runner, handler => request.UploadProgressEvent += handler, fileName);

                    output = runner.SafeRun(() => tu.Upload(request), tracker);
                }

                return(output);
            }
            finally
            {
                if (_Stream != null)
                {
                    _Stream.Dispose();
                }
            }
        }
        // Uploads inline content to S3. We cannot write this to a text file first, as
        // SDK will use tempfile extension (.tmp) to set content-type, overriding what
        // the user has specified. Therefore we use a PutObject call instead of using the
        // transfer manager.
        CmdletOutput UploadTextToS3(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;
            var request       = new PutObjectRequest
            {
                BucketName  = cmdletContext.BucketName,
                Key         = cmdletContext.Key,
                ContentBody = cmdletContext.Content
            };

            if (cmdletContext.CannedACL != null)
            {
                request.CannedACL = cmdletContext.CannedACL.Value;
            }
            if (!string.IsNullOrEmpty(cmdletContext.ContentType))
            {
                request.ContentType = cmdletContext.ContentType;
            }
            if (cmdletContext.StorageClass != null)
            {
                request.StorageClass = cmdletContext.StorageClass.Value;
            }
            if (cmdletContext.ServerSideEncryptionMethod != null)
            {
                request.ServerSideEncryptionMethod = cmdletContext.ServerSideEncryptionMethod.Value;
            }

            if (cmdletContext.ServerSideEncryptionCustomerMethod != null)
            {
                request.ServerSideEncryptionCustomerMethod = cmdletContext.ServerSideEncryptionCustomerMethod;
            }
            if (cmdletContext.ServerSideEncryptionCustomerProvidedKey != null)
            {
                request.ServerSideEncryptionCustomerProvidedKey = cmdletContext.ServerSideEncryptionCustomerProvidedKey;
            }
            if (cmdletContext.ServerSideEncryptionCustomerProvidedKeyMD5 != null)
            {
                request.ServerSideEncryptionCustomerProvidedKeyMD5 = cmdletContext.ServerSideEncryptionCustomerProvidedKeyMD5;
            }
            if (cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId != null)
            {
                request.ServerSideEncryptionKeyManagementServiceKeyId = cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId;
            }
            if (cmdletContext.TagSet != null)
            {
                request.TagSet = new List <Tag>(cmdletContext.TagSet);
            }

            AmazonS3Helper.SetMetadataAndHeaders(request, cmdletContext.Metadata, cmdletContext.Headers);

            CmdletOutput output;

            using (var client = CreateClient(_CurrentCredentials, _RegionEndpoint))
            {
                Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon S3", "PutObject");

                var runner  = new ProgressRunner(this);
                var tracker = new UploadTextProgressTracker(runner, handler => request.StreamTransferProgress += handler, cmdletContext.Key);

                output = runner.SafeRun(() => CallAWSServiceOperation(client, request), tracker);
            }

            return(output);
        }
Ejemplo n.º 8
0
        CmdletOutput DownloadFileFromS3(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;
            var request       = new TransferUtilityDownloadRequest
            {
                BucketName = cmdletContext.BucketName,
                FilePath   = cmdletContext.File,
                Key        = cmdletContext.Key
            };

            if (!string.IsNullOrEmpty(cmdletContext.Version))
            {
                request.VersionId = cmdletContext.Version;
            }
            if (cmdletContext.UtcModifiedSinceDate.HasValue)
            {
                request.ModifiedSinceDateUtc = cmdletContext.UtcModifiedSinceDate.Value;
            }
            if (cmdletContext.UtcUnmodifiedSinceDate.HasValue)
            {
                request.UnmodifiedSinceDateUtc = cmdletContext.UtcUnmodifiedSinceDate.Value;
            }
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
            if (cmdletContext.ModifiedSinceDate.HasValue)
            {
                if (cmdletContext.UtcModifiedSinceDate != null)
                {
                    throw new ArgumentException("Parameters ModifiedSinceDate and UtcModifiedSinceDate are mutually exclusive.");
                }
                request.ModifiedSinceDate = cmdletContext.ModifiedSinceDate.Value;
            }
            if (cmdletContext.UnmodifiedSinceDate.HasValue)
            {
                if (cmdletContext.UtcUnmodifiedSinceDate != null)
                {
                    throw new ArgumentException("Parameters UnmodifiedSinceDate and UtcUnmodifiedSinceDate are mutually exclusive.");
                }
                request.UnmodifiedSinceDate = cmdletContext.UnmodifiedSinceDate.Value;
            }
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute

            request.ServerSideEncryptionCustomerMethod         = cmdletContext.ServerSideEncryptionCustomerMethod;
            request.ServerSideEncryptionCustomerProvidedKey    = cmdletContext.ServerSideEncryptionCustomerProvidedKey;
            request.ServerSideEncryptionCustomerProvidedKeyMD5 = cmdletContext.ServerSideEncryptionCustomerProvidedKeyMD5;

            CmdletOutput output;
            using (var tu = new TransferUtility(Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint)))
            {
                Utils.Common.WriteVerboseEndpointMessage(this, Client.Config, "Amazon S3 object download APIs");

                var runner  = new ProgressRunner(this);
                var tracker = new DownloadFileProgressTracker(runner, handler => request.WriteObjectProgressEvent += handler, cmdletContext.Key);

                output = runner.SafeRun(() => tu.Download(request), tracker);
                if (output.ErrorResponse == null)
                {
                    output.PipelineOutput = new FileInfo(cmdletContext.File);
                }
            }
            return(output);
        }