Sign In Register

How can we help you today?

Start a new topic
Answered

Uploading binary data using the javascript sdk

Hello,

I am developing admin capabilities for our game using the GameSparks Javascript sdk. A feature we want to include is the ability to upload binary assets, so that they would appear in the "downloadables" section of the portal. I understand that this is possible with the Unity SDK using the "UploadFile" method, but this is not included with the javascript sdk. I attempted to emulate the method using a multipart/form-data post request which included the binary file and the following data parameters: username, password, filename, and shortcode, using the url from GetUploadUrlRequest. This hasn't worked as I imagine there are additional server parameters that the GameSparks backend is expecting.


If needed, I am using AngularJS with the ng-file-upload library.


Is this possible with a regular javascript POST request? Any help would be greatly appreciated.


Thank You



Best Answer
Hey Alex,

You can take a look at our brief tutorial on this [here] and let us know if you get the GetUploadURL request working in your client. If you already have this working, than you are good to go!
We dont have an in-built 'uploadFile' method in our JavaScript SDK at the moment, so you'll have to us a native HTTP POST request. All you need is to put the url you get from the GetUploadURL response (you can see the docs for that here), and use that in your post form.

Hope that helps,
Sean

 


Answer
Hey Alex,

You can take a look at our brief tutorial on this [here] and let us know if you get the GetUploadURL request working in your client. If you already have this working, than you are good to go!
We dont have an in-built 'uploadFile' method in our JavaScript SDK at the moment, so you'll have to us a native HTTP POST request. All you need is to put the url you get from the GetUploadURL response (you can see the docs for that here), and use that in your post form.

Hope that helps,
Sean

 

Thanks Sean, I was definitely over complicating things. It works great now.


Cheers!

When "uploadFile" will be with the javascript sdk ? 

When I try send the multipart/form-data post request to the upload url from GetUploadUrlRequest, it is blocked due to it being a CORS exception. I believe this would need to be configured server-side. Without this, it is impossible to upload binary content using the javascript sdk.


Any ideas?


Thank You 


1 person likes this

im having the same issue!

 Hi everyone,


I've put together a very simple project to illustrate how you can go about binary uploads with the Javascript SDK. You'll need to fill in your apiKey and apiSecret, but essentially it works like this.


1. Press Get the Upload Url. (This will return a short lived URL that the binary can be uploaded to. When this URL is received I use jQuery to update the action on the form to this URL.)

2. Select the binary file.

3. Hit Upload. 


It might be also useful to open the developer console while doing this to watch the debug console throughout the process.


I hope this helps, kind regards,

 - Steve

zip

Retrieval  of the uploaded file gives a CORS issue which others have said renders this pretty much useless using this via the JavaScript SDK


:3000/#/index/troll:1 Access to Image at 'https://gsp-aeu000-binaries.s3.amazonaws.com/upload-313003/5ca50567234642468a476ff5b70461db-pinkcoin-logo.png?AWSAccessKeyId=AKIAJGQ3SFUETOHIK6CQ&Expires=1503264853&Signature=SrrKBWj2jr5omhiLT1WkXbluGLM%3D&gsstage=preview' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.


1 person likes this

Hi Guys,


Did you find the problem with the Amazon throwing the CORS error?


I have the same problem.


Cristi


1 person likes this

The above suggestion by GameSparks worked fine for me. 

Sorry, I meant the problem from "Fraser Kemp" message:


Retrieval  of the uploaded file gives a CORS issue which others have said renders this pretty much useless using this via the JavaScript SDK


:3000/#/index/troll:1 Access to Image at 'https://gsp-aeu000-binaries.s3.amazonaws.com/upload-313003/5ca50567234642468a476ff5b70461db-pinkcoin-logo.png?AWSAccessKeyId=AKIAJGQ3SFUETOHIK6CQ&Expires=1503264853&Signature=SrrKBWj2jr5omhiLT1WkXbluGLM%3D&gsstage=preview' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.


1 person likes this

I'm having the same problem described by Fraser Kemp and Cristian Boghina. CORS errors when I try to get a binary downloadable from my javascript code:


"Failed to load https://gsp-binaries.s3.eu-west-1.amazonaws.com/...: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403."


I assume this is because I'm using $.ajax to make an ajax request for the file. However, the file on S3 has no 'Access-Control-Allow-Origin' header set, so the cross-origin ajax request is always blocked by the browser.


Is there a way to set an 'Access-Control-Allow-Origin' header on binary downloadables, so that I can fetch them from javascript running in a browser?

I actually don't retrieve from Gamesparks any image content now. I upload the data then I download to my own server and reference there so I don't run into CORS issues. Having time specific URLs was not really very optimal either for my site. So my solution was a workaround, sorry this does not help you out


1 person likes this

Thanks for the follow-up Fraser! I opened a support ticket and will update if I hear anything back from GS.


Glad you found a workaround for your case! :)

It slows things down a little in terms of uploading content but I have a separate nodejs server and use the nodejs API - I then run a cleanup every hour with on GS running through the uploaded content ID and delete the files from S3. A collection stores all the site asset urls. It would have been less hassle to be able to get content directly and I think it is an oversight of GS. Obviously applications will not run into the same issues and its just an issue for people like us building applications in browsers. This was flagged up quite a while ago so I'm not sure if they will address it adequately. Hope they look into it further for you 

Login to post a comment