Exemplo n.º 1
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var isSingleObjectDelete = this.ParameterSetName == ParamSet_WithKey;

            var context = new CmdletContext
            {
                BucketName = this.BucketName
            };

            string resourceIdentifiersText;

            if (isSingleObjectDelete)
            {
                resourceIdentifiersText = context.Key;

                context.Key       = AmazonS3Helper.CleanKey(this.Key);
                context.VersionId = this.VersionId;
            }
            else
            {
                resourceIdentifiersText = FormatParameterValuesForConfirmationMsg("KeyCollection", MyInvocation.BoundParameters);

                if (this.KeyCollection != null && this.KeyCollection.Length > 0)
                {
                    context.KeyCollection = new List <string>(this.KeyCollection);
                }
                if (this.KeyAndVersionCollection != null && this.KeyAndVersionCollection.Length > 0)
                {
                    context.KeyAndVersionCollection = new List <KeyVersion>(this.KeyAndVersionCollection);
                }
                if (this.InputObject != null && this.InputObject.Length > 0)
                {
                    context.S3ObjectCollection = new List <S3Object>(this.InputObject);
                }

                if (this.ReportErrorsOnly.IsPresent)
                {
                    context.Quiet = true;
                }
            }

            if (!ConfirmShouldProceed(this.Force.IsPresent, resourceIdentifiersText, "Remove-S3Object (DeleteObjects)"))
            {
                return;
            }

            context.SerialNumber        = this.SerialNumber;
            context.AuthenticationValue = this.AuthenticationValue;

            var output = Execute(context) as CmdletOutput;

            ProcessOutput(output);
        }
        protected override void PostExecutionContextLoad(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;

            if (this.Key != null)
            {
                this.Key             = AmazonS3Helper.CleanKey(this.Key);
                cmdletContext.Prefix = this.Key;
            }
            else
            {
                cmdletContext.Prefix = rootIndicators.Contains <string>(this.Prefix, StringComparer.OrdinalIgnoreCase)
                    ? null : AmazonS3Helper.CleanKey(this.Prefix);
            }
        }
        protected override void PostExecutionContextLoad(ExecutorContext context)
        {
            var cmdletContext = context as CmdletContext;

            cmdletContext.Key = AmazonS3Helper.CleanKey(this.Key);

            if (string.IsNullOrEmpty(this.CannedACL))
            {
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
                if (this.PublicReadOnly.IsPresent)
                {
                    cmdletContext.CannedACL = S3CannedACL.PublicRead;
                }
                else if (this.PublicReadWrite.IsPresent)
                {
                    cmdletContext.CannedACL = S3CannedACL.PublicReadWrite;
                }
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute
            }
        }
        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);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            if (!ConfirmShouldProceed(this.Force.IsPresent, this.BucketName, "Write-S3Object (PutObject)"))
            {
                return;
            }

            var context = new CmdletContext
            {
                BucketName = this.BucketName
            };

            if (this.Key != null)
            {
                context.Key = AmazonS3Helper.CleanKey(this.Key);
            }

            if (this.ParameterSetName == ParamSet_FromLocalFile)
            {
                context.File = PSHelpers.PSPathToAbsolute(this.SessionState.Path, this.File.Trim());
                if (string.IsNullOrEmpty(context.Key))
                {
                    context.Key = Path.GetFileName(context.File);
                }
            }
            else if (this.ParameterSetName == ParamSet_FromStream)
            {
                context.Stream = this.Stream;
            }
            else if (this.ParameterSetName == ParamSet_FromContent)
            {
                context.Content = this.Content;
            }
            else
            {
                context.Folder            = PSHelpers.PSPathToAbsolute(this.SessionState.Path, this.Folder.Trim());
                context.Recurse           = this.Recurse.IsPresent;
                context.OriginalKeyPrefix = this.KeyPrefix;
                if (!rootIndicators.Contains <string>(this.KeyPrefix, StringComparer.OrdinalIgnoreCase))
                {
                    context.KeyPrefix = AmazonS3Helper.CleanKey(this.KeyPrefix);
                }
                if (!string.IsNullOrEmpty(this.SearchPattern))
                {
                    context.SearchPattern = this.SearchPattern;
                }
            }

            if (!string.IsNullOrEmpty(this.CannedACLName))
            {
                context.CannedACL = this.CannedACLName;
            }
            else if (this.PublicReadOnly.IsPresent)
            {
                context.CannedACL = S3CannedACL.PublicRead;
            }
            else if (this.PublicReadWrite.IsPresent)
            {
                context.CannedACL = S3CannedACL.PublicReadWrite;
            }

            context.ContentType = this.ContentType;

            if (ParameterWasBound("StorageClass"))
            {
                context.StorageClass = this.StorageClass;
            }
            else
            {
                if (this.StandardStorage.IsPresent)
                {
                    context.StorageClass = S3StorageClass.Standard;
                }
                else if (this.ReducedRedundancyStorage.IsPresent)
                {
                    context.StorageClass = S3StorageClass.ReducedRedundancy;
                }
            }

            if (!string.IsNullOrEmpty(this.ServerSideEncryption))
            {
                context.ServerSideEncryptionMethod = AmazonS3Helper.Convert(this.ServerSideEncryption);
            }

            if (!string.IsNullOrEmpty(this.ServerSideEncryptionCustomerMethod))
            {
                context.ServerSideEncryptionCustomerMethod = this.ServerSideEncryptionCustomerMethod;
            }

            context.ServerSideEncryptionCustomerProvidedKey       = this.ServerSideEncryptionCustomerProvidedKey;
            context.ServerSideEncryptionCustomerProvidedKeyMD5    = this.ServerSideEncryptionCustomerProvidedKeyMD5;
            context.ServerSideEncryptionKeyManagementServiceKeyId = this.ServerSideEncryptionKeyManagementServiceKeyId;

            if (this.ConcurrentServiceRequest.HasValue)
            {
                if (this.ConcurrentServiceRequest.Value <= 0)
                {
                    throw new ArgumentOutOfRangeException("ConcurrentServiceRequests",
                                                          "ConcurrentServiceRequests should be set to a positive integer value.");
                }

                context.ConcurrentServiceRequests = this.ConcurrentServiceRequest.Value;
            }

            context.Metadata = this.Metadata;
            context.Headers  = this.HeaderCollection;
            context.TagSet   = this.TagSet;

            var output = Execute(context) as CmdletOutput;

            ProcessOutput(output);
        }
