コード例 #1
0
        public void UpdateBlobProperties(string containerName, string blobName, BlobProperties properties)
        {
            var updateRequest = new CopyObjectRequest()
            {
                SourceBucket      = _bucket,
                SourceKey         = GenerateKeyName(containerName, blobName),
                DestinationBucket = _bucket,
                DestinationKey    = GenerateKeyName(containerName, blobName),
                ContentType       = properties?.ContentType,
                CannedACL         = GetCannedACL(properties),
                MetadataDirective = S3MetadataDirective.REPLACE
            };

            try
            {
                AsyncHelpers.RunSync(() => _s3Client.CopyObjectAsync(updateRequest));
            }
            catch (AmazonS3Exception asex)
            {
                if (IsInvalidAccessException(asex))
                {
                    throw new StorageException(1000.ToStorageError(), asex);
                }
                else
                {
                    throw new StorageException(1001.ToStorageError(), asex);
                }
            }
        }
コード例 #2
0
        public async Task <bool> ResetDataFilesAsync()
        {
            var request = new ListObjectsRequest();

            request.BucketName = DataFileLocation;
            do
            {
                var response = await _amazonS3Client.ListObjectsAsync(request, CancellationToken.None);

                foreach (var file in response.S3Objects.Where(s => s.Key.StartsWith("DataFiles/") && s.Key != "DataFiles/"))
                {
                    var deleteObjectRequest = new DeleteObjectRequest
                    {
                        BucketName = request.BucketName,
                        Key        = file.Key
                    };
                    await _amazonS3Client.DeleteObjectAsync(deleteObjectRequest);
                }
                if (response.IsTruncated)
                {
                    request.Marker = response.NextMarker;
                }
                else
                {
                    request = null;
                }
            } while (request != null);

            var copyRequest = new CopyObjectRequest
            {
                SourceBucket      = DataFileLocation,
                SourceKey         = "MasterFiles/ClassA.csv",
                DestinationBucket = DataFileLocation,
                DestinationKey    = "DataFiles/ClassA.csv"
            };
            var result = await _amazonS3Client.CopyObjectAsync(copyRequest);

            copyRequest = new CopyObjectRequest
            {
                SourceBucket      = DataFileLocation,
                SourceKey         = "MasterFiles/ClassB.csv",
                DestinationBucket = DataFileLocation,
                DestinationKey    = "DataFiles/ClassB.csv"
            };
            result = await _amazonS3Client.CopyObjectAsync(copyRequest);

            copyRequest = new CopyObjectRequest
            {
                SourceBucket      = DataFileLocation,
                SourceKey         = "MasterFiles/ClassC.csv",
                DestinationBucket = DataFileLocation,
                DestinationKey    = "DataFiles/ClassC.csv"
            };
            result = await _amazonS3Client.CopyObjectAsync(copyRequest);

            return(true);
        }
        public async Task ArchiveAsync(IFileEntry fileEntry, CancellationToken cancellationToken = default)
        {
            var copy = new CopyObjectRequest
            {
                SourceBucket      = _options.BucketName,
                SourceKey         = GetKey(fileEntry),
                DestinationBucket = _options.BucketName,
                DestinationKey    = GetKey(fileEntry),
                StorageClass      = S3StorageClass.StandardInfrequentAccess,
            };

            await _client.CopyObjectAsync(copy, cancellationToken);
        }
コード例 #4
0
        // -------------------- S3 Specific Methods ------------------------------
        /// <summary>
        /// Moves an image from the transfer bucket to the storage bucket with the appropriate key.
        /// </summary>
        /// <param name="tempTransferKey">The S3 object key of the image in the transfer bucket (provided by the client).</param>
        /// <param name="cloudStorageKey">The permanent object key</param>
        /// <returns>HttpStatusCode</returns>
        public async Task <HttpStatusCode> MoveImageFromTransferBucketToStorageBucket(string tempTransferKey, string cloudStorageKey)
        {
            try
            {
                var copyOjectResponse = await _s3Client.CopyObjectAsync(_config["AWSS3:TransferBucketName"], tempTransferKey, _config["AWSS3:StorageBucketName"], cloudStorageKey);

                if (copyOjectResponse.HttpStatusCode == HttpStatusCode.OK)
                {
                    var deleteObjectResponse = await _s3Client.DeleteObjectAsync(_config["AWSS3:TransferBucketName"], tempTransferKey);

                    if (deleteObjectResponse != null)
                    {
                        return(HttpStatusCode.OK);
                    }
                    else
                    {
                        throw new Exception($"Transfer image '{tempTransferKey}' was not deleted from the transfer bucket. Response code '{deleteObjectResponse.HttpStatusCode}'");
                    }
                }
                else
                {
                    throw new Exception($"Image '{cloudStorageKey}' was not copied to the storage bucket. Response code '{copyOjectResponse.HttpStatusCode}'");
                }
            }
            catch (AmazonS3Exception e)
            {
                throw new AmazonS3Exception($"S3 Specific Message: {e.Message}");
            }
            catch (Exception e)
            {
                throw new Exception($"Generic ASP.NET Message: {e.Message}");
            }
        }
コード例 #5
0
        public static bool CopyFileFormFolder(string sourceKey, string destinationKey)
        {
            try
            {
                using (IAmazonS3 client = AwsAuthentication())
                {
                    CopyObjectRequest request = new CopyObjectRequest();
                    request.SourceBucket      = AwsBuketName;
                    request.SourceKey         = AwsFolderName + "/" + sourceKey;
                    request.DestinationBucket = AwsBuketName;
                    request.DestinationKey    = AwsFolderName + "/" + destinationKey;
                    request.CannedACL         = S3CannedACL.PublicReadWrite;
                    request.StorageClass      = S3StorageClass.Standard;

                    Task <CopyObjectResponse> response = client.CopyObjectAsync(request);

                    if (response.Result.HttpStatusCode == System.Net.HttpStatusCode.OK)
                    {
                        return(true);
                    }
                }
            }
            catch (Amazon.S3.AmazonS3Exception ex)
            {
                if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
            return(false);
        }
コード例 #6
0
        /// <summary>
        /// This method calls the AWS SDK for .NET to copy an
        /// object from one Amazon S3 bucket to another.
        /// </summary>
        /// <param name="client">The Amazon S3 client object.</param>
        /// <param name="sourceKey">The name of the object to be copied.</param>
        /// <param name="destinationKey">The name under which to save the copy.</param>
        /// <param name="sourceBucketName">The name of the Amazon S3 bucket
        /// where the file is located now.</param>
        /// <param name="destinationBucketName">The name of the Amazon S3
        /// bucket where the copy should be saved.</param>
        /// <returns>Returns a CopyObjectResponse object with the results from
        /// the async call.</returns>
        public static async Task <CopyObjectResponse> CopyingObjectAsync(
            IAmazonS3 client,
            string sourceKey,
            string destinationKey,
            string sourceBucketName,
            string destinationBucketName)
        {
            var response = new CopyObjectResponse();

            try
            {
                var request = new CopyObjectRequest
                {
                    SourceBucket      = sourceBucketName,
                    SourceKey         = sourceKey,
                    DestinationBucket = destinationBucketName,
                    DestinationKey    = destinationKey,
                };
                response = await client.CopyObjectAsync(request);
            }
            catch (AmazonS3Exception ex)
            {
                Console.WriteLine($"Error copying object: '{ex.Message}'");
            }

            return(response);
        }
コード例 #7
0
        private async Task CopyBlobAsync(string sourceBucket,
                                         string sourceBlobName,
                                         string targetBucket,
                                         string?targetBlobName = null,
                                         CancellationToken cancellationToken = default)
        {
            var newTargetKey = targetBlobName ?? sourceBlobName;

            try
            {
                var request = new CopyObjectRequest
                {
                    SourceBucket      = sourceBucket,
                    SourceKey         = sourceBlobName,
                    DestinationBucket = targetBucket,
                    DestinationKey    = newTargetKey,
                };

                var response = await _client.CopyObjectAsync(request, cancellationToken);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new StorageException("Copy object failed.");
                }
            }
            catch (AmazonS3Exception ex)
            {
                throw new StorageException(ex.Message, ex);
            }
        }
コード例 #8
0
        public async Task CopyAsync(string sourceFileName, string targetFileName, CancellationToken ct = default)
        {
            var sourceKey = GetKey(sourceFileName, nameof(sourceFileName));
            var targetKey = GetKey(targetFileName, nameof(targetFileName));

            try
            {
                await EnsureNotExistsAsync(targetKey, targetFileName, ct);

                var request = new CopyObjectRequest
                {
                    SourceBucket      = options.Bucket,
                    SourceKey         = sourceKey,
                    DestinationBucket = options.Bucket,
                    DestinationKey    = targetKey
                };

                await s3Client.CopyObjectAsync(request, ct);
            }
            catch (AmazonS3Exception ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                throw new AssetNotFoundException(sourceFileName, ex);
            }
            catch (AmazonS3Exception ex) when(ex.StatusCode == HttpStatusCode.PreconditionFailed)
            {
                throw new AssetAlreadyExistsException(targetFileName);
            }
        }
コード例 #9
0
        /// <summary>
        /// Copies an encrypted object from one Amazon S3 bucket to another.
        /// </summary>
        /// <param name="client">The initialized Amazon S3 client object used to call
        /// CopyObjectAsync.</param>
        /// <param name="bucketName">The Amazon S3 bucket containing the object
        /// to copy.</param>
        /// <param name="keyName">The name of the object to copy.</param>
        /// <param name="copyTargetKeyName">The Amazon S3 bucket to which the object
        /// will be copied.</param>
        /// <param name="aesEncryption">The encryption type to use.</param>
        /// <param name="base64Key">The encryption key to use.</param>
        public static async Task CopyObjectAsync(
            IAmazonS3 client,
            string bucketName,
            string keyName,
            string copyTargetKeyName,
            Aes aesEncryption,
            string base64Key)
        {
            aesEncryption.GenerateKey();
            string copyBase64Key = Convert.ToBase64String(aesEncryption.Key);

            CopyObjectRequest copyRequest = new CopyObjectRequest
            {
                SourceBucket      = bucketName,
                SourceKey         = keyName,
                DestinationBucket = bucketName,
                DestinationKey    = copyTargetKeyName,

                // Information about the source object's encryption.
                CopySourceServerSideEncryptionCustomerMethod      = ServerSideEncryptionCustomerMethod.AES256,
                CopySourceServerSideEncryptionCustomerProvidedKey = base64Key,

                // Information about the target object's encryption.
                ServerSideEncryptionCustomerMethod      = ServerSideEncryptionCustomerMethod.AES256,
                ServerSideEncryptionCustomerProvidedKey = copyBase64Key,
            };
            await client.CopyObjectAsync(copyRequest);
        }
コード例 #10
0
        // snippet-end:[S3.dotnetv3.S3_Basics-DownloadObject]

        // snippet-start:[S3.dotnetv3.S3_Basics-CopyObject]

        /// <summary>
        /// Copies an object in an Amazon S3 bucket to a folder within the
        /// same bucket.
        /// </summary>
        /// <param name="client">An initialized Amazon S3 client object.</param>
        /// <param name="bucketName">The name of the Amazon S3 bucket where the
        /// object to copy is located.</param>
        /// <param name="objectName">The object to be copied.</param>
        /// <param name="folderName">The folder to which the object will
        /// be copied.</param>
        /// <returns>A boolean value that indicates the success or failure of
        /// the copy operation.</returns>
        public static async Task <bool> CopyObjectInBucketAsync(
            IAmazonS3 client,
            string bucketName,
            string objectName,
            string folderName)
        {
            try
            {
                var request = new CopyObjectRequest
                {
                    SourceBucket      = bucketName,
                    SourceKey         = objectName,
                    DestinationBucket = bucketName,
                    DestinationKey    = $"{folderName}\\{objectName}",
                };
                var response = await client.CopyObjectAsync(request);

                return(response.HttpStatusCode == System.Net.HttpStatusCode.OK);
            }
            catch (AmazonS3Exception ex)
            {
                Console.WriteLine($"Error copying object: '{ex.Message}'");
                return(false);
            }
        }
