Access S3 files from Unity for mobile development

0

I'm trying to configure the AWS S3 service to download the included files in a bucket using Unity for mobile. I downloaded the SDK package and I got it installed. From AWS console I set up a IAM policy and roles for unauth users I created a Cognito IdentityPool and got the relative id I set up the S3 bucket and its policy using the generator, including the arn:aws:iam::{id}:role/{cognito unauth role} and the resource arn:aws:s3:::{bucket name}/*. In code I set credentials and region and create CognitoAWSCredentials (C# used)

_credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoIdentityRegion);

then I create the client:

 _s3Client = new AmazonS3Client(_credentials, RegionEndpoint.EUCentral1);  
// the region is the same in _CognitoIdentityRegion

I then try to use the s3Client to get my files (in bucketname subfolders)

private void GetAWSObject(string S3BucketName, string folder, string sampleFileName, IAmazonS3 s3Client)
    {
        string message = string.Format("fetching {0} from bucket {1}", sampleFileName, S3BucketName);
        Debug.LogWarning(message);
        s3Client.GetObjectAsync(S3BucketName, folder + "/" + sampleFileName, (responseObj) =>
        {
            var response = responseObj.Response;
            if (response.ResponseStream != null)
            {
                string path = Application.persistentDataPath + "/" + folder + "/" + sampleFileName;
                Debug.LogWarning("\nDownload path AWS: " + path);
                using (var fs = System.IO.File.Create(path))
                {
                    byte[] buffer = new byte[81920];
                    int count;
                    while ((count = response.ResponseStream.Read(buffer, 0, buffer.Length)) != 0)
                        fs.Write(buffer, 0, count);
                    fs.Flush();
                }
            }
            else
            {
                Debug.LogWarning("-----> response.ResponseStream is null");
            }
        });
    }

At this point I cannot debug into the Async method, I don't get any kind of error, I don't get any file downloaded and I even cannot check is connection to AWS S3 has worked in some part of the script.

What am I doing wrong? Thanks for help a lot!

  • Could you please elaborate which SDK version you use? In the current V3 API, I don't see any IAmazonS3.GetObjectAsync() method signature that would match your usage (using a callback). Is GetObjectAsync an extension method here?

  • @Dmitry Balabanov Thank you for the comment! I'm using the aws sdk for unity v3.3.802.0 So it's a unitypackage and not the sdk for .NET; maybe the difference among the methods are due to that. For example the unity version lacks of the all Transfer methods including IAmazonS3.Transfer.TransferUtility... At the moment I'm gonna try to find a method to understand if at least my connection attempt goes right. Thank you again, best regards.

  • @Dmitry Balabanov Ok, I assume I misunderstood your words. I'm following the AWS Amazon guidelines for GetObjecAsync(). It has the following possible args (including callback): void IAmazonS3.GetObjectAsync(string bucketName, string key, AmazonServiceCallback<Amazon.S3.Model.GetObjectRequest, Amazon.S3.Model.GetObjectResponse> callback, [AsyncOptions options = null]). Am I missing to pass the options? Cause the callback, the objectResponse, nor other args, asks me for signature.

  • If you are using Unity 2018.1 or later, please use the AWS SDK for .NET binaries. The AWS Mobile SDK for Unity is for legacy Unity support, use at your own risk. Please consider migrating your app to a newer version of Unity, and use the AWS SDK for .NET - .NET Standard 2.0 binaries.

1 Answer
0

Per the discussion in the comments, leaving an answer to this question to remove it from the unanswered column. Please let us know if you need further assistance!

AWS
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions