1

I use the TransferUtility to get a stream from a file on s3. If I CopyTo the stream to another stream, it'll copy correctly, if I ReadAsync it into a byte[], the result will be only correct up to about 16kb. (So I get different hashes in the following code).

What am I missing?

        private static (string region, string bucketName, string path, string accessKey, string secretAccessKey, string targetFile) info =
            ("eu-west-1", "MyBucketName", "MyFilePath", "MyAccessKey", "MySecretAccessKey", "target/path/on/local/machine.pdf");

        static async Task Main()
        {
            var request = new TransferUtilityOpenStreamRequest
            {
                BucketName = info.bucketName,
                Key = info.path
            };
            var transferUtilty = new TransferUtility(info.accessKey, info.secretAccessKey, Amazon.RegionEndpoint.EUWest1);
            using (var s3Stream = await transferUtilty.OpenStreamAsync(request))
            using (var fileStream = new FileStream(info.targetFile, FileMode.Create, FileAccess.Write))
            {
                await s3Stream.CopyToAsync(fileStream);
                fileStream.Close();
            }
            using (var fileStream = new FileStream(info.targetFile, FileMode.Open, FileAccess.Read))
            {
                var bytes = new byte[fileStream.Length];
                await fileStream.ReadAsync(bytes, 0, bytes.Length);
                Console.WriteLine(Convert.ToBase64String(MD5.Create().ComputeHash(bytes)));
            }
            using (var s3Stream = await transferUtilty.OpenStreamAsync(request))
            {
                var bytes = new byte[s3Stream.Length];
                await s3Stream.ReadAsync(bytes, 0, bytes.Length);
                Console.WriteLine(Convert.ToBase64String(MD5.Create().ComputeHash(bytes)));
            }
        }

p.s: The file is about 60kb. I also give the project file, just in case.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="System.Net.Http" Version="4.3.4" />
    <PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.101" />
    <PackageReference Include="AWSSDK.S3" Version="3.3.110.50" />
  </ItemGroup>
</Project>
Alireza
  • 5,421
  • 5
  • 34
  • 67
  • 1
    Does `ReadAsync` return anything useful? https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.readasync?view=netframework-4.8 – mjwills Apr 14 '20 at 06:26
  • 2
    Don't ignore the return value of ReadAsync(). It returns the number of bytes that were read. keep an offset variable, increment it by the return value of ReadAsync, and loop until ReadAsync returns 0. "the result will be only correct up to about 16kb": I think the result is only 16 kb since you only call ReadAsync once – Oguz Ozgul Apr 14 '20 at 06:52

0 Answers0