How to check whether Azure Blob Storage upload was successful?

≡放荡痞女 提交于 2020-07-09 05:15:11

问题


I'm using an Azure SAS URL to upload a file to a blob storage:

var blockBlob = new Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob(new System.Uri(sasUrl));
blockBlob.UploadFromFile(filePath);

The file exists on my disk, and the URL should be correct since it is automatically retrieved from the Windows Store Ingestion API (and, if I slightly change one character in the URL's signature part, the upload fails with HTTP 403).

However, when checking

var blobs = blockBlob.Container.ListBlobs();

the result is Count = 0, so I'm wondering if the upload was successful? Unfortunately, the UploadFromFile method (similarly to the UploadFromStream method) has no return type, so I'm not sure how to retrieve the upload's result).

If I try to connect to the SAS URL using Azure Storage Explorer, listing blob containers fails with the error "Authentication Error. Signature fields not well formed". I tried URL escaping the URL's signature part since that seems to be the reason for that error in some similar cases, but that doesn't solve the problem.

Is there any way to check the status of a blob upload? Has anybody an idea why an auto-generated URL (delivered by one of Microsoft's official APIs) can not be connected to using Azure Explorer?


回答1:


Please examine the sp field of your SAS. It shows the rights you are authorized to do with the blob. For example, sp=rw means you could read the blob and write content to the blob using this SAS. sp=w means you can only write content to the blob using this SAS.

If you have the read right, you can copy the SAS URL to the browser address bar. The browser will download or show the blob content for you.

Is there any way to check the status of a blob upload?

If no exception throws from your code, it means the blob has been uploaded successfully. Otherwise, a exception will be thrown.

try
{
    blockBlob.UploadFromFile(filePath);
}
catch(Exception ex)
{
    //uploaded failed
}

You can also confirm it using any web debugging proxy tool(ex. Fiddler) to capture the response message from storage server. 201 Created status code will be returned if the blob has been uploaded successfully.

Has anybody an idea why an auto-generated URL (delivered by one of Microsoft's official APIs) can not be connected to using Azure Explorer?

Azure Storage Explorer only allow us to connect a Storage Account using SAS or attach a storage service (blob container, queue, or table) using an SAS. It doesn't allow us to connect a blob item using SAS.




回答2:


In case on synchronous UPLOAD, we can try Exception based approach and also we can cross check "blockBlob.Properties.Length". Before uploading file, its "-1" and after upload completes, it become size of file got uploaded.

So we can add check, to verify block length, which will give details on state of upload.

try
{
    blockBlob.UploadFromFile(filePath);
    if(blockBlob.Properties.Length >= 0)
    {
       // File uploaded successfull
       // You can take any action.
    }
}
catch(Exception ex)
{
    //uploaded failed
}


来源:https://stackoverflow.com/questions/44395881/how-to-check-whether-azure-blob-storage-upload-was-successful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!