Reputation: 1229
I have recently been trying ot write code to add and delete content form an Amazon S3 bucket. I am completely new to Amazon S3 and the AmazonWS .Net SDK.
The bucket region endpoint is http://sqs.eu-west-1.amazonaws.com so I constructed my client like this:
_s3Client = AWSClientFactory.CreateAmazonS3Client(accessKey, awsSecretKey, new AmazonS3Config().WithServiceURL("http://sqs.eu-west-1.amazonaws.com"));
If I leave out the AmazonS3Config bit I get this error:
A redirect was returned without a new location. This can be caused by attempting to access buckets with periods in the name in a different region then the client is configured for.
When I put in the AmazonS3Config bit I no longer get that error but I appear to have no access to this bucket at all or any other bucket that I would usually have access to. Any request I send returns null.
I have tested my code with other buckets that are configured to the standard US region and it all works well. The single difference is in the CreateAmazonS3Client method where I set the config with the EU endpoint.
Could anybody give me some guidance on how I should set up my client to work with a bucket in the EU(Ireland) region. I have been searching for a few hours and every tutorial or document I have followed has not worked so far.
Upvotes: 7
Views: 12565
Reputation: 1976
To whom it may still concern..
With the old AWS SDKs (version 1), you can simply create the S3 client without a region or an AmazonS3Config
. No need to specify a service URL, it uses the default, mentioned above, for you. The only time you really need the region for work with S3 is when you create a bucket which is probably rarely a requirement for an application.
This works for me and all communication I perform to S3 is over https.
With the new AWS SDK for .Net (version 2 and above) it seems the region parameter is required and in fact the AmazonS3Client
would throw an exception if not given one. I've tried working around this limitation with specifying a generic https://s3.amazonaws.com
URL and failed, because the new SDK does not follow the 301 redirect from the default (US-EAST-1 I think) endpoint.
So in summary, best to specify region, even on the old API, to avoid breaking in the future. If your application is making cross-region calls, and are slower (possibly) and more expensive, it's probably best that your code will testify to that.
Upvotes: 6
Reputation: 18832
Just use the standard endpoint - s3.amazonaws.com
AmazonS3Config S3Config = new AmazonS3Config {
ServiceURL = "s3.amazonaws.com",
CommunicationProtocol = Amazon.S3.Model.Protocol.HTTP
};
AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWS_Key, AWS_SecretKey, S3Config);
PutObjectRequest UploadToS3Request = new PutObjectRequest();
UploadToS3Request.WithFilePath(localPath)
.WithBucketName(bucket)
.WithKey(key);
client.PutObject(UploadToS3Request);
Upvotes: 17