public override void CopyDirectory(string srcdomain, string srcdir, string newdomain, string newdir) { string srckey = MakePath(srcdomain, srcdir); string dstkey = MakePath(newdomain, newdir); //List files from src using (AmazonS3 client = GetClient()) { var request = new ListObjectsRequest { BucketName = _bucket }; request.WithPrefix(srckey); using (ListObjectsResponse response = client.ListObjects(request)) { foreach (S3Object s3Object in response.S3Objects) { if (QuotaController != null) { QuotaController.QuotaUsedAdd(_modulename, newdomain, _dataList.GetData(newdomain), s3Object.Size); } client.CopyObject(new CopyObjectRequest() .WithSourceBucket(_bucket) .WithSourceKey(s3Object.Key) .WithDestinationBucket(_bucket) .WithDestinationKey(s3Object.Key.Replace(srckey, dstkey)).WithCannedACL( GetDomainACL(newdomain))); } } } }
public override void CopyDirectory(string srcdomain, string srcdir, string newdomain, string newdir) { string srckey = MakePath(srcdomain, srcdir); string dstkey = MakePath(newdomain, newdir); //List files from src using (AmazonS3 client = GetClient()) { var request = new ListObjectsRequest { BucketName = _bucket, Prefix = srckey }; using (ListObjectsResponse response = client.ListObjects(request)) { foreach (S3Object s3Object in response.S3Objects) { if (QuotaController != null) { QuotaController.QuotaUsedAdd(_modulename, newdomain, _dataList.GetData(newdomain), s3Object.Size); } client.CopyObject(new CopyObjectRequest { SourceBucket = _bucket, SourceKey = s3Object.Key, DestinationBucket = _bucket, DestinationKey = s3Object.Key.Replace(srckey, dstkey), CannedACL = GetDomainACL(newdomain), ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256 }); } } } }
public override Uri Move(string srcdomain, string srcpath, string newdomain, string newpath) { using (AmazonS3 client = GetClient()) { string srcKey = MakePath(srcdomain, srcpath); string dstKey = MakePath(newdomain, newpath); long size = QuotaDelete(srcdomain, client, srcKey); if (QuotaController != null) { QuotaController.QuotaUsedAdd(_modulename, newdomain, _dataList.GetData(newdomain), size); } var request = new CopyObjectRequest { SourceBucket = _bucket, SourceKey = srcKey, DestinationBucket = _bucket, DestinationKey = dstKey, CannedACL = GetDomainACL(newdomain), Directive = S3MetadataDirective.REPLACE, ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256 }; client.CopyObject(request); Delete(srcdomain, srcpath, false); return(GetUri(newdomain, newpath)); } }
public override void MoveDirectory(string srcdomain, string srcdir, string newdomain, string newdir) { string srckey = MakePath(srcdomain, srcdir); string dstkey = MakePath(newdomain, newdir); //List files from src using (AmazonS3 client = GetClient()) { var request = new ListObjectsRequest { BucketName = _bucket }; request.WithPrefix(srckey); using (ListObjectsResponse response = client.ListObjects(request)) { foreach (S3Object s3Object in response.S3Objects) { client.CopyObject(new CopyObjectRequest() .WithSourceBucket(_bucket) .WithSourceKey(s3Object.Key) .WithDestinationBucket(_bucket) .WithDestinationKey(s3Object.Key.Replace(srckey, dstkey)).WithCannedACL( GetDomainACL(newdomain))); client.DeleteObject(new DeleteObjectRequest().WithBucketName(_bucket).WithKey(s3Object.Key)); } } } }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { if (sClass > S3StorageClass.ReducedRedundancy || sClass < S3StorageClass.Standard) { throw new ArgumentException("Invalid value specified for storage class."); } if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // 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); GetObjectMetadataResponse getMetadataResponse = s3Client.GetObjectMetadata(new GetObjectMetadataRequest() .WithBucketName(bucketName) .WithKey(key)); // Set the storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; copyRequest.ServerSideEncryptionMethod = getMetadataResponse.ServerSideEncryptionMethod; if (version != null) { copyRequest.SourceVersionId = version; } copyRequest.StorageClass = sClass; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) { setACLRequest.VersionId = copyResponse.VersionId; } setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the server side encryption method for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // 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); ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest() .WithBucketName(bucketName) .WithPrefix(key) .WithMaxKeys(1)); if (listObjectResponse.S3Objects.Count != 1) { throw new ArgumentNullException("No object exists with this bucket name and key."); } // Set the storage class on the object CopyObjectRequest 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.ServerSideEncryptionMethod = method; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) { setACLRequest.VersionId = copyResponse.VersionId; } setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }
public bool Post([FromUri] string token, [FromUri] long objectId, [FromUri] string newName) // Para renombrar un archivo { var userData = CheckPermissions(token); var fileData = _readOnlyRepository.GetById <File>(objectId); var clientDate = DateTime.Now; if (!fileData.IsDirectory) { //Copy the object var copyRequest = new CopyObjectRequest { SourceBucket = userData.BucketName, SourceKey = fileData.Url + fileData.Name, DestinationBucket = userData.BucketName, DestinationKey = fileData.Url + newName + "." + (fileData.Name.Split('.').LastOrDefault()), CannedACL = S3CannedACL.PublicRead }; AWSClient.CopyObject(copyRequest); //Delete the original var deleteRequest = new DeleteObjectRequest { BucketName = userData.BucketName, Key = fileData.Url + fileData.Name }; AWSClient.DeleteObject(deleteRequest); fileData.ModifiedDate = clientDate; fileData.Name = newName + "." + (fileData.Name.Split('.').LastOrDefault()); _writeOnlyRepository.Update(fileData); return(true); } else { RenameFolder(objectId, fileData.Name, newName, clientDate.ToString()); fileData.ModifiedDate = clientDate; fileData.Name = newName; _writeOnlyRepository.Update(fileData); return(true); } }
public static void CopyFile(AmazonS3 s3Client, string sourcekey, string targetkey) { String destinationPath = targetkey; CopyObjectRequest request = new CopyObjectRequest() { SourceBucket = BUCKET_NAME, SourceKey = sourcekey, DestinationBucket = BUCKET_NAME, DestinationKey = targetkey }; CopyObjectResponse response = s3Client.CopyObject(request); }
/// <summary> /// Copies this file to the location indicated by the passed in S3FileInfo. /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown. /// </summary> /// <param name="file">The target location to copy this file to.</param> /// <param name="overwrite">Determines whether the file can be overwritten.</param> /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception> /// <exception cref="T:System.Net.WebException"></exception> /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception> /// <returns>S3FileInfo of the newly copied file.</returns> public S3FileInfo CopyTo(S3FileInfo file, bool overwrite) { if (!overwrite) { if (file.Exists) { throw new IOException("File already exists"); } } if (SameClient(file)) { var request = new CopyObjectRequest { DestinationBucket = file.BucketName, DestinationKey = S3Helper.EncodeKey(file.ObjectKey), SourceBucket = bucket, SourceKey = S3Helper.EncodeKey(key) }; request.BeforeRequestEvent += S3Helper.FileIORequestEventHandler; s3Client.CopyObject(request); } else { var getObjectRequest = new GetObjectRequest { BucketName = bucket, Key = S3Helper.EncodeKey(key) }; getObjectRequest.BeforeRequestEvent += S3Helper.FileIORequestEventHandler; var getObjectResponse = s3Client.GetObject(getObjectRequest); using (Stream stream = getObjectResponse.ResponseStream) { var putObjectRequest = new PutObjectRequest { BucketName = file.BucketName, Key = S3Helper.EncodeKey(file.ObjectKey), InputStream = stream }; putObjectRequest.BeforeRequestEvent += S3Helper.FileIORequestEventHandler; file.S3Client.PutObject(putObjectRequest); } } return(file); }
/// <summary> /// Sets the redirect location for the S3 Object's when being accessed through the S3 website endpoint. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object</param> /// <param name="websiteRedirectLocation">The redirect location</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetWebsiteRedirectLocation(string bucketName, string key, string websiteRedirectLocation, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, null, s3Client, out copyRequest, out setACLRequest); copyRequest.WebsiteRedirectLocation = websiteRedirectLocation; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) { setACLRequest.VersionId = copyResponse.VersionId; } s3Client.SetACL(setACLRequest); }
public static void SetObjectStorageClass(string bucketName, string key, S3StorageClass sClass, AmazonS3 s3Client) { if ((sClass > S3StorageClass.ReducedRedundancy) || (sClass < S3StorageClass.Standard)) { throw new ArgumentException("Invalid value specified for storage class."); } if (s3Client == null) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } CopyObjectRequest request = new CopyObjectRequest(); request.SourceBucket = request.DestinationBucket = bucketName; request.SourceKey = request.DestinationKey = key; request.StorageClass = sClass; s3Client.CopyObject(request); }
/// <summary> /// Sets the server side encryption method for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.ServerSideEncryptionMethod = method; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) { setACLRequest.VersionId = copyResponse.VersionId; } s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.StorageClass = sClass; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) { setACLRequest.VersionId = copyResponse.VersionId; } s3Client.SetACL(setACLRequest); }
public override Uri Copy(string srcdomain, string srcpath, string newdomain, string newpath) { using (AmazonS3 client = GetClient()) { string srcKey = MakePath(srcdomain, srcpath); string dstKey = MakePath(newdomain, newpath); long size = QuotaDelete(srcdomain, client, srcKey); if (QuotaController != null) { QuotaController.QuotaUsedAdd(_modulename, newdomain, _dataList.GetData(newdomain), size); } CopyObjectRequest request = new CopyObjectRequest() .WithSourceBucket(_bucket) .WithSourceKey(srcKey) .WithDestinationBucket(_bucket) .WithDestinationKey(dstKey).WithCannedACL(GetDomainACL(newdomain)) .WithDirective(S3MetadataDirective.REPLACE); client.CopyObject(request); return(GetUri(newdomain, newpath)); } }
/// <summary> /// Copies this file to the location indicated by the passed in S3FileInfo. /// If the file already exists in S3 and overwrite is set to false than an ArgumentException is thrown. /// </summary> /// <param name="file">The target location to copy this file to.</param> /// <param name="overwrite">Determines whether the file can be overwritten.</param> /// <exception cref="T:System.IO.IOException">If the file already exists in S3 and overwrite is set to false.</exception> /// <exception cref="T:System.Net.WebException"></exception> /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception> /// <returns>S3FileInfo of the newly copied file.</returns> public S3FileInfo CopyTo(S3FileInfo file, bool overwrite) { if (!overwrite) { if (file.Exists) { throw new IOException("File already exists"); } } if (SameClient(file)) { s3Client.CopyObject(new CopyObjectRequest() .WithDestinationBucket(file.BucketName) .WithDestinationKey(S3Helper.EncodeKey(file.ObjectKey)) .WithSourceBucket(bucket) .WithSourceKey(S3Helper.EncodeKey(key)) .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler) as CopyObjectRequest); } else { using (Stream stream = s3Client.GetObject(new GetObjectRequest() .WithBucketName(bucket) .WithKey(S3Helper.EncodeKey(key)) .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler) as GetObjectRequest) .ResponseStream) { file.S3Client.PutObject((PutObjectRequest) new PutObjectRequest() .WithBucketName(file.BucketName) .WithKey(S3Helper.EncodeKey(file.ObjectKey)) .WithInputStream(stream) .WithBeforeRequestHandler(S3Helper.FileIORequestEventHandler)); } } return(file); }
public override void MoveDirectory(string srcdomain, string srcdir, string newdomain, string newdir) { string srckey = MakePath(srcdomain, srcdir); string dstkey = MakePath(newdomain, newdir); //List files from src using (AmazonS3 client = GetClient()) { var request = new ListObjectsRequest { BucketName = _bucket, Prefix = srckey }; using (var response = client.ListObjects(request)) { foreach (S3Object s3Object in response.S3Objects) { client.CopyObject(new CopyObjectRequest { SourceBucket = _bucket, SourceKey = s3Object.Key, DestinationBucket = _bucket, DestinationKey = s3Object.Key.Replace(srckey, dstkey), CannedACL = GetDomainACL(newdomain), ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256 }); client.DeleteObject(new DeleteObjectRequest { BucketName = _bucket, Key = s3Object.Key }); } } } }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { if (sClass > S3StorageClass.ReducedRedundancy || sClass < S3StorageClass.Standard) { throw new ArgumentException("Invalid value specified for storage class."); } if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // 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 storage class on the object CopyObjectRequest copyRequest = new CopyObjectRequest(); copyRequest.SourceBucket = copyRequest.DestinationBucket = bucketName; copyRequest.SourceKey = copyRequest.DestinationKey = key; if (version != null) copyRequest.SourceVersionId = version; copyRequest.StorageClass = sClass; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) setACLRequest.VersionId = copyResponse.VersionId; setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the redirect location for the S3 Object's when being accessed through the S3 website endpoint. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object</param> /// <param name="websiteRedirectLocation">The redirect location</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetWebsiteRedirectLocation(string bucketName, string key, string websiteRedirectLocation, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, null, s3Client, out copyRequest, out setACLRequest); copyRequest.WebsiteRedirectLocation = websiteRedirectLocation; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) setACLRequest.VersionId = copyResponse.VersionId; s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the server side encryption method for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> public static void SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.ServerSideEncryptionMethod = method; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) setACLRequest.VersionId = copyResponse.VersionId; s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the storage class for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object whose storage class needs changing</param> /// <param name="version">The version of the S3 Object whose storage class needs changing</param> /// <param name="sClass">The new Storage Class for the object</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetObjectStorageClass(string bucketName, string key, string version, S3StorageClass sClass, AmazonS3 s3Client) { CopyObjectRequest copyRequest; SetACLRequest setACLRequest; SetupForObjectModification(bucketName, key, version, s3Client, out copyRequest, out setACLRequest); copyRequest.StorageClass = sClass; CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); if (!string.IsNullOrEmpty(copyResponse.VersionId)) setACLRequest.VersionId = copyResponse.VersionId; s3Client.SetACL(setACLRequest); }
/// <summary> /// Sets the server side encryption method for the S3 Object's Version to the value /// specified. /// </summary> /// <param name="bucketName">The name of the bucket in which the key is stored</param> /// <param name="key">The key of the S3 Object</param> /// <param name="version">The version of the S3 Object</param> /// <param name="method">The server side encryption method</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <seealso cref="T:Amazon.S3.Model.S3StorageClass"/> public static void SetServerSideEncryption(string bucketName, string key, string version, ServerSideEncryptionMethod method, AmazonS3 s3Client) { if (null == s3Client) { throw new ArgumentNullException("s3Client", "Please specify an S3 Client to make service requests."); } // 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); ListObjectsResponse listObjectResponse = s3Client.ListObjects(new ListObjectsRequest() .WithBucketName(bucketName) .WithPrefix(key) .WithMaxKeys(1)); if (listObjectResponse.S3Objects.Count != 1) { throw new ArgumentNullException("No object exists with this bucket name and key."); } // Set the storage class on the object CopyObjectRequest 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.ServerSideEncryptionMethod = method; // The copyRequest's Metadata directive is COPY by default CopyObjectResponse copyResponse = s3Client.CopyObject(copyRequest); // Set the object's original ACL back onto it because a COPY // operation resets the ACL on the destination object. SetACLRequest setACLRequest = new SetACLRequest(); setACLRequest.BucketName = bucketName; setACLRequest.Key = key; if (version != null) setACLRequest.VersionId = copyResponse.VersionId; setACLRequest.ACL = getACLResponse.AccessControlList; s3Client.SetACL(setACLRequest); }