简体   繁体   English

使用Unity C#从Amazon S3下载并保存文件

[英]Download and save file from Amazon S3 with Unity C#

I am following Amazon's tutorial on S3 but I cannot download file and save it to Streaming Resources. 我正在关注Amazon关于S3的教程,但无法下载文件并将其保存到Streaming Resources。 Instead I am downloading file content. 相反,我正在下载文件内容。

ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
{
    string data = null;
    var response = responseObj.Response;
    if (response.ResponseStream != null)
    {
        using (StreamReader reader = new StreamReader(response.ResponseStream))
        {
            data = reader.ReadToEnd();
        }

        ResultText.text += "\n";
        ResultText.text += data;
    }
});

I understand that I should convert the response.ResponseStream into File but I tried many different solutions and I could not make it working. 我知道我应该将response.ResponseStream转换为File,但是我尝试了许多不同的解决方案,但无法使其正常运行。

I understand that I should convert the response.ResponseStream into File but .... 我知道我应该将response.ResponseStream转换为File但....

You do not convert it into a file you read from it and write the content to a file. 您不会将其转换为从中读取并将文件内容写入文件的文件。 There are plenty of built in methods that help with this but the steps are simple. 有很多内置的方法可以帮助解决此问题,但是步骤很简单。 Open/Create a file stream and then write from the response stream to the file stream. 打开/创建文件流,然后从响应流写入文件流。

if (response.ResponseStream != null)
{
    using (var fs = System.IO.File.Create(@"c:\some-folder\file-name.ext"))
    {
        byte[] buffer = new byte[81920];
        int count;
        while ((count = response.ResponseStream.Read(buffer, 0, buffer.Length)) != 0)
            fs.Write(buffer, 0, count);
        fs.Flush();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM