Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity : S3Example GetObjects not working. User-Agent header issue #643

Closed
kashifshabbir opened this issue May 8, 2017 · 31 comments
Closed

Comments

@kashifshabbir
Copy link

kashifshabbir commented May 8, 2017

I downloaded latest unity SDK "aws-sdk-unity_3.3.83.0.zip" and installed S3 unitypackage.
but in GetObjects function I am getting error. Following is trace of the problem

ArgumentException: Cannot set Request Header User-Agent - name contains illegal characters or is not user-overridable
UnityEngine.Networking.UnityWebRequest.SetRequestHeader (System.String name, System.String value) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/UnityWebRequest/WebRequestBindings.gen.cs:473)
UnityEngine.WWW..ctor (System.String url, System.Byte[] postData, System.Collections.Generic.Dictionary`2 headers) (at /Users/builduser/buildslave/unity/build/Runtime/WebRequestWWW/UWRWWW.cs:60)
Amazon.Runtime.Internal.UnityMainThreadDispatcher+<InvokeRequest>d__7.MoveNext ()
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Amazon.Runtime.Internal.UnityMainThreadDispatcher:ProcessRequests()
Amazon.Runtime.Internal.UnityMainThreadDispatcher:Update()

Expected Behavior
It used to work seamlessly in Unity version 5.6

Steps to Reproduce
Simply running example scene can reproduce issue

Your Environment
I am currently using Unity 2017.1.0b2 and building for MAC platform.
.NET version in Unity build settings: 3.5

@sstevenkang
Copy link
Contributor

Hi, could you provide a sample code that repros this problem?

@kashifshabbir
Copy link
Author

kashifshabbir commented May 9, 2017

I downloaded the Unity SDK from here http://sdk-for-net.amazonwebservices.com/latest/aws-sdk-unity.zip

Here is function I am using from S3Example.cs

public void GetObjects(Action callback)
   {
       var request = new ListObjectsRequest()
       {
           BucketName = S3BucketName
       };
  
       Client.ListObjectsAsync(request, (responseObject) =>
           {
               if (responseObject.Exception == null)
               {
                   responseObject.Response.S3Objects.ForEach((o) =>
                       {
                           s3Objects.Add(o);
                       });
                   callback();
                   
               }
               else
               {
                   Debug.Log("GetObjects got exceptional :D");
               }
           });
   }

Error is in following line

        Client.ListObjectsAsync(request, (responseObject) =>

You can see exception stack trace in question above
Thanks for taking time!

@schakkis
Copy link

schakkis commented May 10, 2017

I'm experiencing a similar issue with Cognito Identity and Unity 2017.1.0b4 on MacOS.

Unity made some additional changes to UnityWebRequest in 2017.1.0b3/b4.

Web: It is now allowed to override User-Agent header in UnityWebRequest
Unity Beta Release Notes

I've tried with both MobileAnalytics and the S3 sample (in a new empty project). After adding Ids/regions to the sample script and starting the project I get the following error:

InvalidOperationException: Cannot override system-specified headers
UnityEngine.Networking.UnityWebRequest.SetRequestHeader (System.String name, System.String value) (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/modules/UnityWebRequest/WebRequestBindings.gen.cs:471)
UnityEngine.WWW..ctor (System.String url, System.Byte[] postData, System.Collections.Generic.Dictionary`2 headers) (at /Users/builduser/buildslave/unity/build/Runtime/WebRequestWWW/UWRWWW.cs:62)
Amazon.Runtime.Internal.UnityMainThreadDispatcher+<InvokeRequest>d__7.MoveNext ()
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Amazon.Runtime.Internal.UnityMainThreadDispatcher:ProcessRequests()
Amazon.Runtime.Internal.UnityMainThreadDispatcher:Update()

Looks like the _credentials = new CognitoAWSCredentials(IdentityPoolId, _CognitoIdentityRegion); is where it breaks.. My wrong, error is in the actual S3 request, so same place as was reported earlier.

UnityPackage:

  • AWSSDK.S3.3.3.6.0.unitypackage

@PavelSafronov
Copy link

@schakkis, it sounds like the problem you are seeing is different, can you please open a new issue?

@schakkis
Copy link

@PavelSafronov
Sorry, was a bit too fast with an earlier assumption. I think it's still the same issue, but the error that is presented is just different in the current Unity beta.

Should I still report a new issue, or is it okay to keep it here?

@PavelSafronov
Copy link

Ah, I see what you mean. OK, this seems like the appropriate issue then.

Can you try attaching the below callback to find out what the problematic header is?

client.BeforeRequestEvent += (sender, args) =>
{
    var wsrea = args as WebServiceRequestEventArgs;
    foreach(var header in wsrea.Headers)
    {
        Console.WriteLine("{0} = [{1}]", header.Key, header.Value);
    }
};

