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>