S3 Get object not working properly in Unity
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))
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!
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.
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.
Relevant questions
Access S3 files from Unity for mobile development
asked 3 months agoHow do we unzip a file in S3 bucket using C#.NET
asked 2 months agoGetting InvalidS3ObjectException: Unable to get object metadata from S3
asked a year agoS3 download large files (.Net)
asked 6 months agoGamelift Unity Ubuntu Build Failed
asked 2 years agoAccess denied when trying to GET objects uploaded to s3 bucket via aws sdk using cloudfront
asked 7 months agoFile corrupted after download from S3 using AWS C++ SDK
asked 25 days agoAccess denied when trying to GET objects uploaded to s3 bucket via aws sdk using cloudfront
Accepted Answerasked 7 months agoS3 Get object not working properly in Unity
Accepted Answerasked 2 months agoAWS S3 Get object not working. Getting corrupt file
asked a month ago
Hi,.
Are you able to perform operations other than S3Get?
Is the authority okay?
And share the exception information when you crash.