@schakkis
Copy link

schakkis commented May 10, 2017

Just ran a quick test; here is the log output:

User-Agent = [aws-sdk-unity/3.3.6.0 aws-sdk-core/3.3.13.4 .NET_Runtime/2.0 UnityVersion/2017.1.0b4 OS/unity__ ClientAsync UnityWWW]
UnityEngine.Debug:Log(Object)
AWSSDK.Examples.S3Example:<GetObjects>m__9(Object, RequestEventArgs) (at Assets/Examples/S3Example.cs:210)
Amazon.Runtime.AmazonServiceClient:ProcessRequestHandlers(IExecutionContext)
Amazon.Runtime.Internal.CallbackHandler:RaiseOnPreInvoke(IExecutionContext)
Amazon.Runtime.Internal.CallbackHandler:PreInvoke(IExecutionContext)
Amazon.Runtime.Internal.CallbackHandler:InvokeAsync(IAsyncExecutionContext)
...
Amazon.Runtime.Internal.PipelineHandler:InvokeAsync(IAsyncExecutionContext)
Amazon.Runtime.Internal.ThreadPoolExecutionHandler:InvokeAsyncHelper(IAsyncExecutionContext)
Amazon.Runtime.Internal.Util.ThreadPoolThrottler`1:Callback(Object)

@kashifshabbir
Copy link
Author

kashifshabbir commented May 11, 2017

I couldn't find BeforeRequestEvent in IAmazonS3 client;. Am I missing something?

@schakkis
Copy link

@kashifshabbir
It's available in AmazonServiceClient. For a quick test it's easiest to just cast to that.

@kashifshabbir
Copy link
Author

There you go!
User-Agent = aws-sdk-unity/3.3.5.9 aws-sdk-core/3.3.10.0 .NET_Runtime/2.0 UnityVersion/2017.1.0b2 OS/unity__ ClientAsync UnityWWW
Like @schakkis I am getting similar header and stack trace.

@schakkis
Copy link

I did a little more investigation. It looks like AWSSDK doesn't properly detect that it should be using UnityWebRequest in Unity 2017.1.0bX.

If I manually set the client it seems to be working fine.
Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;

@kashifshabbir
Copy link
Author

Thanks a lot. It is fine with this workaround now!

@schakkis
Copy link

schakkis commented Jul 3, 2017

Short update;

We started looking into Unity 2017.1 now again since they are closing in on release. Testing with Unity 2017.1.0f1 this error still occurs.

We have also discovered a related problem; Unity has changed more things in UnityWebRequest and the reflection code in AWS SDK (UnityWebRequestWrapper.cs) doesn't work anymore.

See https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Core/Amazon.Runtime/Pipeline/_unity/UnityWebRequestWrapper.cs#L56
and https://docs.unity3d.com/2017.1/Documentation/ScriptReference/Networking.UnityWebRequest.html

isError seems to have been replaced with isHttpError and isNetworkError.

@kashifshabbir
Copy link
Author

Thanks for update.

This workaround Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest; worked fine for all platforms except windows build.

I am using unity 2017.1.0b2 on macOS Sierra.

I am getting following error in Windows Platform Build

NullReferenceException: Object reference not set to an instance of an object
  at Amazon.Runtime.Internal.UnityWebRequestWrapper..cctor () [0x00000] in <filename unknown>:0 
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Amazon.Runtime.Internal.UnityWebRequestWrapper
  at Amazon.AWSConfigs.set_HttpClient (HttpClientOption value) [0x00000] in <filename unknown>:0 

For now I am waiting for unity 2017.1 to come out of beta.

@Niknokc
Copy link

Niknokc commented Jul 20, 2017

@kashifshabbir
Hi. 2017.1 is out of beta now, do you still have this issue?
Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;
Works just fine on OSX for me, but throws same errors you listed in your last update.
Any tips?

@tbc01
Copy link

tbc01 commented Jul 22, 2017

Having same issue as @Niknokc. Works on OSX not on Android build.

@schakkis
Copy link

This has also been reported in #696, #701 & #704

We have used the same approach as @lakrsv described in #701 and rolled our own dll.

@dkossnick-figma
Copy link

Any chance this will be fixed in the official AWSSDK release for Unity soon? I was hoping #709 might fix it, but this issue still appears to be active for 2017.1. We could go the rolling our own dll approach, but would prefer that the official release supports 2017.1 out of the box. Thanks!

@Yukichu
Copy link

Yukichu commented Oct 23, 2017

Still no update on this? Unity is on 2017.2 now, been out for a while. Why is SDK still broken?

@chenchen2015
Copy link

chenchen2015 commented Nov 9, 2017

Apparently, pull request #709 still did not totally fix the same issue. I'm still receiving the bug message below in Unity 2017.1 versions (specifically, 2017.1.2p2)

InvalidOperationException: Cannot override system-specified headers
UnityEngine.Networking.UnityWebRequest.SetRequestHeader (System.String name, System.String value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/UnityWebRequest/WebRequestBindings.gen.cs:482)
UnityEngine.WWW..ctor (System.String url, System.Byte[] postData, System.Collections.Generic.Dictionary`2 headers) (at C:/buildslave/unity/build/Runtime/WebRequestWWW/UWRWWW.cs:62)
Amazon.Runtime.Internal.UnityMainThreadDispatcher+<InvokeRequest>d__7.MoveNext ()
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Amazon.Runtime.Internal.UnityMainThreadDispatcher:ProcessRequests()
Amazon.Runtime.Internal.UnityMainThreadDispatcher:Update()

Here's a temporary fix for Unity 2017.1 versions. Presumably, this should also work in Unity 2017.2 versions if you have the same bug, but I haven't tested. @schakkis was right, all you need is to manually override the target HttpClient interface with UnityWebRequest. To do this, the best way I have found is to add it right after UnityMainThreadDispatcher awakes.

// UnityMainThreadDispatcher.cs
public void Awake()
{
      _logger = Logger.GetLogger(this.GetType());
      // Call the method to process requests at a regular interval.
      _nextUpdateTime = Time.unscaledTime;
      _nextUpdateTime += _updateInterval;

      // Override HttpClient here
     Amazon.AWSConfigs.HttpClient = Amazon.AWSConfigs.HttpClientOption.UnityWebRequest;
}

As you can see, simply override the HttpClient as suggested by @schakkis in the Awake() method in UnityMainThreadDispatcher.cs code, and compile again. Now everything should be back working again!

@mosky17
Copy link

mosky17 commented Nov 20, 2017

setting the client manually is working for OSX but not for iOS:

NullReferenceException: A null value was found where an object instance was required.
  at Amazon.Runtime.Internal.UnityWebRequestWrapper..ctor (System.String url, System.String method) [0x00000] in <filename unknown>:0 
  at Amazon.AWSConfigs.set_HttpClient (HttpClientOption value) [0x00000] in <filename unknown>:0 
  at AmazonController.Start () [0x00000] in <filename unknown>:0 
Rethrow as TypeInitializationException: The type initializer for 'Amazon.Runtime.Internal.UnityWebRequestWrapper' threw an exception.
  at Amazon.AWSConfigs.set_HttpClient (HttpClientOption value) [0x00000] in <filename unknown>:0 
  at AmazonController.Start () [0x00000] in <filename unknown>:0 

Any solution?

@mutablealligator
Copy link

Hi @mosky17,

For iOS, do you have a link.xml file that contains the content as described in https://github.com/aws/aws-sdk-net/blob/master/Unity.README.md#unity-sdk-fundamentals?

@mosky17
Copy link

mosky17 commented Dec 5, 2017

thanks for your response @kvasukib , my issue was resolved after updating the cognito library, seems it was out of date.

@mutablealligator
Copy link

@chenchen2015
Copy link

Thank you @kvasukib I was hoping that AWS could solve this issue.

@mutablealligator
Copy link

Hi @neosatus, Can you follow this guide https://aws.amazon.com/blogs/mobile/unity-v3-support-in-the-aws-sdk-for-net-is-out-of-preview/ and set the HttpClient to UnityWebRequest manually in the app?

@almorak
Copy link

almorak commented Mar 8, 2018

i encountered similar problem, with sns package (3.3.0.25)
I have added AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;
it solved The Exception BUT still no response, return or exception with SnsClient.CreatePlatformEndpointAsync

Need some advice.
(that part of code just same as example )
Im using unity 2017.1.0.3.

@klaytaybai
Copy link
Contributor

Hi @almorak, please open a new issue if you are still having issues with the SnsClient. It will be much easier for you, other customers, and AWS to follow if your problem is on its own issue.

@oismaelash
Copy link

You can now, use sdk of S3 modified and better: https://github.com/IsmaelNascimento/aws-sdk-s3-for-unity

@dbambulaks
Copy link

Error is in following line

        Client.ListObjectsAsync(request, (responseObject) =>

You can see exception stack trace in question above
Thanks for taking time!

In my case the same line is not throwing any exceptions nor callback is called... Any idea what to do? This happens only on initial ios app installation, if restart the app - works fine.

@github-actions
Copy link

We have noticed this issue has not recieved attention in a year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Sep 12, 2020
@github-actions github-actions bot added closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Sep 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests