在Unity中的Amazon S3存储桶之间传输文件

时间:2017-02-11 00:33:00

标签: amazon-web-services unity3d amazon-s3 aws-sdk

我试图在Unity内部的S3存储桶之间传输文件,并遇到我无法找到文档的神秘错误。两个存储桶都属于同一个帐户。

每当我创建资产时,我都会将其上传到Dev Bucket中的S3服务器,这非常有用。当我准备提交资产时,我想查看Prod Bucket中缺少的资产列表,并将它们从Dev Bucket转移过来。根据我的研究,IAmazonS3. CopyObjectAsync()是应该完成这项任务的功能。亚马逊的Unity SDK中IAmazonS3.CopyObject() function

这是我在尝试复制对象时调用的代码:

public void TestCopy()
{
    var request = new CopyObjectRequest()
    {
        SourceBucket = mLoginData.DevBucket,
        SourceKey =  "myPic.jpg",
        DestinationBucket = mLoginData.ProdBucket,
        DestinationKey = "myPic.jpg"
    };

    AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;

    Client.CopyObjectAsync(request,(responseObj) =>
    {
        if (responseObj.Exception == null)
        {
            ResultText.text += "Copied Object";
        }
        else
        {
            ResultText.text += "Got Exception: \n" + responseObj.Exception.ToString();
        }
    });
}

这会导致"移动" Amazon Error Code documentation中未提及的错误代码:

Got Exception: 
Amazon.S3.AmazonS3Exception: Error making request with Error Code Moved and Http Status Code Moved. No further error information was returned by the service. Response Body: Encountered invalid redirect (missing Location header?) ---> Amazon.Runtime.Internal.HttpErrorResponseException: Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.
at Amazon.Runtime.Internal.UnityRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
at Amazon.Runtime.Internal.HttpHandler`1[System.String].GetResponseCallbackHelper (System.Object state) [0x00000] in <filename unknown>:0 
--- End of inner exception stack trace ---
at Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleException (IExecutionContext executionContext, Amazon.Runtime.Internal.HttpErrorResponseException exception) [0x00000] in <filename unknown>:0 
at Amazon.Runtime.Internal.ExceptionHandler`1[T].Handle (IExecutionContext executionContext, System.Exception exception) [0x00000] in <filename unknown>:0 
at Amazon.Runtime.Internal.ErrorHandler.ProcessException (IExecutionContext executionContext, System.Exception exception) [0x00000] in <filename unknown>:0 
at Amazon.Runtime.Internal.ErrorHandler.InvokeAsyncCallback (IAsyncExecutionContext executionContext) [0x00000] in <filename unknown>:0 

我知道我设置正确,因为我能够查看/列出存储桶,上传/列出/删除文件到两个存储桶等等。解决方案执行的关键是至关重要的Unity编辑器。

2 个答案:

答案 0 :(得分:0)

基于直接使用S3 API的丰富经验,我有理由相信这是您的错误:

  

PermanentRedirect

     

您尝试访问的存储区必须使用指定的端点进行寻址。将所有将来的请求发送到此端点。

     

301 Moved Permanently

我相信底层库会隐藏这个,Moved错误是该库对301错误的解释。为什么它会这样做没有任何意义,但那又是因为我更喜欢直接使用S3 REST API。

Encountered invalid redirect (missing Location header?)

这与记录的行为一致。

  

为了帮助您在开发过程中发现这些错误,此类型的重定向不包含允许您自动将请求发送到正确位置的Location HTTP标头。请使用正确的Amazon S3端点查阅生成的XML错误文档以获取帮助。

     

http://docs.aws.amazon.com/AmazonS3/latest/dev/Redirects.html

如果有办法开始使用该XML响应机构,那么它可能会有所帮助。

如果您使用全局(非特定区域)端点访问存储桶并且存储桶位于us-east-1以外的区域,则此错误在存储桶创建的前几分钟内很常见,这就是为什么我问桶是否是新的。

如果存储桶不在us-east-1中,则可能需要在代码中的某个位置传递区域,因为上面的错误表明您的请求到达了错误的区域端点。

答案 1 :(得分:0)

事实证明,CopyObjectAsync需要使用配置了目标区域而不是源区域的IAmazonS3客户端实例。改变这个允许副本就好了。