コード例 #11
0
        public async Task CopyBlobAsync(string sourceContainerName, string sourceBlobName,
                                        string destinationContainerName, string destinationBlobName = null)
        {
            if (string.IsNullOrEmpty(sourceContainerName))
            {
                throw new StorageException(StorageErrorCode.InvalidName, $"Invalid {nameof(sourceContainerName)}");
            }
            if (string.IsNullOrEmpty(sourceBlobName))
            {
                throw new StorageException(StorageErrorCode.InvalidName, $"Invalid {nameof(sourceBlobName)}");
            }
            if (string.IsNullOrEmpty(destinationContainerName))
            {
                throw new StorageException(StorageErrorCode.InvalidName, $"Invalid {nameof(destinationContainerName)}");
            }
            if (destinationBlobName == string.Empty)
            {
                throw new StorageException(StorageErrorCode.InvalidName, $"Invalid {nameof(destinationBlobName)}");
            }

            var sourceKey      = GenerateKeyName(sourceContainerName, sourceBlobName);
            var destinationKey = GenerateKeyName(destinationContainerName, destinationBlobName ?? sourceBlobName);

            try
            {
                var request = new CopyObjectRequest
                {
                    SourceBucket               = _bucket,
                    SourceKey                  = sourceKey,
                    DestinationBucket          = _bucket,
                    DestinationKey             = destinationKey,
                    ServerSideEncryptionMethod = _serverSideEncryptionMethod
                };

                var response = await _s3Client.CopyObjectAsync(request);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new StorageException(StorageErrorCode.GenericException, "Copy failed.");
                }
            }
            catch (AmazonS3Exception asex)
            {
                throw asex.ToStorageException();
            }
        }
コード例 #12
0
 public static void CopyS3(IAmazonS3 s3Client, string sourceKey, string destKey)
 {
     s3Client.CopyObjectAsync(new Amazon.S3.Model.CopyObjectRequest()
     {
         SourceBucket      = CachedData.S3ContentBucket,
         CannedACL         = S3CannedACL.PublicRead,
         SourceKey         = sourceKey,
         DestinationBucket = CachedData.S3ContentBucket,
         DestinationKey    = destKey
     });;
 }
コード例 #13
0
        public async void CopiarArquivo(string caminhoArquivo, string nomeBucket)
        {
            CopyObjectRequest request = new CopyObjectRequest
            {
                SourceBucket      = nomeBucket,
                SourceKey         = caminhoArquivo,
                DestinationKey    = caminhoArquivo,
                DestinationBucket = "conteudo-oregon",
            };
            var response = await _amazoS3Client.CopyObjectAsync(request);

            Console.WriteLine(response.ETag);
        }
コード例 #14
0
        public async Task CopyFileAsync(string srcPath, string dstPath)
        {
            if (srcPath == dstPath)
            {
                throw new ArgumentException($"The values for {nameof(srcPath)} and {nameof(dstPath)} must not be the same.");
            }

            try
            {
                await _amazonS3Client.GetObjectMetadataAsync(new GetObjectMetadataRequest
                {
                    BucketName = _options.BucketName,
                    Key        = this.Combine(_basePrefix, srcPath)
                });
            }
            catch (AmazonS3Exception)
            {
                throw new FileStoreException($"Cannot copy file '{srcPath}' because it does not exist.");
            }

            try
            {
                var listObjects = await _amazonS3Client.ListObjectsV2Async(new ListObjectsV2Request
                {
                    BucketName = _options.BucketName,
                    Prefix     = this.Combine(_basePrefix, dstPath)
                });

                if (listObjects.S3Objects.Any())
                {
                    throw new FileStoreException($"Cannot copy file '{srcPath}' because a file already exists in the new path '{dstPath}'.");
                }

                var copyObjectResponse = await _amazonS3Client.CopyObjectAsync(new CopyObjectRequest
                {
                    SourceBucket      = _options.BucketName,
                    SourceKey         = this.Combine(_basePrefix, srcPath),
                    DestinationBucket = _options.BucketName,
                    DestinationKey    = this.Combine(_basePrefix, dstPath)
                });

                if (copyObjectResponse.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new FileStoreException($"Error while copying file '{srcPath}'");
                }
            }
            catch (AmazonS3Exception)
            {
                throw new FileStoreException($"Error while copying file '{srcPath}'");
            }
        }
コード例 #15
0
        public async Task CopyObjectInBucketAsyncTest()
        {
            IAmazonS3 client = CreateMockS3Client();

            var request = new CopyObjectRequest
            {
                SourceBucket      = newBucket,
                SourceKey         = keyName,
                DestinationBucket = newBucket,
                DestinationKey    = $"{destinationFolderName}\\{keyName}",
            };

            var response = await client.CopyObjectAsync(request);

            Assert.True(response.HttpStatusCode == System.Net.HttpStatusCode.OK, $"Could not copy {keyName}.");
        }
コード例 #16
0
ファイル: AmazonS3.cs プロジェクト: rainsym/ConsoleApp
        public static void Copy()
        {
            using (client = new AmazonS3Client("AKIAI4YXWZLJGJQBSZ6Q", "FctcgO3mUeaTt+oXOj5WxQWXSzTkOWiuMav26dz5", Amazon.RegionEndpoint.APSoutheast1))
            {
                var copyObjectRequest = new CopyObjectRequest
                {
                    SourceBucket      = bucketName + "/temp",
                    SourceKey         = "17d8d41345aa4185b591ba5f55ef53fc.png",
                    DestinationBucket = bucketName + "/store",
                    DestinationKey    = "17d8d41345aa4185b591ba5f55ef53fc.png",
                };

                client.CopyObjectAsync(copyObjectRequest).Wait();

                Console.WriteLine("Copy successfull");
            }
        }
コード例 #17
0
        private async Task UpdateReadTimeAsync(ObjectIdentity identity, IAmazonS3 s3Client)
        {
            var metadataCollection = await GetObjectMetadataAsync(s3Client, identity, false);

            metadataCollection[MetadataKeys.ReadTime] = RebusTime.Now.ToString("O");
            var copyObjectRequest = new CopyObjectRequest
            {
                SourceBucket      = _options.BucketName,
                DestinationBucket = _options.BucketName,
                SourceKey         = identity.Key,
                DestinationKey    = identity.Key,
                MetadataDirective = S3MetadataDirective.REPLACE
            };

            metadataCollection.SaveTo(copyObjectRequest.Metadata);
            await s3Client.CopyObjectAsync(copyObjectRequest);
        }
コード例 #18
0
        /// <summary>
        /// moves file (object) between bucket folders
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="sourceDirectory"></param>
        /// <param name="destDirectory"></param>
        /// <returns></returns>
        public async Task <bool> MoveFileAsync(string fileName, string sourceDirectory, string destDirectory)
        {
            try
            {
                var copyRequest = new CopyObjectRequest
                {
                    SourceBucket      = _s3BucketOptions.BucketName + @"/" + sourceDirectory,
                    SourceKey         = fileName,
                    DestinationBucket = _s3BucketOptions.BucketName + @"/" + destDirectory,
                    DestinationKey    = fileName
                };
                var response = await _s3Client.CopyObjectAsync(copyRequest);

                if (response.HttpStatusCode == HttpStatusCode.OK)
                {
                    var deleteRequest = new DeleteObjectRequest
                    {
                        BucketName = _s3BucketOptions.BucketName + @"/" + sourceDirectory,
                        Key        = fileName
                    };
                    await _s3Client.DeleteObjectAsync(deleteRequest);

                    return(true);
                }

                return(false);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Please check the provided AWS Credentials.");
                }
                else
                {
                    _logger.LogError("An error occurred with the message '{0}' when moving object",
                                     amazonS3Exception.Message);
                }

                return(false);
            }
        }
コード例 #19
0
        private void MoveS3ImageForReview(IAmazonS3 s3Client, ClassificationModel model)
        {
            var reviewImageCopyResult = s3Client.CopyObjectAsync(
                Constants.IMAGES_BUCKET,
                model.S3Path,
                NationalGalleryOfArtIndexer.BUCKET_REVIEW,
                model.S3Path
                ).Result;

            if (reviewImageCopyResult.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new Exception("Failed to copy image to review bucket");
            }

            var imageOriginalDeleteResult = s3Client.DeleteObjectAsync(Constants.IMAGES_BUCKET, model.S3Path).Result;

            if (!string.Equals(imageOriginalDeleteResult.DeleteMarker, "true", StringComparison.OrdinalIgnoreCase))
            {
                throw new Exception("Failed to delete image from primary bucket");
            }
        }
コード例 #20
0
 private async Task UploadFileAsync(string bucketName, string sourceKey, string destinationKey)
 {
     try
     {
         CopyObjectRequest request = new CopyObjectRequest
         {
             SourceBucket      = bucketName,
             SourceKey         = sourceKey,
             DestinationBucket = bucketName,
             DestinationKey    = destinationKey
         };
         CopyObjectResponse response = await _s3Client.CopyObjectAsync(request);
     }
     catch (AmazonS3Exception e)
     {
         Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
     }
     catch (Exception e)
     {
         Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
     }
 }
コード例 #21
0
 public void CopyObject(String bucketPath, String newBucketPath)
 {
     try
     {
         CopyObjectRequest request = new CopyObjectRequest
         {
             SourceBucket      = _bucketName,
             SourceKey         = bucketPath,
             DestinationBucket = _bucketName,
             DestinationKey    = newBucketPath
         };
         CopyObjectResponse response = _client.CopyObjectAsync(request).Result;
     }
     catch (AmazonS3Exception e)
     {
         Console.WriteLine("Error encountered on server. Message:'{0}' when copying an object", e.Message);
     }
     catch (Exception e)
     {
         Console.WriteLine("Unknown encountered on server. Message:'{0}' when copying an object", e.Message);
     }
 }
コード例 #22
0
        public async Task <S3Response> CopyObjectAsync(string pathOrigem, string pathDestino, string bucketOrigem, string bucketDestino)
        {
            try
            {
                using (Amazon.S3.Transfer.TransferUtility transferUti = new Amazon.S3.Transfer.TransferUtility(_client))
                {
                    CopyObjectRequest request = new CopyObjectRequest
                    {
                        SourceBucket      = bucketOrigem,
                        SourceKey         = pathOrigem,
                        DestinationBucket = bucketDestino,
                        DestinationKey    = pathDestino
                    };
                    await _client.CopyObjectAsync(request);

                    return(new S3Response
                    {
                        Status = HttpStatusCode.OK,
                        Message = "Enviado com sucesso."
                    });
                }
            }
            catch (AmazonS3Exception e)
            {
                return(new S3Response
                {
                    Message = e.Message,
                    Status = e.StatusCode
                });
            }
            catch (Exception e)
            {
                return(new S3Response
                {
                    Status = HttpStatusCode.InternalServerError,
                    Message = e.Message
                });
            }
        }
コード例 #23
0
 public async Task CopyObject(string srcBuctet, string srcKey, string destBucket, string destKey)
 {
     try
     {
         CopyObjectRequest request = new CopyObjectRequest
         {
             SourceBucket      = srcBuctet,
             SourceKey         = srcKey,
             DestinationBucket = destBucket,
             DestinationKey    = destKey
         };
         CopyObjectResponse response = await client.CopyObjectAsync(request);
     }
     catch (AmazonS3Exception e)
     {
         Console.WriteLine("Error encountered on server. Message:'{0}' when copying an object, keyname {1}", e.Message, srcKey);
     }
     catch (Exception e)
     {
         Console.WriteLine("Unknown encountered on server. Message:'{0}' when copying an object, keyname {1}", e.Message, srcKey);
     }
 }
コード例 #24
0
 private static async Task CopyingObjectAsync()
 {
     try
     {
         CopyObjectRequest request = new CopyObjectRequest
         {
             SourceBucket      = sourceBucket,
             SourceKey         = objectKey,
             DestinationBucket = destinationBucket,
             DestinationKey    = destObjectKey
         };
         CopyObjectResponse response = await s3Client.CopyObjectAsync(request);
     }
     catch (AmazonS3Exception e)
     {
         Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
     }
     catch (Exception e)
     {
         Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
     }
 }
コード例 #25
0
        public async Task RenameAsync(string srcFile, string dstFile, AccessMode mode)
        {
            var srcKey = srcFile.CheckSystemFile().NormalizeLinux();
            var dstKey = dstFile.CheckSystemFile().NormalizeLinux();

            srcFile.CheckSystemFile();
            dstFile.CheckSystemFile();
            await myS3Client.CopyObjectAsync(new CopyObjectRequest
            {
                SourceBucket      = myBucketName,
                SourceKey         = srcKey,
                DestinationBucket = myBucketName,
                DestinationKey    = dstKey,
                CannedACL         = GetS3CannedAcl(mode, mySupportAcl)
            });

            await myS3Client.DeleteObjectAsync(new DeleteObjectRequest
            {
                BucketName = myBucketName,
                Key        = srcKey
            });
        }
コード例 #26
0
        public async Task <S3Response> CopyingObjectAsync(string bucketName, string folderFile, string destinationfolder)
        {
            try {
                using (_client) {
                    CopyObjectRequest request = new CopyObjectRequest();
                    request.SourceBucket      = bucketName;
                    request.SourceKey         = folderFile.Replace(@"\", "/");
                    request.DestinationBucket = bucketName;
                    request.DestinationKey    = destinationfolder.Replace(@"\", "/");
                    CopyObjectResponse response = await _client.CopyObjectAsync(request);

                    return(new S3Response {
                        Message = response.ResponseMetadata.RequestId,
                        Status = response.HttpStatusCode
                    });
                }
            } catch (AmazonS3Exception e) {
                return(new S3Response {
                    Message = e.Message,
                    Status = e.StatusCode
                });
            }
        }
コード例 #27
0
        public async Task Copy(string key, int age, CancellationToken stoppingToken)
        {
            string archiveBucketName;

            if (age > 30)
            {
                archiveBucketName = "glacier-bucket";
            }
            else
            {
                archiveBucketName = "normal-bucket";
            }

            var copyRequest = new CopyObjectRequest()
            {
                SourceBucket      = liveBucketName,
                SourceKey         = key,
                DestinationBucket = archiveBucketName,
                DestinationKey    = key,
            };

            await s3.CopyObjectAsync(copyRequest, stoppingToken);
        }
コード例 #28
0
 public static CopyObjectResponse CopyObject(this IAmazonS3 client, CopyObjectRequest request)
 {
     return(client.CopyObjectAsync(request).GetResult());
 }
コード例 #29
0
        /// <summary>
        /// Copy a file or key to another location
        /// </summary>
        /// <param name="sourceBucket">source bucket name</param>
        /// <param name="sourceKey">source key</param>
        /// <param name="targetBucket">target bucket name</param>
        /// <param name="targetKey">traget key</param>
        /// <returns>target key</returns>
        public async Task <string> CopyKey(string sourceBucket, string sourceKey, string targetBucket, string targetKey)
        {
            var copy = await _s3Client.CopyObjectAsync(sourceBucket, sourceKey, targetBucket, targetKey);

            return(targetKey);
        }
コード例 #30
0
        /// <summary>
        /// Provides the actual implementation to move or copy an S3 object
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="partSize"></param>
        /// <param name="deleteSource"></param>
        /// <returns></returns>
        private static async Task <CopyObjectRequestResponse> CopyOrMoveObjectAsync(this IAmazonS3 client, CopyObjectRequest request, long partSize, bool deleteSource, Func <long, long, bool> useMulitpart)
        {
            /// Handle operation cancelled exceptions
            ExponentialBackoffAndRetryClient backoffClient = new ExponentialBackoffAndRetryClient(4, 100, 1000)
            {
                ExceptionHandlingLogic = (ex) =>
                {
                    if (ex is OperationCanceledException)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            };

            try
            {
                ParameterTests.NonNull(request, "request");
                ParameterTests.OutOfRange(partSize >= Constants.MINIMUM_MULTIPART_PART_SIZE, "partSize", $"The part size must be at least {Constants.MINIMUM_MULTIPART_PART_SIZE} bytes.");
                ParameterTests.OutOfRange(partSize <= Constants.MAXIMUM_MULTIPART_PART_SIZE, "partSize", $"The part size cannot exceed {Constants.MAXIMUM_MULTIPART_PART_SIZE} bytes.");

                if (request.SourceKey == request.DestinationKey &&
                    request.SourceBucket != null &&
                    request.SourceBucket.Equals(request.DestinationBucket, StringComparison.OrdinalIgnoreCase))
                {
                    throw new SourceDestinationSameException("The source and destination of the copy operation cannot be the same.", new CopyObjectRequest[] { request });
                }

                // Get the size of the object.
                GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest
                {
                    BucketName = request.SourceBucket,
                    Key        = request.SourceKey
                };

                long objectSize;
                GetObjectMetadataResponse metadataResponse;

                try
                {
                    metadataResponse = await backoffClient.RunAsync(() => client.GetObjectMetadataAsync(metadataRequest));

                    objectSize = metadataResponse.ContentLength; // Length in bytes.
                }
                catch (Exception e)
                {
                    throw e;
                }

                CopyObjectResponse response = null;

                if (UseMultipart(objectSize, partSize))
                {
                    // If it takes more than a 5 GiB part to make 10000 or less parts, than this operation
                    // isn't supported for an object this size
                    if (objectSize / partSize > Constants.MAXIMUM_PARTS)
                    {
                        throw new NotSupportedException($"The object size, {objectSize}, cannot be broken into fewer than {Constants.MAXIMUM_PARTS} parts using a part size of {partSize} bytes.");
                    }

                    List <Task <CopyPartResponse> > copyResponses = new List <Task <CopyPartResponse> >();

                    // This property has a nullable backing private field that when set to
                    // anything non-null causes the x-amz-object-lock-retain-until-date
                    // header to be sent which in turn results in an exception being thrown
                    // that the Bucket is missing ObjectLockConfiguration
                    InitiateMultipartUploadRequest initiateRequest = request.ConvertTo <InitiateMultipartUploadRequest>("ObjectLockRetainUntilDate");
                    initiateRequest.BucketName = request.DestinationBucket;
                    initiateRequest.Key        = request.DestinationKey;

                    InitiateMultipartUploadResponse initiateResponse = await backoffClient.RunAsync(() => client.InitiateMultipartUploadAsync(initiateRequest));

                    try
                    {
                        long bytePosition = 0;
                        int  counter      = 1;

                        // Launch all of the copy parts
                        while (bytePosition < objectSize)
                        {
                            CopyPartRequest copyRequest = request.ConvertTo <CopyPartRequest>("ObjectLockRetainUntilDate");
                            copyRequest.UploadId  = initiateResponse.UploadId;
                            copyRequest.FirstByte = bytePosition;
                            // If we're on the last part, the last byte is the object size minus 1, otherwise the last byte is the part size minus one
                            // added to the current byte position
                            copyRequest.LastByte   = ((bytePosition + partSize - 1) >= objectSize) ? objectSize - 1 : bytePosition + partSize - 1;
                            copyRequest.PartNumber = counter++;

                            copyResponses.Add(backoffClient.RunAsync(() => client.CopyPartAsync(copyRequest)));

                            bytePosition += partSize;
                        }

                        IEnumerable <CopyPartResponse> responses = (await Task.WhenAll(copyResponses)).OrderBy(x => x.PartNumber);

                        // Set up to complete the copy.
                        CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest
                        {
                            BucketName = request.DestinationBucket,
                            Key        = request.DestinationKey,
                            UploadId   = initiateResponse.UploadId
                        };

                        completeRequest.AddPartETags(responses);

                        // Complete the copy.
                        CompleteMultipartUploadResponse completeUploadResponse = await backoffClient.RunAsync(() => client.CompleteMultipartUploadAsync(completeRequest));

                        response = completeUploadResponse.CopyProperties <CopyObjectResponse>();
                        response.SourceVersionId = metadataResponse.VersionId;
                    }
                    catch (AmazonS3Exception e)
                    {
                        AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest()
                        {
                            BucketName = request.DestinationBucket,
                            Key        = request.DestinationKey,
                            UploadId   = initiateResponse.UploadId
                        };

                        await backoffClient.RunAsync(() => client.AbortMultipartUploadAsync(abortRequest));

                        throw e;
                    }
                }
                else
                {
                    response = await backoffClient.RunAsync(() => client.CopyObjectAsync(request));
                }

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new AmazonS3Exception($"Could not copy object from s3://{request.SourceBucket}/{request.SourceKey} to s3://{request.DestinationBucket}/{request.DestinationKey}. Received response : {(int)response.HttpStatusCode}");
                }
                else
                {
                    // We already checked to make sure the source and destination weren't the same
                    // and it's safe to delete the source object
                    if (deleteSource)
                    {
                        DeleteObjectRequest deleteRequest = new DeleteObjectRequest()
                        {
                            BucketName = request.SourceBucket,
                            Key        = request.SourceKey
                        };

                        DeleteObjectResponse deleteResponse = await backoffClient.RunAsync(() => client.DeleteObjectAsync(deleteRequest));

                        if (deleteResponse.HttpStatusCode != HttpStatusCode.NoContent)
                        {
                            throw new AmazonS3Exception($"Could not delete s3://{request.SourceBucket}/{request.SourceKey}. Received response : {(int)deleteResponse.HttpStatusCode}");
                        }
                    }

                    return(new CopyObjectRequestResponse(request, response));
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }