Example #1
0
        public async Task Test_31_Blob_withInfiniteLease()
        {
            CloudBlobContainer cbc = _Client.GetContainerReference("test-append-blob-container");
            await cbc.CreateIfNotExistsAsync();

            CloudAppendBlob blob       = cbc.GetAppendBlobReference("test-append-blob-withlease2.data");
            var             blobExists = await blob.ExistsAsync();

            if (!blobExists)
            {
                await blob.CreateOrReplaceAsync();
            }

            string data = string.Empty.PadLeft(64 * 1024, '*');

            string leaseIdGuid = Guid.NewGuid().ToString();

            var oc      = new OperationContext();
            var ac      = new AccessCondition();
            var leaseID = await blob.AcquireLeaseAsync(null, leaseIdGuid, ac, null, null);

            ac.LeaseId = leaseID;

            try
            {
                for (int ix = 0; ix < 10; ix++)
                {
                    await blob.AppendTextAsync(data, null, ac, null, oc);
                }
            }
            finally
            {
                await blob.ReleaseLeaseAsync(ac, null, oc);
            }
        }
        private async static void AcquireLeaseDemo()
        {
            try
            {
                string             name      = "LeaseDemo/text-0221e06e5e254fb3bb0bf0e37d9d0e32.txt";
                CloudBlobContainer container = _cloudBlobClient.GetContainerReference("text-files");
                await container.CreateIfNotExistsAsync();

                CloudAppendBlob cloudAppendBlob = container.GetAppendBlobReference(name);
                bool            shouldContinue  = true;
                if (await cloudAppendBlob.ExistsAsync())
                {
                    if (cloudAppendBlob.Properties.LeaseStatus == LeaseStatus.Locked)
                    {
                        shouldContinue = false;
                        Console.WriteLine("Blob is currently in lease");
                    }
                    else
                    {
                        await cloudAppendBlob.CreateOrReplaceAsync();
                    }
                }
                if (shouldContinue)
                {
                    string           proposedLeaseId  = Guid.NewGuid().ToString("n");
                    AccessCondition  accessCondition  = new AccessCondition();
                    OperationContext operationContext = new OperationContext();
                    Console.WriteLine("Acquiring lock...");

                    string leaseId = await cloudAppendBlob.AcquireLeaseAsync(null, proposedLeaseId, accessCondition, null, operationContext);

                    accessCondition.LeaseId = leaseId;
                    try
                    {
                        Console.WriteLine("Uploading text...");
                        for (int i = 0; i <= 50; i++)
                        {
                            await cloudAppendBlob.AppendTextAsync($"{Guid.NewGuid().ToString("n")}{Environment.NewLine}", UTF8Encoding.UTF8, accessCondition, null, operationContext);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                    finally
                    {
                        await cloudAppendBlob.ReleaseLeaseAsync(accessCondition, null, operationContext);

                        Console.WriteLine("Process finished. Lock has been released");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        public async Task Test_30_Blob_InfiniteLease()
        {
            // Containers are like folders
            CloudBlobContainer container = _client.GetContainerReference("test-append-blob-container");

            await container.CreateIfNotExistsAsync();

            // Blobs are like files
            CloudAppendBlob blob = container.GetAppendBlobReference("test-append-blob-with-lease");

            var blobExists = await blob.ExistsAsync();

            if (!blobExists)
            {
                await blob.CreateOrReplaceAsync();
            }

            string data = string.Empty.PadLeft(64 * 1024, '*');

            string leaseId = Guid.NewGuid().ToString();

            var operationContext = new OperationContext();

            var accessCondition = new AccessCondition();

            // For pessimistic access control
            // Acquire lease
            var leaseID = await blob.AcquireLeaseAsync(null, leaseId, accessCondition, null, operationContext);

            accessCondition.LeaseId = leaseID;

            try
            {
                // Make modifications based on that lease
                for (int i = 0; i < 1000; i++)
                {
                    await blob.AppendTextAsync(data, null, accessCondition, null, operationContext);
                }
            }
            finally
            {
                // And finally release that lease
                await blob.ReleaseLeaseAsync(accessCondition, null, operationContext);
            }
        }
Example #4
0
 /// <inheritdoc />
 public Task <string> AcquireLeaseAsync(TimeSpan?leaseTime, string proposedLeaseId,
                                        CancellationToken cancellationToken)
 {
     return(_sdk.AcquireLeaseAsync(leaseTime, proposedLeaseId, cancellationToken));
 }
Example #5
0
 /// <inheritdoc />
 public Task <string> AcquireLeaseAsync(TimeSpan?leaseTime, string proposedLeaseId,
                                        CancellationToken cancellationToken)
 {
     return(_sdk.AcquireLeaseAsync(leaseTime, proposedLeaseId, accessCondition: null, options: null, operationContext: null, cancellationToken: cancellationToken));
 }