Exemplo n.º 8
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var context = new CmdletContext
            {
                BucketName = this.BucketName
            };

            switch (this.ParameterSetName)
            {
            case ParamSet_ToLocalFile:
            {
                context.Key     = AmazonS3Helper.CleanKey(this.Key);
                context.File    = PSHelpers.PSPathToAbsolute(this.SessionState.Path, this.File);
                context.Version = this.Version;
            }
            break;

            case ParamSet_ToLocalFolder:
            {
                context.OriginalKeyPrefix = this.KeyPrefix;
                context.KeyPrefix         = rootIndicators.Contains <string>(this.KeyPrefix, StringComparer.OrdinalIgnoreCase)
                            ? "/" : AmazonS3Helper.CleanKey(this.KeyPrefix);
                context.Folder = PSHelpers.PSPathToAbsolute(this.SessionState.Path, this.Folder);
            }
            break;

            case ParamSet_FromS3Object:
            {
                context.BucketName = this.S3Object.BucketName;
                context.Key        = this.S3Object.Key;
                var s3ObjectVersion = this.S3Object as S3ObjectVersion;
                context.Version = s3ObjectVersion == null ? null : s3ObjectVersion.VersionId;

                if (this.ParameterWasBound("File"))
                {
                    context.File = PSHelpers.PSPathToAbsolute(this.SessionState.Path, this.File);
                }
                else
                {
                    var path = PSHelpers.PSPathToAbsolute(this.SessionState.Path, this.Folder);
                    context.File = Path.Combine(path, S3Object.Key);
                }
            }
            break;
            }

            if (ParameterWasBound("UtcModifiedSinceDate"))
            {
                context.UtcModifiedSinceDate = this.UtcModifiedSinceDate;
            }
            if (ParameterWasBound("UtcUnmodifiedSinceDate"))
            {
                context.UtcUnmodifiedSinceDate = this.UtcUnmodifiedSinceDate;
            }
#pragma warning disable CS0618, CS0612 //A class member was marked with the Obsolete attribute
            if (ParameterWasBound("ModifiedSinceDate"))
            {
                context.ModifiedSinceDate = this.ModifiedSinceDate;
            }
            if (ParameterWasBound("UnmodifiedSinceDate"))
            {
                context.UnmodifiedSinceDate = this.UnmodifiedSinceDate;
            }
#pragma warning restore CS0618, CS0612 //A class member was marked with the Obsolete attribute

            if (ParameterWasBound("ServerSideEncryptionCustomerMethod"))
            {
                context.ServerSideEncryptionCustomerMethod = this.ServerSideEncryptionCustomerMethod;
            }

            context.ServerSideEncryptionCustomerProvidedKey    = this.ServerSideEncryptionCustomerProvidedKey;
            context.ServerSideEncryptionCustomerProvidedKeyMD5 = this.ServerSideEncryptionCustomerProvidedKeyMD5;

            var output = Execute(context) as CmdletOutput;
            ProcessOutput(output);
        }