Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 API .Net C# - Accessing external Bucket

I am trying to access an external bucket over the Amazon S3 API through .Net / C#.

I already tried the login with a 3rd party tool which worked like a charm, now I want to get the items of the bucket inside the framework. I am using this behind a Proxy, that's why I am using the S3config.

AmazonS3Config S3Config = new AmazonS3Config()
                        {
                            CommunicationProtocol = Protocol.HTTP,
                            ProxyHost = Proxy.Host,
                            ProxyPort = Convert.ToInt32(Proxy.Port),
                            ProxyCredentials = new NetworkCredential
                                                {
                                                    UserName = Proxy.User,
                                                    Password = Proxy.Password
                                                }
                        };
AmazonS3 S3Client = Amazon.AWSClientFactory.CreateAmazonS3Client(AccessKey, SecretKey,S3Config);

that's the way I establish the connection itself to amazon. I also already tried placing

ServiceURL = "s3.amazonaws.com",

into the config object initializer because I am in EU and the bucket is located somewhere in US.

When I now try to access via :

ListObjectsRequest req = new ListObjectsRequest { BucketName = "yelp-syndication" };
ListObjectsResponse resp = S3Client.ListObjects(req);

or

AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID);
return client.ListBuckets().Buckets.Where(b => b.BucketName == bucket).Single();

or

ListObjectsRequest Request = new ListObjectsRequest
                            {
                            BucketName = BucketName
                            };
ListObjectsResponse Response = S3Client.ListObjects(Request);

I only get Access Denied in the error object that is thrown. The credentials I use are 100% the same as in the 3rd party tool.

Am I missing something here ? do I need to use any special way which I just can't find to make it work ?

a working python snippet is:

from boto.s3.connection import S3Connection
 conn = S3Connection(<ACCESS_KEY_ID>, <SECRET_ACCESS_KEY)
 bucket = conn.get_bucket(<BucketName>, validate=False)

this returns correct results, so the actual connection works and also the credentials.

like image 877
makuro Avatar asked Sep 07 '25 18:09

makuro


1 Answers

After using the given answers as a new base for research I figured out, that I have to give a serviceurl, a regionendpoint and a communicationprotocol for the S3Config Class on the one side and, because I knew the exact name of the file within the bucket, I needed to use getobject and not an access to the bucket.

so the code that got me working is:

            AmazonS3Config S3Config = new AmazonS3Config
                                  {
                                      ServiceURL = "s3.amazonaws.com",
                                      RegionEndpoint = RegionEndpoint.USWest2,
                                      CommunicationProtocol = Protocol.HTTPS,
                                      ProxyHost = Proxy.Host,
                                      ProxyPort = Convert.ToInt32(Proxy.Port),
                                      ProxyCredentials = new NetworkCredential
                                                         {
                                                             UserName = Proxy.User,
                                                             Password = Proxy.Password
                                                         }
                                  };
        _S3Client = AWSClientFactory.CreateAmazonS3Client(AccessKey, SecretKey, S3Config);
        _ObjRequest = new GetObjectRequest
                                      {
                                          BucketName = BucketName,
                                          Key = Key
                                      };
        _ObjResponse = _S3Client.GetObject(_ObjRequest);
        return (_ObjResponse != null);
like image 139
makuro Avatar answered Sep 10 '25 09:09

makuro