2

I'm going through the tutorials by AWS Mobile SDK - Xamarin Developer Guide and I want to upload a text file from my Android phone (through Xamarin) to AWS S3. These are the 2 tutorials: Setting Up the AWS Mobile SDK for .NET and Xamarin and Store and Retrieve Files with Amazon S3.

I'm using the method void Amazon.S3.Transfer.TransferUtility.Upload(string,string,string) to upload my text file. I tried running my code but I couldn't. I run into this exception **System.MissingMethodException:** 'Method not found: void Amazon.S3.Transfer.TransferUtility.Upload(string,string,string)' despite the fact that I have included the correct namespaces. I have confirmed that the method void Amazon.S3.Transfer.TransferUtility.Upload(string,string,string) is found in the namespace Amazon.S3.Transfer. Below is my code

client = new AmazonS3Client(credentials, bucketRegion);
var fileTransferUtility = new TransferUtility(client);
fileTransferUtility.Upload(_fileName, bucketName, "toDo.txt");

I've included the correct namespaces, such as using Amazon.S3; using Amazon.S3.Transfer;

The above code works for Windows Presentation Foundation (WPF) Application, but not on Xamarin.

I've add the latest version of

  • AWSSDK.Core (v3.3.107.8)
  • AWSSDK.S3 (v3.3.111.9)

How do I resolve this error?

Any help is highly appreciated. Thank you so much in advance.

2 Answers2

1

I have successfully uploaded my text file. Many thanks to @AniketBhansali for suggesting to check for internal exception, and giving UploadAsync a try. Let me detail my debugging process.

In summary, to upload my text file, I

  1. Checked for internal exception
  2. Did UploadAsync
  3. Changed my credentials from the one Amazon Cognito credentials provider gave, to the IAM user I'm using to access my AWS account

For some reason, transferUtility.Upload() is not working, but transferUtility.UploadAsync() is.

So at first I checked for S3 specific exception like this

try
{
    var fileTransferUtility = new TransferUtility(client);
    fileTransferUtility.Upload(_fileName, bucketName, "toDo.txt");
}
catch(AmazonS3Exception exception)
{
    Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", exception.Message);
}
catch (Exception exception)
{
    Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", exception.Message);
}

However, the same exception message still occured. So instead of Upload I tried UploadAsync like this

await fileTransferUtility.UploadAsync(_fileName, bucketName, "toDo.txt");

And thankfully, I got a new error message. Unknown encountered on server. Message:'Object reference not set to an instance of an object' when writing an object

I Google Searched and found this Null reference exception with AWS S3 API.

From this question thread, I suspected that the error occured because I was using the credentials from Amazon Cognito credentials provider.

Therefore, I changed the credentials to the IAM user I'm using to access my AWS account and I managed to send my text file to AWS S3.

0

AWS evolves pretty quickly, so it is possible that they stopped supporting the older methods in their newer SDKs. The latest guide from AWS recommends using transferUtility.Upload(file, bucketName) instead. This is what it should look like:

// Initialize the AWS TransferUtility client
var s3Client = new AmazonS3Client(credentials,region);
var transferUtility = new TransferUtility(s3Client);
// Upload on the Transfer Utility object
transferUtility.Upload(
  Path.Combine(Environment.SpecialFolder.ApplicationData,"file"),
  "bucketName"
);
Saamer
  • 4,687
  • 1
  • 13
  • 55