Amazon.S3.Util.AmazonS3Util.SetupForObjectModification C# (CSharp) Method

SetupForObjectModification() static private method

Sets up the request needed to make an exact copy of the object leaving the parent method the ability to change just the attribute being requested to change.
static private SetupForObjectModification ( IAmazonS3 s3Client, string bucketName, string key, string version, CopyObjectRequest &copyRequest, Amazon.S3.Model.PutACLRequest &putACLRequest ) : void
s3Client IAmazonS3
bucketName string
key string
version string
copyRequest Amazon.S3.Model.CopyObjectRequest
putACLRequest Amazon.S3.Model.PutACLRequest
return void
        static void SetupForObjectModification(IAmazonS3 s3Client, string bucketName, string key, string version, 
            out CopyObjectRequest copyRequest, out PutACLRequest putACLRequest)
        {
            // Get the existing ACL of the object
            GetACLRequest getACLRequest = new GetACLRequest();
            getACLRequest.BucketName = bucketName;
            getACLRequest.Key = key;
            if (version != null)
                getACLRequest.VersionId = version;
            GetACLResponse getACLResponse = s3Client.GetACL(getACLRequest);


            // Set the object's original ACL back onto it because a COPY
            // operation resets the ACL on the destination object.
            putACLRequest = new PutACLRequest();
            putACLRequest.BucketName = bucketName;
            putACLRequest.Key = key;
            putACLRequest.AccessControlList = getACLResponse.AccessControlList;


            ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest
            {
                BucketName = bucketName,
                Prefix = key,
                MaxKeys = 1
            });

            if (listObjectResponse.S3Objects.Count != 1)
            {
                throw new InvalidOperationException("No object exists with this bucket name and key.");
            }

            GetObjectMetadataRequest getMetaRequest = new GetObjectMetadataRequest()
            {
                BucketName = bucketName,
                Key = key
            };
            GetObjectMetadataResponse getMetaResponse = s3Client.GetObjectMetadata(getMetaRequest);

            // Set the storage class on the object
            copyRequest = new CopyObjectRequest();
            copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName;
            copyRequest.SourceKey = copyRequest.DestinationKey = key;
            copyRequest.StorageClass = listObjectResponse.S3Objects[0].StorageClass == "STANDARD" ? S3StorageClass.Standard : S3StorageClass.ReducedRedundancy;
            if (version != null)
                copyRequest.SourceVersionId = version;

            copyRequest.WebsiteRedirectLocation = getMetaResponse.WebsiteRedirectLocation;
            copyRequest.ServerSideEncryptionMethod = getMetaResponse.ServerSideEncryptionMethod;
        }