简体   繁体   中英

Uploading blob to Azure with Unity

I am trying to upload data to a blob service on Azure portal.

I have been trying to use this page: https://msdn.microsoft.com/library/azure/dd179451.aspx

The code I have is the following:

IEnumerator SetItem ()
{
    DataJson data = new DataJson("Amy", "201289");
    string json = JsonUtility.ToJson(data);
    UnityWebRequest newWWW = UnityWebRequest.Put(
        "https://compstorage.blob.core.windows.net/folderName/item", json);
    yield return newWWW.Send();
    if (newWWW.isError == false)
    {
        Debug.Log("Form upload complete!");
    }
    UnityWebRequest www = UnityWebRequest.Get("compstorage.blob.core.windows.net/folderName/item.json");
    yield return www.Send();
    string text = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
    DataJson newData = JsonUtility.FromJson<DataJson>(text);
    Debug.Log(newData.ToString()); 
}

The blob is set as Blob meaning read, write permission. I tried all kind of variations like adding/removing file extension, also adding the SaS key but it never updates the server content despite the Put request claiming to be successful. The Get request does work fine but prints old version of data, not the updated one.

Does anyone know the way to use the Put request in Unity

Can't tell why this is happening but you can use this plugin to communicate with your Azure service. It will solve your problem.

EDIT :

After reading the MS link you posted, the problem is that you are missing several headers:

  • Authorization
  • Date or x-ms-date
  • x-ms-version
  • Content-Length
  • x-ms-blob-type

Please understand that there types of blob. You did not mention the type of blob request you are making. Please see that link again to see which extra headers are needed for each blob. If it says "Required" in the description then you must include it in the request. You don't need it if it says "Optional".

How do you include headers with UnityWebRequest ?

UnityWebRequest newWWW = UnityWebRequest.Put("https://compstorage.blob.core.windows.net/folderName/item", json);
newWWW.SetRequestHeader("Authorization","your Authorization");
newWWW.SetRequestHeader("x-ms-version","v1");
newWWW.SetRequestHeader("x-ms-blob-type","BlockBlob");
newWWW.SetRequestHeader("x-ms-date","Your Datte");

Don't worry about the Content-Length and Date headers as Unity will automatically generate them for you. You are not allowed to set them manually with UnityWebRequest anyways. Look here for what to put for the authorization header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM