コード例 #1
0
 private static void DoLeaseOperation(CloudBlob blob, string leaseId, LeaseAction action)
 {
     var credentials = blob.ServiceClient.Credentials;
     var request = BlobRequest.Lease(new Uri(credentials.TransformUri(blob.Uri.AbsoluteUri)), 90,
         action, leaseId);
     credentials.SignRequest(request);
     request.GetResponse().Close();
 }
コード例 #2
0
 private static void DoLeaseOperation(CloudBlob blob, string leaseId, LeaseAction action)
 {
     var creds = blob.ServiceClient.Credentials;
     var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
     var req = BlobRequest.Lease(transformedUri, 60, action, leaseId);
     creds.SignRequest(req);
     req.GetResponse().Close();
 }
コード例 #3
0
 private static async Task DoLeaseOperationAsync(CloudBlob blob, string leaseId, LeaseAction action)
 {
     var creds = blob.ServiceClient.Credentials;
     var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
     var req = BlobRequest.Lease(transformedUri, 90, action, leaseId);
     creds.SignRequest(req);
     using (var response = await req.GetResponseAsync())
     {
         response.Close();
     }
 }
コード例 #4
0
ファイル: AzureUtils.cs プロジェクト: smosgin/labofthings
 public static void DoLeaseOperation(VLogger logger, CloudBlob blob, string leaseId, LeaseAction action, int AzureBlobLeaseTimeout)
 {
     try
     {
         if (blob == null || leaseId == null)
             return;
         var creds = blob.ServiceClient.Credentials;
         var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
         var req = BlobRequest.Lease(transformedUri, AzureBlobLeaseTimeout, action, leaseId);
         creds.SignRequest(req);
         req.GetResponse().Close();
     }
     catch (WebException e)
     {
         Utils.structuredLog(logger, "WebException", e.Message + ". DoLeaseOperation, blob: " + blob.Name + ", leaseId: " + leaseId + ", action " + action);
     }
 }
コード例 #5
0
        /// <summary>
        /// Generates a web request to use to acquire, renew, change, release or break the lease for the blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval, in seconds.</param>
        /// <param name="action">The lease action to perform.</param>
        /// <param name="proposedLeaseId">A lease ID to propose for the result of an acquire or change operation,
        /// or null if no ID is proposed for an acquire operation. This should be null for renew, release, and break operations.</param>
        /// <param name="leaseDuration">The lease duration, in seconds, for acquire operations.
        /// If this is -1 then an infinite duration is specified. This should be null for renew, change, release, and break operations.</param>
        /// <param name="leaseBreakPeriod">The amount of time to wait, in seconds, after a break operation before the lease is broken.
        /// If this is null then the default time is used. This should be null for acquire, renew, change, and release operations.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static StorageRequestMessage Lease(Uri uri, int?timeout, LeaseAction action, string proposedLeaseId, int?leaseDuration, int?leaseBreakPeriod, AccessCondition accessCondition, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "lease");

            StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);

            request.ApplyAccessCondition(accessCondition);

            // Add lease headers
            BlobHttpRequestMessageFactory.AddLeaseAction(request, action);
            BlobHttpRequestMessageFactory.AddLeaseDuration(request, leaseDuration);
            BlobHttpRequestMessageFactory.AddProposedLeaseId(request, proposedLeaseId);
            BlobHttpRequestMessageFactory.AddLeaseBreakPeriod(request, leaseBreakPeriod);

            return(request);
        }
コード例 #6
0
        /// <summary>
        /// Generates a web request to use to acquire, renew, change, release or break the lease for the container.
        /// </summary>
        /// <param name="uri">The absolute URI to the container.</param>
        /// <param name="timeout">The server timeout interval, in seconds.</param>
        /// <param name="action">The lease action to perform.</param>
        /// <param name="proposedLeaseId">A lease ID to propose for the result of an acquire or change operation,
        /// or null if no ID is proposed for an acquire operation. This should be null for renew, release, and break operations.</param>
        /// <param name="leaseDuration">The lease duration, in seconds, for acquire operations.
        /// If this is -1 then an infinite duration is specified. This should be null for renew, change, release, and break operations.</param>
        /// <param name="leaseBreakPeriod">The amount of time to wait, in seconds, after a break operation before the lease is broken.
        /// If this is null then the default time is used. This should be null for acquire, renew, change, and release operations.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest Lease(Uri uri, int?timeout, LeaseAction action, string proposedLeaseId, int?leaseDuration, int?leaseBreakPeriod, AccessCondition accessCondition, OperationContext operationContext)
        {
            UriQueryBuilder builder = GetContainerUriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "lease");

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, builder, operationContext);

            request.ContentLength = 0;

            // Add Headers
            BlobHttpWebRequestFactory.AddLeaseAction(request, action);
            BlobHttpWebRequestFactory.AddLeaseDuration(request, leaseDuration);
            BlobHttpWebRequestFactory.AddProposedLeaseId(request, proposedLeaseId);
            BlobHttpWebRequestFactory.AddLeaseBreakPeriod(request, leaseBreakPeriod);

            request.ApplyAccessCondition(accessCondition);
            return(request);
        }
