I am currently following Amazon's AWSSDK documentation (in particular: this) in an attempt to access AmazonS3 data on a Xamarin forms application. I ran into an error, so I started a fresh project with fresh packages, all up to date as far as I can tell, and am still encountering the same problem.
Here is the current code for the main code behind class:
public partial class MainPage : ContentPage
{
private static IAmazonS3 client;
private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
public MainPage()
{
InitializeComponent();
// I have tried each of the following:
client = new AmazonS3Client(); <--- ERROR
client = new AmazonS3Client(RegionEndpoint.USWest2); <-- SIMILAR ERROR
client = new AmazonS3Client(bucketRegion); <-- SAME ERROR
// This has a different error, lack of RegionEndpoint, which is expected, but at least this seems to be recognized
client = new AmazonS3Client(new AnonymousAWSCredentials());
}
}
This results in the following runtime error on the indicated line:
Method not found: void Amazon.S3.AmazonS3Client..ctor()
I understand that MethodNotFoundExceptions often are the result of old versions of DLLs hanging around, outdated dependencies, duplicate assemblies, etc, but I have deployed this from a completely fresh project with the only packages installed being the up-to-date standard Xamarin packages and the Amazon.S3 package.
What is causing this error?
The issue has nothing to do with "old dlls, outdated dependencies", etc. If you look at the example that you are following, they are not trying to call the default ctor, but instead they are initialising it through a bucket region.
If you take a look at Simple cross-platform application using the AWS SDK for .NET (or even in the documentation inside the IDE that you are using), you can see that when the default ctor is being used, you need to:
Before running this app, credentials must be specified either in the [default] profile or in another profile and then by setting the AWS_PROFILE environment variable. A region must be specified either in the [default] profile or by setting the AWS_REGION environment variable.
The docs also say that App.config can be used.
But since we will be using it for Xamarin, it will be a lot easier to use some of the others overloads. Here are some of them:
Let's say that you want to use AWSCredentials
in order to generate your client. Again, you have many options here:
NB: When you initialise your client, it is a good practice to specify the RegionEndpoint
. In most of the cases, if you forget, you will get a AmazonClientException: No RegionEndpoint or ServiceURL configured
so it will remind you that it is required.
Edit: Due to an update in your question, here is an update from me:
The same rules apply for there ctors, that you have tried with. They are looking for credentials in config files, that are not present for a Xamarin.Forms app. In order to use the client, it needs to know about its credentials. If you need to use the client, then provide it with some credentials, during initialisation - use either some of the other AWSCredentials - Basic
, Federated
, etc, or use the simples one - with accessKeyId
+ accessKey
.
If you are curious why the ctors, that you have tried, are not working, or what they are doing behind-the-scenes, their SDK is open-sourced here. The empty ctor's code is here and the more interesting FallbackCredentialsFactory
here.
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