Reputation: 265
I would like to lease a blob while writing stream to it until it completes.Following code:
Scenario 1:
blob.AcquireLease();
blob.UploadText("Content");
blob.Release();
If i Acquire a lease on blob first, to which content needs to be upload throws an exception since blob is already locked.
Scenario 2:
blob.uploadText("Content");
blob.AcquireLease();
blob.Release();
Since I am uploading the content to blob synchronously,blob.AcquireLease() statement will get executed only when upload is completed.If this the case whats the advantage of this.
Upvotes: 1
Views: 2722
Reputation: 13551
Locking in this way is not necessary for block blobs, which is the most common type of blobs in my experience. Updates to block blobs are atomic.
When you want to guard against overwrites, the If-None-Match
header makes the request fail if the blob has a different ETag than the expected one. If the blob does not exist yet (and thus no ETag is expected), If-None-Match: *
guards against overwriting any blob.
You may read more on the topic in the Managing Concurrency in Blob Storage article in the docs.
Upvotes: 0
Reputation: 60133
You can only modify a leased blob if you own the lease. That means you have to pass the lease along with your request.
Something like this code should work (caveat: not tested, or even compiled):
var leaseId = blob.AcquireLease();
blob.UploadText("Content", Encoding.UTF8, AccessCondition.GenerateLeaseCondition(leaseId), null);
blob.ReleaseLease(AccessCondition.GenerateLeaseCondition(leaseId));
Come to think of it, did your code above even work? ReleaseLease
requires at least one parameter, doesn't it?
For your second question, I believe leases are up to 60 (not 90) seconds by default. Back before the storage client library supported leases, I built my own code for that, and I made a class called AutoRenewLease
that renewed the lease every 45 seconds to make sure I didn't lose it. You can find the code at https://github.com/smarx/WazStorageExtensions. Something similar should work for you.
Upvotes: 5