Home > Mobile >  S3 file uploaded is getting corrupted
S3 file uploaded is getting corrupted

Time:05-11

I have some code that uploads filestream to s3 bucket. One of my customer is having some issues but I'm having trouble reproducing it on my end. Upon upload, their filesize remains at 0 bytes. This does not occur every time. Seems very sporadic.

enter image description here

using (var webclient = new WebClient())
{
    using (MemoryStream stream = new MemoryStream(webclient.DownloadData(uri)))
    {
        using (var client = new AmazonS3Client())
        {
            PutObjectRequest request = new PutObjectRequest
            {
                BucketName = bucketName,
                Key = GetObjectKey(fileName, companyAccountId),
                InputStream = stream
            };

            if (height > 0)
                request.Metadata.Add("x-amz-meta-height", height.ToString());

            if (width > 0)
                request.Metadata.Add("x-amz-meta-width", width.ToString());

            var response = await client.PutObjectAsync(request);
        }
    }
}

Any help or suggestion would be greatly appreciated to help determine why a file being uploaded is remaining at 0 bytes.

CodePudding user response:

Is there anything else that could be consuming the inputStream and leaving the position at the end? I have had a similar problem before and solved the problem by ensuring the stream is at the start by either

stream.Seek(0, SeekOrigin.Begin);

or

stream.Position = 0;
  • Related