6

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.

peterh
  • 1
  • 15
  • 76
  • 99
  • 3
    Possible duplicate of [Public class is inaccessible due to its protection level](https://stackoverflow.com/questions/18404264/public-class-is-inaccessible-due-to-its-protection-level) – vahdet Nov 26 '18 at 11:01
  • 1
    @vahdet - not a duplicate of the question you linked – Joe Nov 26 '18 at 12:28

1 Answers1

17

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;
Joe
  • 118,426
  • 28
  • 194
  • 329