BlobLeaseClient
The BlobLeaseClient allows you to manipulate Azure
Storage leases on containers and blobs.
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Storage.Blobs.Models;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Storage.Blobs.Specialized
{
public class BlobLeaseClient
{
private readonly BlobBaseClient _blob;
private readonly BlobContainerClient _container;
private string _leaseId;
public static readonly TimeSpan InfiniteLeaseDuration = TimeSpan.FromSeconds(-1);
protected virtual BlobBaseClient BlobClient => _blob;
protected virtual BlobContainerClient BlobContainerClient => _container;
public Uri Uri {
get {
object obj = BlobClient?.Uri;
if (obj == null) {
BlobContainerClient blobContainerClient = BlobContainerClient;
if (blobContainerClient == null)
return null;
obj = blobContainerClient.Uri;
}
return (Uri)obj;
}
}
public virtual string LeaseId {
get {
return Volatile.Read(ref _leaseId);
}
private set {
Volatile.Write(ref _leaseId, value);
}
}
private HttpPipeline Pipeline => BlobClient?.ClientConfiguration.Pipeline ?? BlobContainerClient.ClientConfiguration.Pipeline;
internal virtual BlobClientOptions.ServiceVersion Version => BlobClient?.ClientConfiguration.Version ?? BlobContainerClient.ClientConfiguration.Version;
internal virtual ClientDiagnostics ClientDiagnostics => BlobClient?.ClientConfiguration.ClientDiagnostics ?? BlobContainerClient.ClientConfiguration.ClientDiagnostics;
protected BlobLeaseClient()
{
_blob = null;
_container = null;
}
public BlobLeaseClient(BlobBaseClient client, string leaseId = null)
{
if (client == null)
throw Errors.ArgumentNull("client");
_blob = client;
_container = null;
LeaseId = (leaseId ?? CreateUniqueLeaseId());
}
public BlobLeaseClient(BlobContainerClient client, string leaseId = null)
{
_blob = null;
if (client == null)
throw Errors.ArgumentNull("client");
_container = client;
LeaseId = (leaseId ?? CreateUniqueLeaseId());
}
private static string CreateUniqueLeaseId()
{
return Guid.NewGuid().ToString();
}
private void EnsureClient()
{
if (BlobClient == null && BlobContainerClient == null)
throw BlobErrors.BlobOrContainerMissing("BlobLeaseClient", "BlobBaseClient", "BlobContainerClient");
}
public virtual Response<BlobLease> Acquire(TimeSpan duration, RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
RequestContext val = new RequestContext();
val.set_CancellationToken(cancellationToken);
return ParseAcquireResponse(AcquireInternal(duration, conditions, false, val).EnsureCompleted());
}
[AsyncStateMachine(typeof(<AcquireAsync>d__25))]
public virtual Task<Response<BlobLease>> AcquireAsync(TimeSpan duration, RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
<AcquireAsync>d__25 stateMachine = default(<AcquireAsync>d__25);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<BlobLease>>.Create();
stateMachine.<>4__this = this;
stateMachine.duration = duration;
stateMachine.conditions = conditions;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
private Response<BlobLease> ParseAcquireResponse(Response response)
{
if (BlobClient != null)
return Response.FromValue<BlobLease>(ResponseWithHeaders.FromValue(new BlobAcquireLeaseHeaders(response), response).ToBlobLease(), response);
return Response.FromValue<BlobLease>(ResponseWithHeaders.FromValue(new ContainerAcquireLeaseHeaders(response), response).ToBlobLease(), response);
}
public virtual Response Acquire(TimeSpan duration, RequestConditions conditions, RequestContext context)
{
return AcquireInternal(duration, conditions, false, context).EnsureCompleted();
}
[AsyncStateMachine(typeof(<AcquireAsync>d__28))]
public virtual Task<Response> AcquireAsync(TimeSpan duration, RequestConditions conditions, RequestContext context)
{
<AcquireAsync>d__28 stateMachine = default(<AcquireAsync>d__28);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response>.Create();
stateMachine.<>4__this = this;
stateMachine.duration = duration;
stateMachine.conditions = conditions;
stateMachine.context = context;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<AcquireInternal>d__29))]
private Task<Response> AcquireInternal(TimeSpan duration, RequestConditions conditions, bool async, RequestContext context)
{
<AcquireInternal>d__29 stateMachine = default(<AcquireInternal>d__29);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response>.Create();
stateMachine.<>4__this = this;
stateMachine.duration = duration;
stateMachine.conditions = conditions;
stateMachine.async = async;
stateMachine.context = context;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public virtual Response<BlobLease> Renew(RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return RenewInternal(conditions, false, cancellationToken).EnsureCompleted();
}
[AsyncStateMachine(typeof(<RenewAsync>d__31))]
public virtual Task<Response<BlobLease>> RenewAsync(RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
<RenewAsync>d__31 stateMachine = default(<RenewAsync>d__31);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<BlobLease>>.Create();
stateMachine.<>4__this = this;
stateMachine.conditions = conditions;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<RenewInternal>d__32))]
private Task<Response<BlobLease>> RenewInternal(RequestConditions conditions, bool async, CancellationToken cancellationToken)
{
<RenewInternal>d__32 stateMachine = default(<RenewInternal>d__32);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<BlobLease>>.Create();
stateMachine.<>4__this = this;
stateMachine.conditions = conditions;
stateMachine.async = async;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public virtual Response<ReleasedObjectInfo> Release(RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return ReleaseInternal(conditions, false, cancellationToken).EnsureCompleted();
}
[AsyncStateMachine(typeof(<ReleaseAsync>d__34))]
public virtual Task<Response<ReleasedObjectInfo>> ReleaseAsync(RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
<ReleaseAsync>d__34 stateMachine = default(<ReleaseAsync>d__34);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<ReleasedObjectInfo>>.Create();
stateMachine.<>4__this = this;
stateMachine.conditions = conditions;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<ReleaseInternal>d__35))]
[EditorBrowsable(EditorBrowsableState.Never)]
public virtual Task<Response<ReleasedObjectInfo>> ReleaseInternal(RequestConditions conditions, bool async, CancellationToken cancellationToken)
{
<ReleaseInternal>d__35 stateMachine = default(<ReleaseInternal>d__35);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<ReleasedObjectInfo>>.Create();
stateMachine.<>4__this = this;
stateMachine.conditions = conditions;
stateMachine.async = async;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public virtual Response<BlobLease> Change(string proposedId, RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return ChangeInternal(proposedId, conditions, false, cancellationToken).EnsureCompleted();
}
[AsyncStateMachine(typeof(<ChangeAsync>d__37))]
public virtual Task<Response<BlobLease>> ChangeAsync(string proposedId, RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
<ChangeAsync>d__37 stateMachine = default(<ChangeAsync>d__37);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<BlobLease>>.Create();
stateMachine.<>4__this = this;
stateMachine.proposedId = proposedId;
stateMachine.conditions = conditions;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<ChangeInternal>d__38))]
private Task<Response<BlobLease>> ChangeInternal(string proposedId, RequestConditions conditions, bool async, CancellationToken cancellationToken)
{
<ChangeInternal>d__38 stateMachine = default(<ChangeInternal>d__38);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<BlobLease>>.Create();
stateMachine.<>4__this = this;
stateMachine.proposedId = proposedId;
stateMachine.conditions = conditions;
stateMachine.async = async;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public virtual Response<BlobLease> Break(TimeSpan? breakPeriod = default(TimeSpan?), RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
return BreakInternal(breakPeriod, conditions, false, cancellationToken).EnsureCompleted();
}
[AsyncStateMachine(typeof(<BreakAsync>d__40))]
public virtual Task<Response<BlobLease>> BreakAsync(TimeSpan? breakPeriod = default(TimeSpan?), RequestConditions conditions = null, CancellationToken cancellationToken = default(CancellationToken))
{
<BreakAsync>d__40 stateMachine = default(<BreakAsync>d__40);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<BlobLease>>.Create();
stateMachine.<>4__this = this;
stateMachine.breakPeriod = breakPeriod;
stateMachine.conditions = conditions;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<BreakInternal>d__41))]
private Task<Response<BlobLease>> BreakInternal(TimeSpan? breakPeriod, RequestConditions conditions, bool async, CancellationToken cancellationToken)
{
<BreakInternal>d__41 stateMachine = default(<BreakInternal>d__41);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Response<BlobLease>>.Create();
stateMachine.<>4__this = this;
stateMachine.breakPeriod = breakPeriod;
stateMachine.conditions = conditions;
stateMachine.async = async;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
}