question

contact@gentlymad.org avatar image
contact@gentlymad.org asked

Unity: Upload Content with GetContentUploadUrl results in "Forbidden 403" error

I am trying to upload content to PlayFab from the unity editor.
Fetching the upload url works just fine.
However using that url to actually upload something is not working for me. I guess i am missing something in the process.

I first tried using Unity's WWW class to upload content like this:

WWWForm form = new WWWForm();
//Set data to upload
form.AddBinaryData("data", data, "", mime);
//Add PUT header.
form.headers.Add( "X-HTTP-Method-Override", "PUT" );

//Start upload
WWW www = new WWW(url, form);

This gives me a 403 error.



I then tried using a WebClient like this:

//Ignore SSL certificates so the WebClient can send data to target url
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(
delegate
{ return true; }
);

System.Net.WebClient client = new System.Net.WebClient();
//client.UploadData(new System.Uri(url), "PUT", data);
client.UploadFile(new Uri(url), "PUT", filePath);

Again, i get a 403 error.


I had a look at this thread
https://community.playfab.com/hc/en-us/community/posts/205469488-How-to-upload-files-to-PlayFab-s-Content-Service?input_string=Unity%3A%20Upload%20Content%20with
which mentions an attached file including an example for uploading content with unity. But i can't find an attachment.

Any ideas what i am doing wrong during the upload process?

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

The issue is around WWW itself. The example mentioned in the other thread (I've re-added the code for it) shows using HttpWebRequest to accomplish this. Here's the relevant portion of the code:

 

 public void PutFile(string putURL, byte[] payload)
{
var request = (HttpWebRequest)WebRequest.Create(putURL);
request.Method = "PUT";
request.ContentType = this.mimeType;

if (payload != null)
{
Stream dataStream = request.GetRequestStream();
dataStream.Write(payload, 0, payload.Length);
dataStream.Close();
}
else
{
Debug.LogWarning(string.Format("ERROR: Byte arrry was empty or null"));
return;
}

Debug.Log("Starting HTTP PUT...");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if(response.StatusCode == HttpStatusCode.OK)
{
Debug.Log("...HTTP PUT Successful");
this.isImageUploaded = true;
}
else
{
Debug.LogWarning(string.Format("ERROR: [{0}] -- {1}", response.StatusCode, response.StatusDescription));
}
}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

contact@gentlymad.org avatar image
contact@gentlymad.org answered

Thanks, using your code it is working now :)

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.