[Azure] C# + Azure.Storage.Blobs v12 - 判斷檔案存在、取得 ETag 、判斷 ETag 刪除檔案

2020-12-29

上篇文章  [Azure] C# + Azure.Storage.Blobs 2020 - 建立 Container, 上傳檔案 , 列出資料 , 列出資料夾 , 刪除檔案 , 刪除檔案夾  我們對 Azure Storage Blobs 有基本的操作,之後開始寫一些不是那麼基本的,但是我覺得我會蠻常用到的操作..




前置步驟,首先你得安裝新版的 Azure.Storage.Blobs ( https://www.nuget.org/packages/Azure.Storage.Blobs ) 12.7.0 ,不要裝到舊版的,這邊我撰寫案例是用 .net core 3.1;在 Azure Portal  開一個 儲存體帳戶,並且在左側的存取金鑰中拿到 Connection String.



1.判斷檔案(  案例為 images/girls/images.jpg )是否存在,如果存在就取得 properties ,其中包含 ETag

透過 Microsoft Azure Storage Explorer 查看檔案屬性,也會看到 ETag




Result:
ETag:"0x8D8ABA5AD717292"

2.透過 ETag 比對刪除,這邊就要說一下了,為何要透過比對 ETag 刪除,原因是因為 假設上面上面有一個檔案叫做 sotck.txt 裡面有一個數字做為庫存,假設是10好了,程式如果要 –1 一定是先把檔案拿回後改成 9 之後寫回,但是如果這時候被人家搶先做這動作呢? 你得確保你寫回去的時候原本的值是一樣的,這邊範例就是 刪除檔案 (  images/girls/images.jpg ) 比對 ETag 是否一樣。


如果 ETag 不對,會出現

Unhandled exception. Azure.RequestFailedException: The condition specified using HTTP conditional header(s) is not met.
RequestId:68f8da13-101e-001d-72b8-dd77de000000
Time:2020-12-29T08:01:22.1399564Z
Status: 412 (The condition specified using HTTP conditional header(s) is not met.)
ErrorCode: ConditionNotMet

Headers:
Server: Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0
x-ms-request-id: 68f8da13-101e-001d-72b8-dd77de000000
x-ms-client-request-id: 811fcdea-dd7a-42b0-b1a7-a8c87cbf2b18
x-ms-version: 2020-02-10
x-ms-error-code: ConditionNotMet
Date: Tue, 29 Dec 2020 08:01:21 GMT
Content-Length: 252
Content-Type: application/xml

   at Azure.Storage.Blobs.BlobRestClient.BlockBlob.UploadAsync_CreateResponse(ClientDiagnostics clientDiagnostics, Response response)
    at Azure.Storage.Blobs.BlobRestClient.BlockBlob.UploadAsync(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri resourceUri, Stream body, Int64 contentLength, String version, Nullable`1 timeout, Byte[] transactionalContentHash, String blobContentType, String blobContentEncoding, String blobContentLanguage, Byte[] blobContentHash, String blobCacheControl, IDictionary`2 metadata, String leaseId, String blobContentDisposition, String encryptionKey, String encryptionKeySha256, Nullable`1 encryptionAlgorithm, String encryptionScope, Nullable`1 tier, Nullable`1 ifModifiedSince, Nullable`1 ifUnmodifiedSince, Nullable`1 ifMatch, Nullable`1 ifNoneMatch, String ifTags, String requestId, String blobTagsString, Boolean async, String operationName, CancellationToken cancellationToken)
    at Azure.Storage.Blobs.Specialized.BlockBlobClient.UploadInternal(Stream content, BlobHttpHeaders blobHttpHeaders, IDictionary`2 metadata, IDictionary`2 tags, BlobRequestConditions conditions, Nullable`1 accessTier, IProgress`1 progressHandler, String operationName, Boolean async, CancellationToken cancellationToken)
    at Azure.Storage.Blobs.Specialized.BlockBlobClient.<>c__DisplayClass48_0.<<GetPartitionedUploaderBehaviors>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
    at Azure.Storage.PartitionedUploader`2.UploadInternal(Stream content, TServiceSpecificArgs args, IProgress`1 progressHandler, Boolean async, CancellationToken cancellationToken)
    at Azure.Storage.Blobs.BlobClient.StagedUploadInternal(Stream content, BlobUploadOptions options, Boolean async, CancellationToken cancellationToken)
    at Azure.Core.Pipeline.TaskExtensions.EnsureCompleted[T](Task`1 task)
    at Azure.Storage.Blobs.BlobClient.Upload(String path, BlobUploadOptions options, CancellationToken cancellationToken)   at TestAzureStorageBlobs.Program.DeleteByETag() in C:\Users\donma\source\repos\TestAzureStorageBlobs\TestAzureStorageBlobs\Program.cs:line 102
    at TestAzureStorageBlobs.Program.Main(String[] args) in C:\Users\donma\source\repos\TestAzureStorageBlobs\TestAzureStorageBlobs\Program.cs:line 57



就先筆記到這邊,之前有這概念我是在 Azure Storage Table 看到的只是測試剛好看到就順手側了一下。


當麻許的超技八 2014 | Donma Hsu Design.