Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Size Mismatch error?

Discussion in 'Unity Cloud Content Delivery' started by cabbyU, Nov 2, 2020.

  1. cabbyU

    cabbyU

    Joined:
    Sep 21, 2016
    Posts:
    18
    I'm trying to write a C# script that will upload some asset bundles to a bucket and create a release. I tried to generate a C# client sdk, but it had compiler errors, and even after I fixed them the upload still didn't work so I went with writing my own code from scratch. I got it mostly working, but I'm having an issue uploading certain files, which return error code 406 with the response: {"code":1,"details":["Size Mismatch"],"reason":"Invalid argument"}. The error is only occurring on 1 out of 21 files. I'm not sure if it's relevant, but this particular file is the manifest file for the collection of asset bundles I'm uploading. I checked that the length of the byte array matches the "content_size" value I used when I created the entry, so I'm not sure why I'm getting this error. Does anyone have any ideas? Thanks in advance. Here is my upload function:

    Code (CSharp):
    1.  
    2. private static bool UploadFile(string entryID, string bucketID, string localFilePath) {
    3.         Debug.Log($"Uploading {localFilePath}");
    4.         try {
    5.             string fileName = Path.GetFileName(localFilePath);
    6.  
    7.             List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    8.  
    9.             formData.Add(new MultipartFormDataSection("file", localFilePath));
    10.             formData.Add(new MultipartFormFileSection(fileName, File.ReadAllBytes(localFilePath)));
    11.  
    12.             string url = $"{BASE_URL}buckets/{bucketID}/entries/{entryID}/content/";
    13.  
    14.             using (var webRequest = UnityWebRequest.Post(url, formData)) {
    15.                 webRequest.method = "PATCH";
    16.                 webRequest.SetRequestHeader("Accept", "application/json");
    17.                 webRequest.SetRequestHeader("Authorization", $"Basic {AUTHORIZATION}");
    18.                 webRequest.SendWebRequest();
    19.  
    20.                 while (!webRequest.isDone) {
    21.                     //wait
    22.                 }
    23.  
    24.                 Debug.Log("Upload response code: " + webRequest.responseCode);
    25.                 Debug.Log(webRequest.downloadHandler.text);
    26.  
    27.                 return webRequest.responseCode == 204;
    28.             }
    29.         }catch(Exception e) {
    30.             Debug.LogError(e.ToString());
    31.             return false;
    32.         }
    33. }
    34.  
    P.S. I realize that I could use curl or the CLI tool. I tried uploading the offending file via a curl command, and that did work, but I'd rather use C#.
     
  2. timtunity3d

    timtunity3d

    Unity Technologies

    Joined:
    Oct 1, 2015
    Posts:
    130
    Just wanted to let you know I'm looking into this. I can reproduce this if my text file ends with a blank line. Does that match what you're seeing?

    I will say that you can upload the message body directly and skip the multipart form encoding. The content type for that needs to be set to 'application/offset+octet-stream'
     
  3. cabbyU

    cabbyU

    Joined:
    Sep 21, 2016
    Posts:
    18
    It's not a text file. It's binary. Specifically, it's the manifest file for a set of android asset bundles (see attached upload). I will try to do the upload without using a multipart form. I did not know I could do that.
     

    Attached Files:

  4. cabbyU

    cabbyU

    Joined:
    Sep 21, 2016
    Posts:
    18
    I can't believe I've been stuck on this issue for 3 days and the solution ended up being so simple. As suggested, simply not using a multipart form worked for me. This is the code I ended up using:

    Code (CSharp):
    1.  
    2. private static bool UploadFile(string entryID, string bucketID, string localFilePath) {
    3.         Debug.Log($"Uploading {localFilePath}");
    4.        
    5.         try {
    6.             string url = $"{BASE_URL}buckets/{bucketID}/entries/{entryID}/content/";
    7.             using (var webRequest = new UnityWebRequest(url, "PATCH")) {
    8.  
    9.                 webRequest.SetRequestHeader("Accept", "application/json");
    10.                 webRequest.SetRequestHeader("Authorization", $"Basic {AUTHORIZATION}");
    11.                 webRequest.SetRequestHeader("Content-Type", "application/offset+octet-stream");
    12.                 webRequest.uploadHandler = new UploadHandlerRaw(File.ReadAllBytes(localFilePath));
    13.                 webRequest.downloadHandler = new DownloadHandlerBuffer();
    14.  
    15.                 webRequest.SendWebRequest();
    16.  
    17.                 while (!webRequest.isDone) {
    18.                     //wait
    19.                 }
    20.  
    21.                 Debug.Log("Upload response code: " + webRequest.responseCode);
    22.                 Debug.Log(webRequest.downloadHandler.text);
    23.  
    24.                 return webRequest.responseCode == 204;
    25.             }
    26.         } catch (Exception e) {
    27.             Debug.LogError(e.ToString());
    28.             return false;
    29.         }
    30.     }
    31.  
     
  5. timtunity3d

    timtunity3d

    Unity Technologies

    Joined:
    Oct 1, 2015
    Posts:
    130
    That's great to hear, and thanks for posting the file! We'll post here when the bug in the first code is fixed as well just for future reference.