コード例 #7
0
        static bool DoLeaseOperation(CloudBlob blob, string leaseId, LeaseAction action)
        {
            var creds          = blob.ServiceClient.Credentials;
            var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
            var req            = BlobRequest.Lease(transformedUri, 10, action, leaseId);

            creds.SignRequest(req);

            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException we)
            {
                var statusCode = ((HttpWebResponse)we.Response).StatusCode;
                switch (statusCode)
                {
                case HttpStatusCode.Conflict:
                case HttpStatusCode.NotFound:
                case HttpStatusCode.RequestTimeout:
                case HttpStatusCode.InternalServerError:
                    return(false);

                default:
                    throw;
                }
            }

            try
            {
                var expectedCode = action == LeaseAction.Break ? HttpStatusCode.Accepted : HttpStatusCode.OK;
                return(response.StatusCode == expectedCode);
            }
            finally
            {
                response.Close();
            }
        }
        /// <summary>
        /// Generates a web request to use to acquire, renew, change, release or break the lease for the container.
        /// </summary>
        /// <param name="uri">The absolute URI to the container.</param>
        /// <param name="timeout">The server timeout interval, in seconds.</param>
        /// <param name="action">The lease action to perform.</param>
        /// <param name="proposedLeaseId">A lease ID to propose for the result of an acquire or change operation,
        /// or null if no ID is proposed for an acquire operation. This should be null for renew, release, and break operations.</param>
        /// <param name="leaseDuration">The lease duration, in seconds, for acquire operations.
        /// If this is -1 then an infinite duration is specified. This should be null for renew, change, release, and break operations.</param>
        /// <param name="leaseBreakPeriod">The amount of time to wait, in seconds, after a break operation before the lease is broken.
        /// If this is null then the default time is used. This should be null for acquire, renew, change, and release operations.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpRequestMessage Lease(Uri uri, int? timeout, LeaseAction action, string proposedLeaseId, int? leaseDuration, int? leaseBreakPeriod, AccessCondition accessCondition, HttpContent content, OperationContext operationContext)
        {
            UriQueryBuilder builder = GetContainerUriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "lease");

            HttpRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext);

            // Add Headers
            BlobHttpRequestMessageFactory.AddLeaseAction(request, action);
            BlobHttpRequestMessageFactory.AddLeaseDuration(request, leaseDuration);
            BlobHttpRequestMessageFactory.AddProposedLeaseId(request, proposedLeaseId);
            BlobHttpRequestMessageFactory.AddLeaseBreakPeriod(request, leaseBreakPeriod);

            request.ApplyAccessCondition(accessCondition);
            return request;
        }
コード例 #9
0
        /// <summary>
        /// Constructs a web request to use to acquire, renew, release or break the lease for the blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="action">The lease action to perform.</param>
        /// <param name="leaseId">The lease ID.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest Lease(Uri uri, int timeout, LeaseAction action, string leaseId)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "lease");

            HttpWebRequest request = CreateWebRequest(uri, timeout, builder);

            request.ContentLength = 0;
            request.Method = "PUT";

            Request.AddLeaseId(request, leaseId);

            // acquire, renew, release, or break; required
            request.Headers.Add("x-ms-lease-action", action.ToString());

            return request;
        }
コード例 #10
0
 /// <summary>
 /// Adds a lease action to a request.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="leaseAction">The lease action.</param>
 internal static void AddLeaseAction(StorageRequestMessage request, LeaseAction leaseAction)
 {
     request.Headers.Add(Constants.HeaderConstants.LeaseActionHeader, leaseAction.ToString().ToLower());
 }
