S3 Get object not working properly in Unity

0

I am using AWS SDK .NET for Unity to download zip files from S3. I implemented the get method just as this tutorial for .NET https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html But when I call the method with ReadObjectDataAsync().Wait(); Unity stops and crashes, like is in an infinite loop. This is my code, has a different name but is practically the same:

/// <summary>
/// Start is called before the first frame update
/// </summary>
void Start()
{
    customSongsManager = gameObject.GetComponent<CustomSongsManager>();
    GetZip(S3SampleFile).Wait();
}

/// <summary>
/// Get Object from S3 Bucket
/// </summary>
public async Task GetZip(string pFile)
{
    string folder = "Assets/Audio/Custom/";

    try
    {
        GetObjectRequest request = new GetObjectRequest
        {
            BucketName = S3Bucket,
            Key = pFile
        };
        using (GetObjectResponse response = await S3Client.GetObjectAsync(request))
        using (Stream responseStream = response.ResponseStream)
        {
            string title = response.Metadata["x-amz-meta-title"]; // Assume you have "title" as medata added to the object.
            string contentType = response.Headers["Content-Type"];
            Debug.Log("Object metadata, Title: " + title);
            Debug.Log("Content type: " + contentType);

            if (responseStream != null)
            {
                using (BinaryReader bReader = new BinaryReader(response.ResponseStream))
                {
                    byte[] buffer = bReader.ReadBytes((int)response.ResponseStream.Length);
                    File.WriteAllBytes(folder + S3SampleFile, buffer);

                    Debug.Log("Writed all bytes");
                    StartCoroutine(customSongsManager.ReadDownloadedSong(folder + S3SampleFile));
                }
            }
        }
    }
    catch (AmazonS3Exception e)
    {
        // If bucket or object does not exist
        Debug.Log("Error encountered ***. Message:"+ e.Message + " when reading object");
    }
    catch (Exception e)
    {
        Debug.Log("Unknown encountered on server. Message:"+ e.Message + " when reading object");
    }
}

The game crashes in this line: using (GetObjectResponse response = await S3Client.GetObjectAsync(request))

  • Hi,.

    Are you able to perform operations other than S3Get?
    Is the authority okay?
    And share the exception information when you crash.

  • No, I am just doing this get. The game is not crashing anymore, I now have a different problem but I created a new question for it. Thanks!

gefragt vor 2 Jahren283 Aufrufe
2 Antworten
1
Akzeptierte Antwort

This seems to be not AWS SDK specific, but it's rather a Unity specific issue.

By calling Wait on a Task object in your Start method, you essentially block the main thread in Unity. Hence, the Unity engine cannot continue to work properly, which might cause crashes, freezes, and other unexpected behavior. You should never block the main thread in Unity.

Instead of Task.Wait, you could try using a Unity Coroutine to wait for your task to complete. You could also familiarize yourself with the threading and asynchronous programming models in Unity, because they slightly differ from those in .NET.

profile pictureAWS
beantwortet vor 2 Jahren
0

Hello yes, I changed that because it was stopping the main thread. Now I called it from a Coroutine and doesn't stop the game. Thanks.

beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen