I am trying to download a file from S3 to a byte array iin .net using c#.
I am following the below method:
var client = new AmazonS3Client(AccessKey, SecretKey, Amazon.RegionEndpoint.EUWEST2);
using (client)
{
MemoryStream ms = new MemoryStream();
GetObjectRequest getObjectRequest = new GetObjectRequest();
getObjectRequest.BucketName = Bucketname;
getObjectRequest.Key = Keyname;
using (var getObjectResponse = client.GetObject(getObjectRequest))
{
getObjectResponse.ResponseStream.CopyTo(ms);
}
}
I referred to a stackoverflow answer and followed the above method.
However I am getting the following error saying;
GetObject is inaccessible due to protection level.
I am just learning S3, I am now confused if this error is because of bucket policy or class scope.
You mention .NET Core: most of the AWS APIs for .Net Core and .NET Standard only provide Async variants. You should use GetObjectAsync
.
It's surprising that Amazon introduced this incompatibility as it would have been easy to provide a synchronous version, using code something like:
var task = s3Client.GetObjectAsync(request);
task.Wait();
return task.Result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With