Forum Cross Platform with Xamarin

Upload photo to s3

I am attempting to upload an image to AWS s3 using Xamarin and experiencing this error:

System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'System.Net.Sockets.NetworkStream'.

I've implemented as follows:

    public TransferUtility s3transferUtility;
    public AmazonS3Client s3Client;

    public AWSObject()
    {
    }

    public void setupAWSCredentials(){

        CognitoAWSCredentials credentials = new CognitoAWSCredentials(
            "eu-west-1:**", 
            RegionEndpoint.EUWest1
        );

        var config = new AmazonS3Config() { RegionEndpoint = Amazon.RegionEndpoint.EUWest1, Timeout = TimeSpan.FromSeconds(30), UseHttp = true };

        AWSConfigsS3.UseSignatureVersion4 = true;

        s3Client = new AmazonS3Client(credentials, config);
        this.s3transferUtility = new TransferUtility(s3Client);

    }

    public async Task UploadPhoto(string filename){

        try {

            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var filePath = Path.Combine(documentsPath, filename);

            TransferUtilityUploadRequest request = new TransferUtilityUploadRequest{BucketName = "xamarinbucket",FilePath = filePath,Key = "photoOne.png",ContentType = "image/png"};

            CancellationToken cancellationToken = new CancellationToken();

            await this.s3transferUtility.UploadAsync(request, cancellationToken);

        } catch (AmazonS3Exception s3Exception) {

            System.Console.WriteLine(s3Exception.StackTrace);
        }
    }

The functions are called as follows:

AWSObject awsObject = new AWSObject();
awsObject.setupAWSCredentials();
Task.Run(()=> awsObject.UploadPhoto("photoOne.png"));

Any suggestions ?

Tagged:

Answers

  • muktimukti USUniversity

    I am facing the same problem, please update if anybody got any solution

  • DonMesserli.1843DonMesserli.1843 USMember ✭✭✭

    Any solution to this problem?

  • RamAkkireddyRamAkkireddy USMember ✭✭

    I'm also facing the problem any solution

  • NicolaLoroNicolaLoro USMember ✭✭

    Same problem here :(

  • joscojosco Member ✭✭

    same problem

  • joscojosco Member ✭✭

    I have fixed it, the problem was here
    var s3Client = new AmazonS3Client(creds, RegionEndpoint.EUWest1);

    I was using EUWest1 then I changed to RegionEndpoint.USEast1 and I got another error:
    System.IO.IOException: Error writing request ---> System.Net.Sockets.SocketException: The socket has been shut down

    Then I changed to RegionEndpoint.USEast2 and the error was fixed

    I realize that my correct region was USEast2 in the url of my buckets that looks like this:

    https://s3.console.aws.amazon.com/s3/home?region=us-east-2

    At the end of the link you can see the region.

    my code is this:

    private TransferUtility transferUtility;
    
    private async void SetupAsync(){
    
    
        BasicAWSCredentials creds = new BasicAWSCredentials("MYACCESSKEY",
                                                                        "MYSECRETKEY");
    
    
                    var config = new TransferUtilityConfig();
                    config.ConcurrentServiceRequests = 10;
                    config.MinSizeBeforePartUpload = 16 * 1024 * 1024;
    
                    var s3Client = new AmazonS3Client(creds, RegionEndpoint.EUWest1);
                    ListBucketsResponse response = await s3Client.ListBucketsAsync();
                    Console.WriteLine("Buckets owner - {0}", response.Owner.DisplayName);
                    foreach (S3Bucket bucket in response.Buckets)
                    {
                        Console.WriteLine("Bucket {0}, Created on {1}", bucket.BucketName, bucket.CreationDate);
                    }
    
                    transferUtility = new TransferUtility(s3Client, config);
                }
    
                public async void UploadFileAsync(string filePath, string bucketName){
    
                    try
                    {
    
    
                        await transferUtility.UploadAsync(
                            filePath,
                            bucketName
                        );
                    }
                    catch(Exception e){
                        Debug.WriteLine("AWSS3 upload file exception = " + e);
                    }
    
    
                }
    

    call first setupAsyncMethod then UploadFileAsync

    I am using AWSSDK.S3 nuget plugin

Sign In or Register to comment.