コード例 #11
0
 /// <summary>
 /// Generates a web request to use to acquire, renew, change, release or break the lease for the container.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the container.</param>
 /// <param name="timeout">An integer specifying the server timeout interval.</param>
 /// <param name="action">A <see cref="LeaseAction"/> enumeration value indicating the lease action to perform.</param>
 /// <param name="proposedLeaseId">A string specifying the lease ID to propose for the result of an acquire or change operation,
 /// or <c>null</c> if no ID is proposed for an acquire operation. This parameter should be <c>null</c> for renew, release, and break operations.</param>
 /// <param name="leaseDuration">The lease duration, in seconds, for acquire operations.
 /// If this is -1 then an infinite duration is specified. This should be <c>null</c> for renew, change, release, and break operations.</param>
 /// <param name="leaseBreakPeriod">The amount of time to wait, in seconds, after a break operation before the lease is broken.
 /// If this is <c>null</c> then the default time is used. This should be <c>null</c> for acquire, renew, change, and release operations.</param>
 /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest Lease(Uri uri, int? timeout, LeaseAction action, string proposedLeaseId, int? leaseDuration, int? leaseBreakPeriod, AccessCondition accessCondition, OperationContext operationContext)
 {
     return ContainerHttpWebRequestFactory.Lease(uri, timeout, action, proposedLeaseId, leaseDuration, leaseBreakPeriod, accessCondition, true /* useVersionHeader */, operationContext);
 }
コード例 #12
0
 /// <summary>
 /// Adds a lease action to a request.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="leaseAction">The lease action.</param>
 internal static void AddLeaseAction(HttpWebRequest request, LeaseAction leaseAction)
 {
     request.Headers.Add(Constants.HeaderConstants.LeaseActionHeader, leaseAction.ToString().ToLower());
 }
コード例 #13
0
 /// <summary>
 /// Generates a web request to use to acquire, renew, change, release or break the lease for the container.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the container.</param>
 /// <param name="timeout">An integer specifying the server timeout interval.</param>
 /// <param name="action">A <see cref="LeaseAction"/> enumeration value indicating the lease action to perform.</param>
 /// <param name="proposedLeaseId">A string specifying the lease ID to propose for the result of an acquire or change operation,
 /// or <c>null</c> if no ID is proposed for an acquire operation. This parameter should be <c>null</c> for renew, release, and break operations.</param>
 /// <param name="leaseDuration">The lease duration, in seconds, for acquire operations.
 /// If this is -1 then an infinite duration is specified. This should be <c>null</c> for renew, change, release, and break operations.</param>
 /// <param name="leaseBreakPeriod">The amount of time to wait, in seconds, after a break operation before the lease is broken.
 /// If this is <c>null</c> then the default time is used. This should be <c>null</c> for acquire, renew, change, and release operations.</param>
 /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest Lease(Uri uri, int?timeout, LeaseAction action, string proposedLeaseId, int?leaseDuration, int?leaseBreakPeriod, AccessCondition accessCondition, OperationContext operationContext)
 {
     return(ContainerHttpWebRequestFactory.Lease(uri, timeout, action, proposedLeaseId, leaseDuration, leaseBreakPeriod, accessCondition, true /* useVersionHeader */, operationContext));
 }
コード例 #14
0
        static bool DoLeaseOperation(CloudBlob blob, string leaseId, LeaseAction action)
        {
            var creds = blob.ServiceClient.Credentials;
            var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
            var req = BlobRequest.Lease(transformedUri, 10, action, leaseId);
            creds.SignRequest(req);

            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException we)
            {
                var statusCode = ((HttpWebResponse)we.Response).StatusCode;
                switch (statusCode)
                {
                    case HttpStatusCode.Conflict:
                    case HttpStatusCode.NotFound:
                    case HttpStatusCode.RequestTimeout:
                    case HttpStatusCode.InternalServerError:
                        return false;
                    default:
                        throw;
                }
            }

            try
            {
                var expectedCode = action == LeaseAction.Break ? HttpStatusCode.Accepted : HttpStatusCode.OK;
                return response.StatusCode == expectedCode;
            }
            finally
            {
                response.Close();
            }
        }
コード例 #15
0
 /// <summary>
 /// Adds a lease action to a request.
 /// </summary>
 /// <param name="request">The request.</param>
 /// <param name="leaseAction">The lease action.</param>
 internal static void AddLeaseAction(HttpRequestMessage request, LeaseAction leaseAction)
 {
     request.Headers.Add(Constants.HeaderConstants.LeaseActionHeader, leaseAction.ToString().ToLower());
 }
