【以下的问题经过翻译处理】 我正在使用AWS SDK .NET for Unity从S3下载zip文件。我刚按照此教程为.NET,实现了获取方法 https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html
但是当我用ReadObjectDataAsync().Wait()调用该方法时,Unity会停止和崩溃,就像在无限循环中一样。这是我的代码,具有不同的名称,但基本相同:
/// <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");
}
}
游戏在此行崩溃:
using (GetObjectResponse response