Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy between two AWS buckets - Stream returned from AWS: HashStream does not support seeking

I am trying to re-upload a stream that I just retrieved. It shouldn't really matter that I am using AWS I believe... Maybe my understanding of working with streams is just too limited? :-)

I am using the following method straight from the AWS documentation to download and upload streams:

File upload:

public bool UploadFile(string keyName, Stream stream)
{
using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1))
{
    try
    {
        TransferUtility fileTransferUtility = new TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

        fileTransferUtility.Upload(stream, bucketName, keyName);

        return true;
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        [...]
    }
}
}

Getting the file:

public Stream GetFile(string keyName)
{    
using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2))
{
    try
    {
        GetObjectRequest request = new GetObjectRequest
        {
            BucketName = bucketName,
            Key = keyName
        };

        GetObjectResponse response = client.GetObject(request);
        responseStream = response.ResponseStream;

        return responseStream;
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        [...]
    }
}
}

Now, I am trying to combine the two methods: I am getting a stream and immediately want to upload it again. However, I am getting the following error message: System.NotSupportedException : HashStream does not support seeking

I am guessing it has something to do that I am getting a stream which is somehow now immediately ready to be uploaded again?

This is how I am trying to get the stream of an existing file (.jpg) and immediately try to upload it with a different filename:

newAWS.UploadFile(newFileName, oldAWS.GetFile(oldFile));

Where newAWS and oldAWS are instances of the AWS class, and newFileName is a string :-) This is the line I am getting the error message pasted above.

Please let me know if I am missing something obvious here why I would not be able to re-upload a fetched stream. Or could it be that it is related to something else I am not aware of and I am on the wrong track trying to troubleshoot the returned stream?

What I am basically trying to do is to copy one file from an AWS bucket to another using streams. But for some reason I get the error message trying to upload the stream that I just downloaded.

Thank you very much for taking your time digging through my code :-)

like image 822
jrn Avatar asked Mar 17 '17 15:03

jrn


People also ask

Why can't I copy an object between two Amazon S3 buckets?

If the object that you can't copy between buckets is owned by another account, then the object owner can do one of the following: The object owner can grant the bucket owner full control of the object. After the bucket owner owns the object, the bucket policy applies to the object.

What does AWS S3 do?

Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere.


1 Answers

As the error indicates, the Hashstream object that Amazon returns does not support seeking.

Something in your code, or a method you're calling, is trying to do seeking on that stream. Most likely it's trying to seek to the beginning of the stream.

So you need to convert Hashstream to a different stream that supports seeking:

using (GetObjectResponse response = client.GetObject(request))
using (Stream responseStream = response.ResponseStream)
using (MemoryStream memStream = new MemoryStream())
{
    responseStream.CopyTo(memStream);
    memStream.Seek(0, SeekOrigin.Begin);

    // now use memStream wherever you were previously using responseStream
}
like image 130
Doug S Avatar answered Oct 15 '22 12:10

Doug S