コード例 #16
0
 public void LeaseOperation(CloudBlob blob, string leaseId, LeaseAction action, int timeout)
 {
     var creds = blob.ServiceClient.Credentials;
     var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
     var request = BlobRequest.Lease(transformedUri, timeout, action, leaseId);
     creds.SignRequest(request);
     request.GetResponse().Close();
 }
コード例 #17
0
 public static void DoLeaseOperation(VLogger logger, CloudBlob blob, string leaseId, LeaseAction action, int AzureBlobLeaseTimeout)
 {
     try
     {
         if (blob == null || leaseId == null)
         {
             return;
         }
         var creds          = blob.ServiceClient.Credentials;
         var transformedUri = new Uri(creds.TransformUri(blob.Uri.ToString()));
         var req            = BlobRequest.Lease(transformedUri, AzureBlobLeaseTimeout, action, leaseId);
         creds.SignRequest(req);
         req.GetResponse().Close();
     }
     catch (WebException e)
     {
         Utils.structuredLog(logger, "WebException", e.Message + ". DoLeaseOperation, blob: " + blob.Name + ", leaseId: " + leaseId + ", action " + action);
     }
 }
コード例 #18
0
        /// <summary>
        /// Generates a web request to use to acquire, renew, change, release or break the lease for the container.
        /// </summary>
        /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the container.</param>
        /// <param name="timeout">An integer specifying the server timeout interval.</param>
        /// <param name="action">A <see cref="LeaseAction"/> enumeration value indicating the lease action to perform.</param>
        /// <param name="proposedLeaseId">A string specifying the lease ID to propose for the result of an acquire or change operation,
        /// or <c>null</c> if no ID is proposed for an acquire operation. This parameter should be <c>null</c> for renew, release, and break operations.</param>
        /// <param name="leaseDuration">The lease duration, in seconds, for acquire operations.
        /// If this is -1 then an infinite duration is specified. This should be <c>null</c> for renew, change, release, and break operations.</param>
        /// <param name="leaseBreakPeriod">The amount of time to wait, in seconds, after a break operation before the lease is broken.
        /// If this is <c>null</c> then the default time is used. This should be <c>null</c> for acquire, renew, change, and release operations.</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
        public static HttpWebRequest Lease(Uri uri, int? timeout, LeaseAction action, string proposedLeaseId, int? leaseDuration, int? leaseBreakPeriod, AccessCondition accessCondition, bool useVersionHeader, OperationContext operationContext)
        {
            UriQueryBuilder builder = GetContainerUriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "lease");

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Put, uri, timeout, builder, useVersionHeader, operationContext);

            // Add Headers
            BlobHttpWebRequestFactory.AddLeaseAction(request, action);
            BlobHttpWebRequestFactory.AddLeaseDuration(request, leaseDuration);
            BlobHttpWebRequestFactory.AddProposedLeaseId(request, proposedLeaseId);
            BlobHttpWebRequestFactory.AddLeaseBreakPeriod(request, leaseBreakPeriod);

            request.ApplyAccessCondition(accessCondition);
            return request;
        }
コード例 #19
0
        /// <summary>
        /// Generates a web request to use to acquire, renew, change, release or break the lease for the blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval, in seconds.</param>
        /// <param name="action">The lease action to perform.</param>
        /// <param name="proposedLeaseId">A lease ID to propose for the result of an acquire or change operation,
        /// or null if no ID is proposed for an acquire operation. This should be null for renew, release, and break operations.</param>
        /// <param name="leaseDuration">The lease duration, in seconds, for acquire operations.
        /// If this is -1 then an infinite duration is specified. This should be null for renew, change, release, and break operations.</param>
        /// <param name="leaseBreakPeriod">The amount of time to wait, in seconds, after a break operation before the lease is broken.
        /// If this is null then the default time is used. This should be null for acquire, renew, change, and release operations.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static StorageRequestMessage Lease(Uri uri, int? timeout, LeaseAction action, string proposedLeaseId, int? leaseDuration, int? leaseBreakPeriod, AccessCondition accessCondition, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "lease");

            StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);
            request.ApplyAccessCondition(accessCondition);

            // Add lease headers
            BlobHttpRequestMessageFactory.AddLeaseAction(request, action);
            BlobHttpRequestMessageFactory.AddLeaseDuration(request, leaseDuration);
            BlobHttpRequestMessageFactory.AddProposedLeaseId(request, proposedLeaseId);
            BlobHttpRequestMessageFactory.AddLeaseBreakPeriod(request, leaseBreakPeriod);

            return request;
        }