azure-storageazure-blob-storage

Trying to acquire a lease on a new blockblob gets me a 404


When I try to acquire a lease on a CloudBlockBlob that I want to create I get a 404.

lease = blockBlob.AcquireLease(_leaseTimeOut, null);
blockBlob.UploadFromByteArrayAsync(contentBytes, 0, contentBytes.Length).Wait();

I can imagine that it is because it doesn't exist yet, however how do I use leases on a blob that I want to create. I don't want concurrency errors here.


Solution

  • You're right, the problem is that the blob hasn't yet been created in the service at this point. The call to acquire a lease is a call to the service, so it will fail if you call it on a blob that doesn't exist.

    Currently there is no way to lease a blob at the same time that you create it. To create a block blob, you need to write at least a single character to it. Once you do that, you can immediately acquire the lease.

    Depending on how you handle your security, you can exclude access to the blob by ensuring that the container is private until you have leased the blob (assuming other clients are not using the account key for access). Alternately, you could write your application so that users must have a SAS in order to write to the blob, then ensure that the SAS grants access to the blob only when and if the blob is available for other clients to write to.

    Finally, see https://learn.microsoft.com/en-us/azure/storage/blobs/concurrency-manage for guidelines on developing concurrent applications with Blob storage.