2017-11-18 156 views
0

我正嘗試使用azure blob觸發器將blob流反序列化爲JSON對象。每當我將視頻上傳到Blob存儲時,都會觸發此觸發器。但是,它是引發此錯誤:將Azure Blob流反序列化爲Json對象

Newtonsoft.Json: Unexpected character encountered while parsing value: . Path ''.

這是我使用的反序列化的代碼:

public static void Run(Stream myBlob, string name, TraceWriter log) 
{ 
    myBlob.Position = 0; //resetting stream's position to 0 
    var serializer = new JsonSerializer(); 
    using(var sr = new StreamReader(myBlob)) 
    { 
     using(var jsonTextReader = new JsonTextReader(sr)) 
     { 
      BlobData blobData = serializer.Deserialize<BlobData>(jsonTextReader); 
     } 
    } 

    public class BlobData 
    { 
     public string path { get; set; } 
    } 
} 

任何幫助將是appreciated.Thanks。

+0

你可以編輯你的問題,幷包括blob的實際內容? –

+0

我之前提到過,blob將包含一個視頻,上傳後會觸發一個觸發器。截至目前,我正在使用一些示例視頻。 – Vin

+1

當blob是一個視頻,它如何被反序列化爲一個JSON對象? –

回答

0

A i mentioned earlier, the blob will contain a video and after upload a trigger will fire. As of now, i am using some sample videos

由於Gaurav Mantri評論說你無法將視頻反序列化爲JSON對象。根據我的理解,如果您想在上傳視頻blob後檢索blob Uri,並將視頻Blob網址存儲到某個其他數據存儲區。在這一點上,你可以綁定CloudBlockBlob類型,請myBlob參數,可以按如下方式檢索BLOB網址:

run.csx

#r "Microsoft.WindowsAzure.Storage" 

using Microsoft.WindowsAzure.Storage.Blob; 

public static void Run(CloudBlockBlob myBlob, string name, TraceWriter log) 
{ 
    //blob has public read access permission 
    var blobData = new BlobData() { path = myBlob.Uri.ToString() }; 

    //blob is private, generate a SAS token for this blob with the limited permission(s) 
    var blobSasToken=myBlob.GetSharedAccessSignature(new SharedAccessBlobPolicy() 
      { 
       SharedAccessExpiryTime =DateTimeOffset.UtcNow.AddDays(2), 
       Permissions = SharedAccessBlobPermissions.Read 
      })); 
    var blobData = new BlobData() 
      { 
      path = $"{myBlob.Uri.ToString()}{blobSasToken}" 
      }; 
    //TODO: 
} 

此外,你可以遵循Create and use a SAS with Blob storageAzure Functions Blob storage bindingsGet started with Azure Blob storage using .NET瞭解更多詳細代碼示例。