public void CopyFromToRestoreSnapshot(BlobContext context, string containerName, string blobName) { string oldText = "Old stuff"; string newText = "New stuff"; StorageCredentials accountAndKey = new StorageCredentials(context.Account, context.Key); CloudStorageAccount account = new CloudStorageAccount(accountAndKey, false); CloudBlobClient blobClient = new CloudBlobClient(new Uri(context.Address), account.Credentials); CloudBlobContainer container = blobClient.GetContainerReference(containerName); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); BlobTestBase.UploadText(blob, oldText, Encoding.UTF8); CloudBlockBlob snapshot = blob.CreateSnapshot(); Assert.IsNotNull(snapshot.SnapshotTime); BlobTestBase.UploadText(blob, newText, Encoding.UTF8); Uri sourceUri = new Uri(snapshot.Uri.AbsoluteUri + "?snapshot=" + BlobRequest.ConvertDateTimeToSnapshotString(snapshot.SnapshotTime.Value)); OperationContext opContext = new OperationContext(); HttpWebRequest request = BlobHttpWebRequestFactory.CopyFrom(blob.Uri, 30, sourceUri, null, null, opContext); Assert.IsTrue(request != null, "Failed to create HttpWebRequest"); BlobTests.SignRequest(request, context); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.Accepted); string text = BlobTestBase.DownloadText(blob, Encoding.UTF8); Assert.AreEqual <string>(text, oldText); blob.Delete(DeleteSnapshotsOption.IncludeSnapshots, null, null); }
public static HttpWebRequest Delete(Uri uri, int?timeout, DateTimeOffset?snapshot, DeleteSnapshotsOption deleteSnapshotsOption, AccessCondition accessCondition, OperationContext operationContext) { if ((snapshot != null) && (deleteSnapshotsOption != DeleteSnapshotsOption.None)) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, SR.DeleteSnapshotsNotValidError, "deleteSnapshotsOption", "snapshot")); } UriQueryBuilder builder = new UriQueryBuilder(); BlobHttpWebRequestFactory.AddSnapshot(builder, snapshot); HttpWebRequest request = HttpWebRequestFactory.Delete(uri, builder, timeout, operationContext); switch (deleteSnapshotsOption) { case DeleteSnapshotsOption.None: break; // nop case DeleteSnapshotsOption.IncludeSnapshots: request.Headers.Add( Constants.HeaderConstants.DeleteSnapshotHeader, Constants.HeaderConstants.IncludeSnapshotsValue); break; case DeleteSnapshotsOption.DeleteSnapshotsOnly: request.Headers.Add( Constants.HeaderConstants.DeleteSnapshotHeader, Constants.HeaderConstants.SnapshotsOnlyValue); break; } request.ApplyAccessCondition(accessCondition); return(request); }
public static HttpWebRequest GetBlobRequest(BlobContext context, string containerName, string blobName, AccessCondition accessCondition) { bool valid = BlobTests.ContainerNameValidator(containerName) && BlobTests.BlobNameValidator(blobName) && BlobTests.LeaseIdValidator(accessCondition); Uri uri = BlobTests.ConstructGetUri(context.Address, containerName, blobName); HttpWebRequest request = null; OperationContext opContext = new OperationContext(); try { request = BlobHttpWebRequestFactory.Get(uri, context.Timeout, null /* snapshot */, accessCondition, opContext); } catch (InvalidOperationException) { if (valid) { Assert.Fail(); } } if (valid) { Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual("GET", request.Method); BlobTestUtils.RangeHeader(request, null); BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId); } return(request); }
public static void AcquireAndReleaseLeaseTest(BlobContext context, string containerName, string blobName) { CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials(context.Account, context.Key), false); CloudBlobClient client = new CloudBlobClient(new Uri(context.Address), account.Credentials); CloudBlobContainer container = client.GetContainerReference(containerName); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); BlobTestBase.UploadText(blob, "Text sent to cloud", Encoding.UTF8); // acquire a release on the blob and check LeaseStatus to be "locked" OperationContext opContext = new OperationContext(); HttpWebRequest blobRequest = BlobHttpWebRequestFactory.Lease( blob.Uri, context.Timeout, LeaseAction.Acquire, null /* proposed lease ID */, 60 /* lease duration */, null /* break period */, null /* access condition */, opContext); BlobTests.SignRequest(blobRequest, context); string leaseId = null; using (HttpWebResponse response = (HttpWebResponse)blobRequest.GetResponse()) { leaseId = response.Headers["x-ms-lease-id"]; Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.Created); } blob.FetchAttributes(); Assert.AreEqual <LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Locked); // release the release on the blob and check LeaseStatus to be "unlocked" opContext = new OperationContext(); blobRequest = BlobHttpWebRequestFactory.Lease( blob.Uri, context.Timeout, LeaseAction.Release, null /* proposed lease ID */, null /* lease duration */, null /* break period */, AccessCondition.GenerateLeaseCondition(leaseId), opContext); BlobTests.SignRequest(blobRequest, context); using (HttpWebResponse response = (HttpWebResponse)blobRequest.GetResponse()) { Assert.AreEqual <HttpStatusCode>(response.StatusCode, HttpStatusCode.OK); } blob.FetchAttributes(); Assert.AreEqual <LeaseStatus>(blob.Properties.LeaseStatus, LeaseStatus.Unlocked); blob.Delete(); }
/// <summary> /// Constructs a web request to return the user-defined metadata for the blob. /// </summary> /// <param name="uri">The absolute URI to the blob.</param> /// <param name="timeout">The server timeout interval.</param> /// <param name="snapshot">The snapshot timestamp, if the blob is a snapshot.</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 for performing the operation.</returns> public static HttpWebRequest GetMetadata(Uri uri, int?timeout, DateTimeOffset?snapshot, AccessCondition accessCondition, OperationContext operationContext) { UriQueryBuilder builder = new UriQueryBuilder(); BlobHttpWebRequestFactory.AddSnapshot(builder, snapshot); HttpWebRequest request = HttpWebRequestFactory.GetMetadata(uri, timeout, builder, operationContext); request.ApplyAccessCondition(accessCondition); return(request); }
public static HttpWebRequest GetBlockListRequest(BlobContext context, string containerName, string blobName, BlockListingFilter typesOfBlocks, AccessCondition accessCondition) { Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName); OperationContext opContext = new OperationContext(); HttpWebRequest request = BlobHttpWebRequestFactory.GetBlockList(uri, context.Timeout, null /* snapshot */, typesOfBlocks, accessCondition, opContext); Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual("GET", request.Method); BlobTestUtils.RangeHeader(request, null); BlobTestUtils.LeaseIdHeader(request, null); return(request); }
public static HttpWebRequest DeleteBlobRequest(BlobContext context, string containerName, string blobName, AccessCondition accessCondition) { Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName); OperationContext opContext = new OperationContext(); HttpWebRequest request = BlobHttpWebRequestFactory.Delete(uri, context.Timeout, null /* snapshot */, DeleteSnapshotsOption.None, accessCondition, true, opContext); Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual("DELETE", request.Method); BlobTestUtils.RangeHeader(request, null); BlobTestUtils.LeaseIdHeader(request, null); return(request); }
/// <summary> /// Constructs a web request to return the list of blocks for a block blob. /// </summary> /// <param name="uri">The absolute URI to the blob.</param> /// <param name="timeout">The server timeout interval.</param> /// <param name="snapshot">The snapshot timestamp, if the blob is a snapshot.</param> /// <param name="typesOfBlocks">The types of blocks to include in the list: committed, uncommitted, or both.</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 GetBlockList(Uri uri, int?timeout, DateTimeOffset?snapshot, BlockListingFilter typesOfBlocks, AccessCondition accessCondition, OperationContext operationContext) { UriQueryBuilder builder = new UriQueryBuilder(); builder.Add(Constants.QueryConstants.Component, "blocklist"); builder.Add("blocklisttype", typesOfBlocks.ToString()); BlobHttpWebRequestFactory.AddSnapshot(builder, snapshot); HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Get, uri, timeout, builder, operationContext); request.ApplyAccessCondition(accessCondition); return(request); }
public static HttpWebRequest PutBlockListRequest(BlobContext context, string containerName, string blobName, BlobProperties blobProperties, AccessCondition accessCondition) { Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName); HttpWebRequest request = null; OperationContext opContext = new OperationContext(); request = BlobHttpWebRequestFactory.PutBlockList(uri, context.Timeout, blobProperties, accessCondition, opContext); Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual("PUT", request.Method); BlobTestUtils.VersionHeader(request, false); BlobTestUtils.ContentLanguageHeader(request, null); BlobTestUtils.ContentMd5Header(request, null); return(request); }
public static HttpWebRequest PutBlobRequest(BlobContext context, string containerName, string blobName, BlobProperties properties, BlobType blobType, byte[] content, long pageBlobSize, AccessCondition accessCondition) { bool valid = BlobTests.ContainerNameValidator(containerName) && BlobTests.BlobNameValidator(blobName) && BlobTests.PutPropertiesValidator(properties) && BlobTestUtils.ContentValidator(content); bool fatal = !BlobTests.PutPropertiesValidator(properties); Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName); HttpWebRequest request = null; OperationContext opContext = new OperationContext(); try { request = BlobHttpWebRequestFactory.Put(uri, context.Timeout, properties, blobType, pageBlobSize, accessCondition, opContext); if (fatal) { Assert.Fail(); } } catch (InvalidOperationException) { if (valid) { Assert.Fail(); } } if (valid) { Assert.IsNotNull(request); Assert.IsNotNull(request.Method); Assert.AreEqual("PUT", request.Method); BlobTestUtils.VersionHeader(request, false); BlobTestUtils.ContentTypeHeader(request, null); BlobTestUtils.ContentDispositionHeader(request, properties.ContentDisposition); BlobTestUtils.ContentEncodingHeader(request, properties.ContentEncoding); BlobTestUtils.ContentLanguageHeader(request, null); BlobTestUtils.ContentMd5Header(request, null); BlobTestUtils.CacheControlHeader(request, null); BlobTestUtils.BlobTypeHeader(request, properties.BlobType); BlobTestUtils.BlobSizeHeader(request, (properties.BlobType == BlobType.PageBlob) ? properties.Length : (long?)null); } return(request); }
/// <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 flag indicating whether to set the x-ms-version 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); }
/// <summary> /// Constructs a web request to return the list of valid page ranges for a page blob. /// </summary> /// <param name="uri">The absolute URI to the blob.</param> /// <param name="timeout">The server timeout interval.</param> /// <param name="snapshot">The snapshot timestamp, if the blob is a snapshot.</param> /// <param name="offset">The starting offset of the data range over which to list page ranges, in bytes. Must be a multiple of 512.</param> /// <param name="count">The length of the data range over which to list page ranges, in bytes. Must be a multiple of 512.</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 GetPageRanges(Uri uri, int?timeout, DateTimeOffset?snapshot, long?offset, long?count, AccessCondition accessCondition, OperationContext operationContext) { if (offset.HasValue) { CommonUtility.AssertNotNull("count", count); } UriQueryBuilder builder = new UriQueryBuilder(); builder.Add(Constants.QueryConstants.Component, "pagelist"); BlobHttpWebRequestFactory.AddSnapshot(builder, snapshot); HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Get, uri, timeout, builder, operationContext); AddRange(request, offset, count); request.ApplyAccessCondition(accessCondition); return(request); }
/// <summary> /// Generates and validates a request to break a lease. /// </summary> /// <param name="context">The blob context.</param> /// <param name="containerName">The container name.</param> /// <param name="blobName">The blob name.</param> /// <param name="breakPeriod">The break period.</param> /// <returns>A web request for the operation.</returns> public static HttpWebRequest BreakLeaseRequest(BlobContext context, string containerName, string blobName, int?breakPeriod, AccessCondition accessCondition) { HttpWebRequest request; OperationContext opContext = new OperationContext(); if (blobName != null) { // blob lease Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName); request = BlobHttpWebRequestFactory.Lease( uri, context.Timeout, LeaseAction.Break, null /* proposed lease ID */, null /* lease duration */, breakPeriod, accessCondition, opContext); } else { // container lease Uri uri = BlobClientTests.ConstructUri(context.Address, containerName); request = ContainerHttpWebRequestFactory.Lease( uri, context.Timeout, LeaseAction.Break, null /* proposed lease ID */, null /* lease duration */, breakPeriod, accessCondition, opContext); } Assert.IsNotNull(request); Assert.AreEqual("PUT", request.Method); BlobTestUtils.VersionHeader(request, false); BlobTestUtils.LeaseIdHeader(request, null); BlobTestUtils.LeaseActionHeader(request, "break"); BlobTestUtils.LeaseDurationHeader(request, null); BlobTestUtils.ProposedLeaseIdHeader(request, null); BlobTestUtils.BreakPeriodHeader(request, breakPeriod.HasValue ? breakPeriod.Value.ToString() : null); return(request); }
/// <summary> /// Generates and validates a request to change a lease. /// </summary> /// <param name="context">The blob context.</param> /// <param name="containerName">The container name.</param> /// <param name="blobName">The blob name.</param> /// <param name="leaseID">The lease ID.</param> /// <param name="proposedLeaseId">The proposed lease ID.</param> /// <returns>A web request for the operation.</returns> public static HttpWebRequest ChangeLeaseRequest(BlobContext context, string containerName, string blobName, string proposedLeaseId, AccessCondition accessCondition) { HttpWebRequest request; OperationContext opContext = new OperationContext(); if (blobName != null) { // blob lease Uri uri = BlobTests.ConstructPutUri(context.Address, containerName, blobName); request = BlobHttpWebRequestFactory.Lease( uri, context.Timeout, LeaseAction.Change, proposedLeaseId, null /* lease duration */, null /* break period */, accessCondition, opContext); } else { // container lease Uri uri = BlobClientTests.ConstructUri(context.Address, containerName); request = ContainerHttpWebRequestFactory.Lease( uri, context.Timeout, LeaseAction.Change, proposedLeaseId, null /* lease duration */, null /* break period */, accessCondition, opContext); } Assert.IsNotNull(request); Assert.AreEqual("PUT", request.Method); BlobTestUtils.VersionHeader(request, false); BlobTestUtils.LeaseIdHeader(request, accessCondition == null ? null : accessCondition.LeaseId); BlobTestUtils.LeaseActionHeader(request, "change"); BlobTestUtils.LeaseDurationHeader(request, null); BlobTestUtils.ProposedLeaseIdHeader(request, proposedLeaseId); BlobTestUtils.BreakPeriodHeader(request, null); return(request); }
public void ClearPageRangeScenarioTest(string containerName, string blobName, HttpStatusCode?expectedError) { // 1. Create Sparse Page Blob int blobSize = 128 * 1024; BlobProperties properties = new BlobProperties() { BlobType = BlobType.PageBlob }; Uri uri = BlobTests.ConstructPutUri(BlobContext.Address, containerName, blobName); OperationContext opContext = new OperationContext(); HttpWebRequest webRequest = BlobHttpWebRequestFactory.Put(uri, BlobContext.Timeout, properties, BlobType.PageBlob, blobSize, null, opContext); BlobTests.SignRequest(webRequest, BlobContext); using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse) { BlobTests.PutBlobResponse(response, BlobContext, expectedError); } // 2. Now upload some page ranges for (int m = 0; m * 512 * 4 < blobSize; m++) { int startOffset = 512 * 4 * m; int length = 512; PageRange range = new PageRange(startOffset, startOffset + length - 1); opContext = new OperationContext(); HttpWebRequest pageRequest = BlobHttpWebRequestFactory.PutPage(uri, BlobContext.Timeout, range, PageWrite.Update, null, opContext); pageRequest.ContentLength = 512; BlobTests.SignRequest(pageRequest, BlobContext); Stream outStream = pageRequest.GetRequestStream(); for (int n = 0; n < 512; n++) { outStream.WriteByte((byte)m); } outStream.Close(); using (HttpWebResponse pageResponse = pageRequest.GetResponse() as HttpWebResponse) { } } // 3. Now do a Get Page Ranges List <PageRange> pageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpWebRequest pageRangeRequest = BlobHttpWebRequestFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, opContext); BlobTests.SignRequest(pageRangeRequest, BlobContext); using (HttpWebResponse pageRangeResponse = pageRangeRequest.GetResponse() as HttpWebResponse) { GetPageRangesResponse getPageRangesResponse = new GetPageRangesResponse(pageRangeResponse.GetResponseStream()); pageRanges.AddRange(getPageRangesResponse.PageRanges.ToList()); } // 4. Now Clear some pages bool skipFlag = false; foreach (PageRange pRange in pageRanges) { skipFlag = !skipFlag; if (skipFlag) { continue; } opContext = new OperationContext(); HttpWebRequest clearPageRequest = BlobHttpWebRequestFactory.PutPage(uri, BlobContext.Timeout, pRange, PageWrite.Clear, null, opContext); clearPageRequest.ContentLength = 0; BlobTests.SignRequest(clearPageRequest, BlobContext); using (HttpWebResponse clearResponse = clearPageRequest.GetResponse() as HttpWebResponse) { } } // 5. Get New Page ranges and verify List <PageRange> newPageRanges = new List <PageRange>(); opContext = new OperationContext(); HttpWebRequest newPageRangeRequest = BlobHttpWebRequestFactory.GetPageRanges(uri, BlobContext.Timeout, null, null, null, null, opContext); BlobTests.SignRequest(newPageRangeRequest, BlobContext); using (HttpWebResponse newPageRangeResponse = newPageRangeRequest.GetResponse() as HttpWebResponse) { GetPageRangesResponse getNewPageRangesResponse = new GetPageRangesResponse(newPageRangeResponse.GetResponseStream()); newPageRanges.AddRange(getNewPageRangesResponse.PageRanges.ToList()); } Assert.AreEqual(pageRanges.Count(), newPageRanges.Count() * 2); for (int l = 0; l < newPageRanges.Count(); l++) { Assert.AreEqual(pageRanges[2 * l].StartOffset, newPageRanges[l].StartOffset); Assert.AreEqual(pageRanges[2 * l].EndOffset, newPageRanges[l].EndOffset); } }