aws-sdk-net: AWS S3 upload object HttpErrorResponseException

I’m trying to upload my assets to an S3 bucket but AWS keeps getting an HttpErrorResponseException exception. I’m testing the S3Example in the editor with only one difference, instead of using CognitoAWSCredentials I’m using _credentials = new BasicAWSCredentials(awsKey, awsSecretKey);

Is there a way to upload a file to S3 with Unity? Or is it not supported at the time?

Using Unity 5.4.2f & aws-sdk-unity_3.3.14.1 plugin

Stack Trace error:

receieved error Amazon.Runtime.Internal.HttpErrorResponseException: Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.
  at Amazon.Runtime.Internal.UnityWwwRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in <filename unknown>:0 
  at Amazon.S3.AmazonS3Client.ProcessPostResponse (IAsyncResult result) [0x00000] in <filename unknown>:0 
UnityEngine.Debug:Log(Object)
AWSSDK.Examples.S3Example:<PostObject>m__7(AmazonServiceResult`2) (at Assets/Examples/S3Example.cs:175)
Amazon.S3.<>c__DisplayClass4_0:<PostObjectAsync>b__0(AmazonWebServiceRequest, AmazonWebServiceResponse, Exception, AsyncOptions)
Amazon.Runtime.Internal.UnityMainThreadDispatcher:ProcessRequests()
Amazon.Runtime.Internal.UnityMainThreadDispatcher:Update()

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 29 (4 by maintainers)

Most upvoted comments

I managed to upload the file using PutObjectRequest instead of post. I remember one issue with form on unity which I had to use put intead of post to a http request. Maybe is the same thing internally to AWSSDK.Core

` Debug.Log(“Creating request object”); var request = new PutObjectRequest() { BucketName = S3BucketName, Key = Path.GetFileName(path), InputStream = stream, CannedACL = S3CannedACL.Private };

    Debug.Log("Making HTTP post call");
    lastRequestSucceed = false;
    try {
        
        Client.PutObjectAsync(request, (responseObj) => {
            
            if (responseObj.Exception == null) {
                Debug.Log(string.Format("object {0} posted to bucket {1}", responseObj.Request.Key, responseObj.Request.BucketName));
                lastRequestSucceed = true;
            } else {
                Debug.Log("\nException while posting the result object");
                Debug.Log(string.Format("\n receieved error {0}", responseObj.Response.HttpStatusCode.ToString()));
            }
            isLastRequestDone = true;
        });
    }
    catch (Exception ex) {
        Debug.Log("Unhandled Exception while posting the result object");
        Debug.Log(string.Format("\n received error {0}", ex.ToString()));

    }`

I’m also using S3Example.cs from Unity AWSSDK and got HttpErrorResponseException, then after i setting AWSConfigs in Start()

        void Start()
        {
            UnityInitializer.AttachToGameObject(this.gameObject);
            GetBucketListButton.onClick.AddListener(() => { GetBucketList(); });
            PostBucketButton.onClick.AddListener(() => { PostObject(); });
            GetObjectsListButton.onClick.AddListener(() => { GetObjects(); });
            DeleteObjectButton.onClick.AddListener(() => { DeleteObject(); });
            GetObjectButton.onClick.AddListener(() => { GetObject(); });

            AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;

            AWSConfigs.LoggingConfig.LogTo = LoggingOptions.UnityLogger;
            AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.Always;
            AWSConfigs.LoggingConfig.LogMetrics = true;
            AWSConfigs.CorrectForClockSkew = true;
        }

now i can see the problem

Received error response: [<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message>
<RequestId>99EB18D75BE52010</RequestId>
<HostId>gl3VFqE2t4esxMl5vXsVROEPZlM5hoXlSTdl3QdH5lJJW+5UYvMDCIg4pqAOYMa+vlux0qrqonw=</HostId></Error>]

try with Full Access Policy and it’s works

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "FullAccessS3",
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

Hi, try to add region (“Region = _S3Region”) to your request like here:

       request = new PostObjectRequest()
           {
                Bucket = S3BucketName,
                Key = fileName,
                InputStream = stream,
                CannedACL = S3CannedACL.Private,
                Region = _S3Region
        };

The things that I’ve done:

  1. Changed HttpClient
UnityInitializer.AttachToGameObject(this.gameObject); 
AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;
  1. Added AmazonS3FullAccess policy for both Cognito_nameUnauth_Role and Cognito_nameAuth_Role

  2. Added “Region = _S3Region” to my request

And it finally worked 😃

Check out this thread as well: https://github.com/aws/aws-sdk-net/issues/332

GOT IT! “Resource”: “arn:aws:s3:::bucketName/*”

I was missing the /* at the end of the resource path in the access policy. That allowed the Put request, but broke the List Objects request, though, so I guess I have a few things to learn about getting the policies right.

Hi, thank you for sharing that much details, i’ve found this topic after looking for a case with the same log error, I’ve already written as mentioned:

AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;

no improvement so far, the only difference with the script i use and the one posted here is that section:

{ “Version”: “2012-10-17”, “Id”: “AppAccess”, “Statement”: [ { “Sid”: “Stmt1524688564267”, “Effect”: “Allow”, “Principal”: “", “Action”: "”, “Resource”: “arn:aws:s3:::bucketName/*” } ] }

I dont use any arn yet, does that the reason why nothing work ? until now i only use a ARN generated in my datawarehouse for a different purpose which is currently part of a user’s parameter generated in IAM casual way to create a pipe. If i’ve understood between unity and S3 bucket i only need: arn:aws:s3:::Mybucketname, but where should i write this authentification in the script?

Nice! Why don’t you use the visual interface for setting the policies in IAM? So there is no risk of misstyping =)

It’s finding the bucket okay. Pulling down the list of bucket objects works just fine, as does the Cognito authentication call before the request. I tried switching regions for the sake of testing, and only the one I was previously using works for the pull request. Also did a double-check of the path+filename – File is being found, and has no unfriendly chars in its name or path.

The code is essentially just the code from the S3Example script provided by AWS (with extra debug logs):

private void PutObject(string fileName)
{
    var stream = new FileStream(Application.persistentDataPath + Path.DirectorySeparatorChar + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);  
    var putRequest = new PutObjectRequest()
    {
        BucketName  = S3BucketName,
        Key         = fileName,
        InputStream = stream,
        CannedACL   = S3CannedACL.Private
    };
    ResultText.text += "\nMaking HTTP put call";
    PutObjectAsync(putRequest);
}

private void PutObjectAsync(PutObjectRequest objRequest)
{
    Client.PutObjectAsync(objRequest, (responseObj) =>
    {
        if( responseObj.Exception == null ) {
            ResultText.text += string.Format("\nobject {0} posted to bucket {1}", responseObj.Request.Key, responseObj.Request.BucketName);
        } else {
            ResultText.text += "\nException while posting the result object";
            if( responseObj != null ) {
                if( responseObj.Response != null ) {
                    ResultText.text += string.Format("\n receieved error {0}", responseObj.Response.HttpStatusCode.ToString());
                } else {
                    if( responseObj.Exception.InnerException != null ) {
                        Debug.LogError("responseObj.Response NULL\n" + responseObj.Exception.InnerException);
                    } else {
                        Debug.LogError("responseObj.Response.InnerException NULL\n" + responseObj.Exception);
                    }
                }
            } else {
                Debug.LogError("responseObj NULL");
            }
        }
    });
}

I have also tried changing the CannedACL to PublicReadWrite as well as NoACL. No luck there, either.