Azure Blob - 调用UploadFile()时的ArgumentNullException

时间:2012-10-22 18:18:48

标签: azure azure-storage azure-storage-blobs

尝试使用以下代码上传文件时出现以下异常:

            string encodedUrl = "videos/Sample.mp4"
            CloudBlockBlob encodedVideoBlob = blobClient.GetBlockBlobReference(encodedUrl);
            Log(string.Format("Got blob reference for {0}", encodedUrl), EventLogEntryType.Information);
            encodedVideoBlob.Properties.ContentType = contentType;
            encodedVideoBlob.Metadata[BlobProperty.Description] = description;
            encodedVideoBlob.UploadFile(localEncodedBlobPath);

我看到“获得blob引用”消息,因此我假设引用正确解析。

Void Run() C:\Inter\Projects\PoC\WorkerRole\WorkerRole.cs (40)
System.ArgumentNullException: Value cannot be null.
Parameter name: value
   at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result()
   at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.ExecuteAndWait()
   at Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFromStream(Stream source, BlobRequestOptions options)
   at Microsoft.WindowsAzure.StorageClient.CloudBlob.UploadFile(String fileName, BlobRequestOptions options)
   at EncoderWorkerRole.WorkerRole.ProcessJobOutput(IJob job, String videoBlobToEncodeUrl) in C:\Inter\Projects\PoC\WorkerRole\WorkerRole.cs:line 144
   at EncoderWorkerRole.WorkerRole.Run() in C:\Inter\Projects\PoC\WorkerRole\WorkerRole.cs:line 40

有趣的是,我正在从本地服务器运行相同的代码段,即在Azure之外,它运行正常。

欢迎提示,谢谢!

1 个答案:

答案 0 :(得分:7)

找到了罪魁祸首!

基本上,问题出在这一行

encodedVideoBlob.Metadata [BlobProperty.Description] = description;

有一个检查井,确保没有设置空元数据或空元数据。通过从Reflector中反汇编并搜索ArgumentNullException ocurrences找到它。

可以很好地捕获并在堆栈中重新抛出更有意义的异常: - )

以下是有趣的方法:

internal static void AddMetadata(HttpWebRequest request, NameValueCollection metadata)
{
    if (metadata != null)
    {
        foreach (string str in metadata.AllKeys)
        {
            AddMetadata(request, str, metadata[str]);
        }
    }
}

internal static void AddMetadata(HttpWebRequest request, string name, string value)
{
    CommonUtils.AssertNotNullOrEmpty("value", value);
    request.Headers.Add("x-ms-meta-" + name, value);
}


internal static void AssertNotNullOrEmpty(string paramName, string value)
{
    AssertNotNull(paramName, value);
    if (string.IsNullOrEmpty(value))
    {
        throw new ArgumentException(Microsoft.WindowsAzure.SR.ArgumentEmptyError, paramName);
    }
}

internal static void AssertNotNull(string paramName, object value)
{
    if (value == null)
    {
        throw new ArgumentNullException(paramName);
    }
}