Transfer the file from your amazon s3 account to another

I am writing an application and we need to transfer files between different s3 accounts, we do not want to save the file locally before uploading to the target s3 account.
So we get the source file stream using this:

public Stream GetFileStream(string path) { var pathPieces = path.Split('/'); string bucket = pathPieces[0]; string fileName = pathPieces[pathPieces.Length - 1]; GetObjectRequest request = new GetObjectRequest(); request.BucketName = bucket; request.Key = fileName; GetObjectResponse response = Client.GetObject(request); return response.ResponseStream; } 

And after that we use this stream to load to the destination:

  TransferUtility transferUtility = new TransferUtility(Client); transferUtility.S3Client.PutBucket(new PutBucketRequest() { UseClientRegion = true }.WithBucketName(bucket)); TransferUtilityUploadRequest request = new TransferUtilityUploadRequest() .WithBucketName(bucket) .WithKey(key) .WithPartSize(1024) .WithTimeout(100 * 60 * 60 * 1000) .WithAutoCloseStream(true) .WithCannedACL(S3CannedACL.PublicReadWrite) .WithInputStream(fileStream) as TransferUtilityUploadRequest; transferUtility.Upload(request); 

But in the last line of the call to the "Download" function, I always get this error: This thread does not support search operations.

Is there any other way to do what I'm trying to do? because it is not the right way.

+4
source share
3 answers

I came across this issue and published the approach that I used to solve it. I borrowed some code from John Skeet. This worked for me, but I know it is a little awkward and probably could be improved.

 string filename = "nameoffiletocopy" using (Stream stream = GetS3Stream(filename)) { AmazonS3Config config = new AmazonS3Config {CommunicationProtocol = Protocol.HTTP}; AmazonS3 client = AWSClientFactory.CreateAmazonS3Client( "destinationaccesskey", "destinationsecretacceskey", config); string destinationBucketName = "destinationbucketname"; PutObjectRequest request = new PutObjectRequest { BucketName = destinationBucketName, InputStream = stream, Key = filename, CannedACL = S3CannedACL.PublicRead }; PutObjectResponse response = client.PutObject(request); } private static Stream GetS3Stream(string filename) { AmazonS3Config config = new AmazonS3Config {CommunicationProtocol = Protocol.HTTP}; AmazonS3 client = AWSClientFactory.CreateAmazonS3Client( "sourceaccesskey", "sourcesecretacceskey", config); string sourceBucketName = "sourcebucketname"; GetObjectRequest request = new GetObjectRequest { BucketName = sourceBucketName, Key = filename }; using (GetObjectResponse response = client.GetObject(request)) { byte[] binaryData = ReadFully(response.ResponseStream, -1); //ReadyFully from Jon Skeet blog return new MemoryStream(binaryData); } } 

ReadFully method fully explained by Jon Skeet

+2
source

Amazon has a request to copy objects

 AmazonS3Client client = new AmazonS3Client(); CopyObjectRequest request = new CopyObjectRequest { SourceBucket = "fromBucket", SourceKey = "fromFile", DestinationBucket = "toBucket", DestinationKey = "toFilename", CannedACL = S3CannedACL.BucketOwnerFullControl }; CopyObjectResponse response = client.CopyObject(request); 
+1
source

I really don't think there is a way to transfer between accounts. The only protocol I know that can do this is FXP . You can query AWS to do so in the future, it would be a nice feature.

0
source

All Articles