public void CloudBlobContainerPolicy() { CloudBlockBlob testBlockBlob = this.testContainer.GetBlockBlobReference("blockblob"); UploadText(testBlockBlob, "blob", Encoding.UTF8); CloudPageBlob testPageBlob = this.testContainer.GetPageBlobReference("pageblob"); UploadText(testPageBlob, "blob", Encoding.UTF8); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = SharedAccessBlobPermissions.Read, }; BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.SharedAccessPolicies.Add("testpolicy", policy); this.testContainer.SetPermissions(permissions); Thread.Sleep(35 * 1000); string sasToken = testBlockBlob.GetSharedAccessSignature(null, "testpolicy"); SASTests.TestAccess(sasToken, policy.Permissions, null, null, testBlockBlob); sasToken = testPageBlob.GetSharedAccessSignature(null, "testpolicy"); SASTests.TestAccess(sasToken, policy.Permissions, null, null, testPageBlob); sasToken = this.testContainer.GetSharedAccessSignature(null, "testpolicy"); SASTests.TestAccess(sasToken, policy.Permissions, null, this.testContainer, testBlockBlob); SASTests.TestAccess(sasToken, policy.Permissions, null, this.testContainer, testPageBlob); }
public void CloudBlobContainerPolicy() { CloudBlockBlob testBlockBlob = this.testContainer.GetBlockBlobReference("blockblob"); CloudPageBlob testPageBlob = this.testContainer.GetPageBlobReference("pageblob"); CloudAppendBlob testAppendBlob = this.testContainer.GetAppendBlobReference("appendblob"); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = SharedAccessBlobPermissions.Read, }; BlobContainerPermissions permissions = new BlobContainerPermissions(); permissions.SharedAccessPolicies.Add("testpolicy", policy); this.testContainer.SetPermissions(permissions); string sasToken = testBlockBlob.GetSharedAccessSignature(null, "testpolicy"); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => SASTests.TestAccess(sasToken, policy.Permissions, null, null, testBlockBlob)); sasToken = testPageBlob.GetSharedAccessSignature(null, "testpolicy"); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => SASTests.TestAccess(sasToken, policy.Permissions, null, null, testPageBlob)); sasToken = testAppendBlob.GetSharedAccessSignature(null, "testpolicy"); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => SASTests.TestAccess(sasToken, policy.Permissions, null, null, testAppendBlob)); sasToken = this.testContainer.GetSharedAccessSignature(null, "testpolicy"); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => SASTests.TestAccess(sasToken, policy.Permissions, null, this.testContainer, testBlockBlob)); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => SASTests.TestAccess(sasToken, policy.Permissions, null, this.testContainer, testPageBlob)); TestHelper.SpinUpTo30SecondsIgnoringFailures(() => SASTests.TestAccess(sasToken, policy.Permissions, null, this.testContainer, testAppendBlob)); }
public void CloudPageBlobSASWithAbsoluteUri() { CloudBlobClient blobClient = GenerateCloudBlobClient(); CloudBlobContainer container = GetRandomContainerReference(); try { container.CreateIfNotExists(); CloudPageBlob testPageBlob = container.GetPageBlobReference(container.Uri + "/" + "pageblob"); UploadText(testPageBlob, "text", Encoding.UTF8); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(10) }; string sasToken = testPageBlob.GetSharedAccessSignature(policy); StorageCredentials creds = new StorageCredentials(sasToken); CloudPageBlob sasPageBlob = new CloudPageBlob(testPageBlob.Uri, creds); UploadText(sasPageBlob, "new text", Encoding.UTF8); } finally { container.DeleteIfExists(); } }
public void CloudPageBlobSASCombinations() { for (int i = 1; i < 8; i++) { CloudPageBlob testBlob = this.testContainer.GetPageBlobReference("blob" + i); UploadText(testBlob, "blob", Encoding.UTF8); SharedAccessBlobPermissions permissions = (SharedAccessBlobPermissions)i; SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = testBlob.GetSharedAccessSignature(policy); TestAccess(sasToken, permissions, null, testBlob); } }
public void CloudBlobContainerGetBlobReferenceFromServerAPM() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), }; CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); blockBlob.PutBlockList(new string[] { }); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); pageBlob.Create(0); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = container.BeginGetBlobReferenceFromServer("bb", ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob1 = container.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob1, typeof(CloudBlockBlob)); result = ((CloudBlockBlob)blob1).BeginCreateSnapshot( ar => waitHandle.Set(), null); waitHandle.WaitOne(); CloudBlockBlob blob1Snapshot = ((CloudBlockBlob)blob1).EndCreateSnapshot(result); result = blob1.BeginSetProperties( ar => waitHandle.Set(), null); waitHandle.WaitOne(); blob1.EndSetProperties(result); Uri blob1SnapshotUri = new Uri(blob1Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob1Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); result = container.ServiceClient.BeginGetBlobReferenceFromServer(blob1SnapshotUri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob1SnapshotReference = container.ServiceClient.EndGetBlobReferenceFromServer(result); AssertAreEqual(blob1Snapshot.Properties, blob1SnapshotReference.Properties); result = container.BeginGetBlobReferenceFromServer("pb", ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob2 = container.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob2, typeof(CloudPageBlob)); result = ((CloudPageBlob)blob2).BeginCreateSnapshot( ar => waitHandle.Set(), null); waitHandle.WaitOne(); CloudPageBlob blob2Snapshot = ((CloudPageBlob)blob2).EndCreateSnapshot(result); result = blob2.BeginSetProperties( ar => waitHandle.Set(), null); waitHandle.WaitOne(); blob2.EndSetProperties(result); Uri blob2SnapshotUri = new Uri(blob2Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob2Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); result = container.ServiceClient.BeginGetBlobReferenceFromServer(blob2SnapshotUri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob2SnapshotReference = container.ServiceClient.EndGetBlobReferenceFromServer(result); AssertAreEqual(blob2Snapshot.Properties, blob2SnapshotReference.Properties); result = container.ServiceClient.BeginGetBlobReferenceFromServer(blockBlob.Uri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob3 = container.ServiceClient.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob3, typeof(CloudBlockBlob)); result = container.ServiceClient.BeginGetBlobReferenceFromServer(pageBlob.Uri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob4 = container.ServiceClient.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob4, typeof(CloudPageBlob)); string blockBlobToken = blockBlob.GetSharedAccessSignature(policy); StorageCredentials blockBlobSAS = new StorageCredentials(blockBlobToken); Uri blockBlobSASUri = blockBlobSAS.TransformUri(blockBlob.Uri); string pageBlobToken = pageBlob.GetSharedAccessSignature(policy); StorageCredentials pageBlobSAS = new StorageCredentials(pageBlobToken); Uri pageBlobSASUri = pageBlobSAS.TransformUri(pageBlob.Uri); result = container.ServiceClient.BeginGetBlobReferenceFromServer(blockBlobSASUri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob5 = container.ServiceClient.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob5, typeof(CloudBlockBlob)); result = container.ServiceClient.BeginGetBlobReferenceFromServer(pageBlobSASUri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob6 = container.ServiceClient.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob6, typeof(CloudPageBlob)); CloudBlobClient client7 = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS); result = client7.BeginGetBlobReferenceFromServer(blockBlobSASUri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob7 = client7.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob7, typeof(CloudBlockBlob)); CloudBlobClient client8 = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS); result = client8.BeginGetBlobReferenceFromServer(pageBlobSASUri, ar => waitHandle.Set(), null); waitHandle.WaitOne(); ICloudBlob blob8 = client8.EndGetBlobReferenceFromServer(result); Assert.IsInstanceOfType(blob8, typeof(CloudPageBlob)); } } finally { container.DeleteIfExists(); } }
private string GetAzureVmSasUri(string vmImageName) { string mediaLinkUri = null; Uri uri = null; StorageManagementClient storageClient = null; string storageAccountName = null; StorageAccountGetKeysResponse getKeysResponse = null; ErrorRecord er = null; StorageCredentials credentials = null; SharedAccessBlobPolicy accessPolicy = null; CloudPageBlob pageBlob = null; string sas = null; mediaLinkUri = GetImageUri(vmImageName); uri = new Uri(mediaLinkUri); storageClient = new StorageManagementClient(this.Client.Credentials, this.Client.BaseUri); storageAccountName = uri.Authority.Split('.')[0]; getKeysResponse = storageClient.StorageAccounts.GetKeys(storageAccountName); if (getKeysResponse.StatusCode != System.Net.HttpStatusCode.OK) { er = RemoteAppCollectionErrorState.CreateErrorRecordFromString( String.Format(Commands_RemoteApp.GettingStorageAccountKeyErrorFormat, getKeysResponse.StatusCode.ToString()), String.Empty, Client.TemplateImages, ErrorCategory.ConnectionError ); ThrowTerminatingError(er); } credentials = new StorageCredentials(storageAccountName, getKeysResponse.SecondaryKey); accessPolicy = new SharedAccessBlobPolicy(); pageBlob = new CloudPageBlob(uri, credentials); accessPolicy.Permissions = SharedAccessBlobPermissions.Read; accessPolicy.SharedAccessStartTime = DateTime.Now; accessPolicy.SharedAccessExpiryTime = DateTime.Now.AddHours(12); sas = pageBlob.GetSharedAccessSignature(accessPolicy); if (sas == null) { er = RemoteAppCollectionErrorState.CreateErrorRecordFromString( Commands_RemoteApp.FailedToGetSasUriError, String.Empty, Client.TemplateImages, ErrorCategory.ConnectionError ); ThrowTerminatingError(er); } return mediaLinkUri + sas; }
public async Task CloudBlobContainerGetBlobReferenceFromServerAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), }; CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); await blockBlob.PutBlockListAsync(new List <string>()); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); await pageBlob.CreateAsync(0); CloudAppendBlob appendBlob = container.GetAppendBlobReference("ab"); await appendBlob.CreateOrReplaceAsync(); CloudBlobClient client; ICloudBlob blob; blob = await container.GetBlobReferenceFromServerAsync("bb"); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); CloudBlockBlob blockBlobSnapshot = await((CloudBlockBlob)blob).CreateSnapshotAsync(); await blob.SetPropertiesAsync(); Uri blockBlobSnapshotUri = new Uri(blockBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + blockBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSnapshotUri); AssertAreEqual(blockBlobSnapshot.Properties, blob.Properties); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlobSnapshot.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.GetBlobReferenceFromServerAsync("pb"); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); CloudPageBlob pageBlobSnapshot = await((CloudPageBlob)blob).CreateSnapshotAsync(); await blob.SetPropertiesAsync(); Uri pageBlobSnapshotUri = new Uri(pageBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + pageBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSnapshotUri); AssertAreEqual(pageBlobSnapshot.Properties, blob.Properties); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlobSnapshot.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.GetBlobReferenceFromServerAsync("ab"); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); CloudAppendBlob appendBlobSnapshot = await((CloudAppendBlob)blob).CreateSnapshotAsync(); await blob.SetPropertiesAsync(); Uri appendBlobSnapshotUri = new Uri(appendBlobSnapshot.Uri.AbsoluteUri + "?snapshot=" + appendBlobSnapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSnapshotUri); AssertAreEqual(appendBlobSnapshot.Properties, blob.Properties); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlobSnapshot.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.Uri); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.Uri); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlob.Uri); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.StorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.StorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlob.StorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); string blockBlobToken = blockBlob.GetSharedAccessSignature(policy); StorageCredentials blockBlobSAS = new StorageCredentials(blockBlobToken); Uri blockBlobSASUri = blockBlobSAS.TransformUri(blockBlob.Uri); StorageUri blockBlobSASStorageUri = blockBlobSAS.TransformUri(blockBlob.StorageUri); string appendBlobToken = appendBlob.GetSharedAccessSignature(policy); StorageCredentials appendBlobSAS = new StorageCredentials(appendBlobToken); Uri appendBlobSASUri = appendBlobSAS.TransformUri(appendBlob.Uri); StorageUri appendBlobSASStorageUri = appendBlobSAS.TransformUri(appendBlob.StorageUri); string pageBlobToken = pageBlob.GetSharedAccessSignature(policy); StorageCredentials pageBlobSAS = new StorageCredentials(pageBlobToken); Uri pageBlobSASUri = pageBlobSAS.TransformUri(pageBlob.Uri); StorageUri pageBlobSASStorageUri = pageBlobSAS.TransformUri(pageBlob.StorageUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); blob = await container.ServiceClient.GetBlobReferenceFromServerAsync(appendBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); client = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); client = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); client = new CloudBlobClient(container.ServiceClient.BaseUri, appendBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(appendBlobSASUri); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(appendBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); client = new CloudBlobClient(container.ServiceClient.StorageUri, blockBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(blockBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); client = new CloudBlobClient(container.ServiceClient.StorageUri, pageBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(pageBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); client = new CloudBlobClient(container.ServiceClient.StorageUri, appendBlobSAS); blob = await client.GetBlobReferenceFromServerAsync(appendBlobSASStorageUri, null, null, null); Assert.IsInstanceOfType(blob, typeof(CloudAppendBlob)); Assert.IsTrue(blob.StorageUri.Equals(appendBlob.StorageUri)); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }
private string CreateSasUriWithPermission(string vhdName, int p) { // Set the destination string vhdBlobName = string.Format("{0}/{1}.vhd", vhdContainerName, Utilities.GetUniqueShortName(Path.GetFileNameWithoutExtension(vhdName))); string httpsBlobUrlRoot = string.Format("https:{0}", blobUrlRoot.Substring(blobUrlRoot.IndexOf('/'))); string vhdDestUri = httpsBlobUrlRoot + vhdBlobName; var destinationBlob2 = new CloudPageBlob(new Uri(vhdDestUri), new StorageCredentials(storageAccountKey.StorageAccountName, storageAccountKey.Primary)); var policy2 = new SharedAccessBlobPolicy() { Permissions = (SharedAccessBlobPermissions)p, SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromHours(1) }; var destinationBlobToken2 = destinationBlob2.GetSharedAccessSignature(policy2); vhdDestUri += destinationBlobToken2; return vhdDestUri; }
private async Task IncrementalCopyAsyncImpl(int overload) { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudPageBlob source = container.GetPageBlobReference("source"); await source.CreateAsync(1024); string data = new string('a', 512); await UploadTextAsync(source, data, Encoding.UTF8); CloudPageBlob sourceSnapshot = await source.CreateSnapshotAsync(null, null, null, null); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write, }; string sasToken = sourceSnapshot.GetSharedAccessSignature(policy); StorageCredentials blobSAS = new StorageCredentials(sasToken); Uri sourceSnapshotUri = blobSAS.TransformUri(TestHelper.Defiddler(sourceSnapshot).SnapshotQualifiedUri); StorageCredentials accountSAS = new StorageCredentials(sasToken); CloudStorageAccount accountWithSAS = new CloudStorageAccount(accountSAS, source.ServiceClient.StorageUri, null, null, null); CloudPageBlob snapshotWithSas = await accountWithSAS.CreateCloudBlobClient().GetBlobReferenceFromServerAsync(sourceSnapshot.SnapshotQualifiedUri) as CloudPageBlob; CloudPageBlob copy = container.GetPageBlobReference("copy"); string copyId = null; if (overload == 0) { #if !FACADE_NETCORE copyId = await copy.StartIncrementalCopyAsync(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); copyId = await copy.StartIncrementalCopyAsync(new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null)); #endif } else if (overload == 1) { #if !FACADE_NETCORE CloudPageBlob blob = new CloudPageBlob(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); #endif copyId = await copy.StartIncrementalCopyAsync(blob); } else if (overload == 2) { #if !FACADE_NETCORE CloudPageBlob blob = new CloudPageBlob(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri)); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); #endif copyId = await copy.StartIncrementalCopyAsync(blob, null, null, null, CancellationToken.None); } else { #if !FACADE_NETCORE copyId = await copy.StartIncrementalCopyAsync(accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri), null, null, null, CancellationToken.None); #else Uri snapShotQualifiedUri = accountSAS.TransformUri(TestHelper.Defiddler(snapshotWithSas).SnapshotQualifiedUri); CloudPageBlob blob = new CloudPageBlob(new StorageUri(snapShotQualifiedUri), null, null); copyId = await copy.StartIncrementalCopyAsync(blob, null, null, null, CancellationToken.None); #endif } await WaitForCopyAsync(copy); Assert.AreEqual(BlobType.PageBlob, copy.BlobType); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, copy.CopyState.TotalBytes); Assert.AreEqual(data.Length, copy.CopyState.BytesCopied); Assert.AreEqual(copyId, copy.CopyState.CopyId); Assert.IsTrue(copy.Properties.IsIncrementalCopy); Assert.IsTrue(copy.CopyState.DestinationSnapshotTime.HasValue); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); } finally { container.DeleteIfExistsAsync().Wait(); } }
public void Initialize() { vmPowershellCmdlets = new ServiceManagementCmdletTestHelper(); vmPowershellCmdlets.ImportAzurePublishSettingsFile(); defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(Resource.DefaultSubscriptionName); Assert.AreEqual(Resource.DefaultSubscriptionName, defaultAzureSubscription.SubscriptionName); storageAccountKey = vmPowershellCmdlets.GetAzureStorageAccountKey(defaultAzureSubscription.CurrentStorageAccount); Assert.AreEqual(defaultAzureSubscription.CurrentStorageAccount, storageAccountKey.StorageAccountName); destination = string.Format(@"http://{0}.blob.core.windows.net/vhdstore/{1}", defaultAzureSubscription.CurrentStorageAccount, Utilities.GetUniqueShortName("PSTestAzureVhd")); patchDestination = string.Format(@"http://{0}.blob.core.windows.net/vhdstore/{1}", defaultAzureSubscription.CurrentStorageAccount, Utilities.GetUniqueShortName("PSTestAzureVhd")); destinationSasUri = string.Format(@"http://{0}.blob.core.windows.net/vhdstore/{1}", defaultAzureSubscription.CurrentStorageAccount, Utilities.GetUniqueShortName("PSTestAzureVhd")); patchDestinationSasUri = string.Format(@"http://{0}.blob.core.windows.net/vhdstore/{1}", defaultAzureSubscription.CurrentStorageAccount, Utilities.GetUniqueShortName("PSTestAzureVhd")); var destinationBlob = new CloudPageBlob(new Uri(destinationSasUri), new StorageCredentials(storageAccountKey.StorageAccountName, storageAccountKey.Primary)); var patchDestinationBlob = new CloudPageBlob(new Uri(patchDestinationSasUri), new StorageCredentials(storageAccountKey.StorageAccountName, storageAccountKey.Primary)); var policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List, SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromHours(1) }; var destinationBlobToken = destinationBlob.GetSharedAccessSignature(policy); var patchDestinationBlobToken = patchDestinationBlob.GetSharedAccessSignature(policy); destinationSasUri += destinationBlobToken; patchDestinationSasUri += patchDestinationBlobToken; blobUrlRoot = string.Format(@"http://{0}.blob.core.windows.net/", defaultAzureSubscription.CurrentStorageAccount); perfFile = "perf.csv"; }
public void CloudBlobSASApiVersionQueryParam() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlob blob; SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), }; CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); blockBlob.PutBlockList(new string[] { }); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); pageBlob.Create(0); CloudAppendBlob appendBlob = container.GetAppendBlobReference("ab"); appendBlob.CreateOrReplace(); string blockBlobToken = blockBlob.GetSharedAccessSignature(policy); StorageCredentials blockBlobSAS = new StorageCredentials(blockBlobToken); Uri blockBlobSASUri = blockBlobSAS.TransformUri(blockBlob.Uri); StorageUri blockBlobSASStorageUri = blockBlobSAS.TransformUri(blockBlob.StorageUri); string pageBlobToken = pageBlob.GetSharedAccessSignature(policy); StorageCredentials pageBlobSAS = new StorageCredentials(pageBlobToken); Uri pageBlobSASUri = pageBlobSAS.TransformUri(pageBlob.Uri); StorageUri pageBlobSASStorageUri = pageBlobSAS.TransformUri(pageBlob.StorageUri); string appendBlobToken = appendBlob.GetSharedAccessSignature(policy); StorageCredentials appendBlobSAS = new StorageCredentials(appendBlobToken); Uri appendBlobSASUri = appendBlobSAS.TransformUri(appendBlob.Uri); StorageUri appendBlobSASStorageUri = appendBlobSAS.TransformUri(appendBlob.StorageUri); OperationContext apiVersionCheckContext = new OperationContext(); apiVersionCheckContext.SendingRequest += (sender, e) => { Assert.IsTrue(e.Request.RequestUri.Query.Contains("api-version")); }; blob = new CloudBlob(blockBlobSASUri); blob.FetchAttributes(operationContext: apiVersionCheckContext); Assert.AreEqual(blob.BlobType, BlobType.BlockBlob); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = new CloudBlob(pageBlobSASUri); blob.FetchAttributes(operationContext: apiVersionCheckContext); Assert.AreEqual(blob.BlobType, BlobType.PageBlob); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = new CloudBlob(blockBlobSASStorageUri, null, null); blob.FetchAttributes(operationContext: apiVersionCheckContext); Assert.AreEqual(blob.BlobType, BlobType.BlockBlob); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); blob = new CloudBlob(pageBlobSASStorageUri, null, null); blob.FetchAttributes(operationContext: apiVersionCheckContext); Assert.AreEqual(blob.BlobType, BlobType.PageBlob); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); } finally { container.DeleteIfExists(); } }
public void CloudBlobSASApiVersionQueryParam() { CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); ICloudBlob blob; SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), }; CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); blockBlob.PutBlockList(new string[] { }); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); pageBlob.Create(0); string blockBlobToken = blockBlob.GetSharedAccessSignature(policy); StorageCredentials blockBlobSAS = new StorageCredentials(blockBlobToken); Uri blockBlobSASUri = blockBlobSAS.TransformUri(blockBlob.Uri); StorageUri blockBlobSASStorageUri = blockBlobSAS.TransformUri(blockBlob.StorageUri); string pageBlobToken = pageBlob.GetSharedAccessSignature(policy); StorageCredentials pageBlobSAS = new StorageCredentials(pageBlobToken); Uri pageBlobSASUri = pageBlobSAS.TransformUri(pageBlob.Uri); StorageUri pageBlobSASStorageUri = pageBlobSAS.TransformUri(pageBlob.StorageUri); OperationContext apiVersionCheckContext = new OperationContext(); apiVersionCheckContext.SendingRequest += (sender, e) => { Assert.IsNull(e.Request.Headers.Get("x-ms-version")); Assert.IsTrue(e.Request.RequestUri.Query.Contains("api-version")); }; blob = container.ServiceClient.GetBlobReferenceFromServer(blockBlobSASUri, operationContext: apiVersionCheckContext); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(blockBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = container.ServiceClient.GetBlobReferenceFromServer(pageBlobSASUri, operationContext: apiVersionCheckContext); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.PrimaryUri.Equals(pageBlob.Uri)); Assert.IsNull(blob.StorageUri.SecondaryUri); blob = container.ServiceClient.GetBlobReferenceFromServer(blockBlobSASStorageUri, operationContext: apiVersionCheckContext); Assert.IsInstanceOfType(blob, typeof(CloudBlockBlob)); Assert.IsTrue(blob.StorageUri.Equals(blockBlob.StorageUri)); blob = container.ServiceClient.GetBlobReferenceFromServer(pageBlobSASStorageUri, operationContext: apiVersionCheckContext); Assert.IsInstanceOfType(blob, typeof(CloudPageBlob)); Assert.IsTrue(blob.StorageUri.Equals(pageBlob.StorageUri)); } finally { container.DeleteIfExists(); } }
public async Task CloudBlobContainerGetBlobReferenceFromServerAsync() { CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy() { Permissions = SharedAccessBlobPermissions.Read, SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), }; CloudBlockBlob blockBlob = container.GetBlockBlobReference("bb"); await blockBlob.PutBlockListAsync(new List <string>()); CloudPageBlob pageBlob = container.GetPageBlobReference("pb"); await pageBlob.CreateAsync(0); ICloudBlob blob1 = await container.GetBlobReferenceFromServerAsync("bb"); Assert.IsInstanceOfType(blob1, typeof(CloudBlockBlob)); CloudBlockBlob blob1Snapshot = await((CloudBlockBlob)blob1).CreateSnapshotAsync(); await blob1.SetPropertiesAsync(); Uri blob1SnapshotUri = new Uri(blob1Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob1Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); ICloudBlob blob1SnapshotReference = await container.ServiceClient.GetBlobReferenceFromServerAsync(blob1SnapshotUri); AssertAreEqual(blob1Snapshot.Properties, blob1SnapshotReference.Properties); ICloudBlob blob2 = await container.GetBlobReferenceFromServerAsync("pb"); Assert.IsInstanceOfType(blob2, typeof(CloudPageBlob)); CloudPageBlob blob2Snapshot = await((CloudPageBlob)blob2).CreateSnapshotAsync(); await blob2.SetPropertiesAsync(); Uri blob2SnapshotUri = new Uri(blob2Snapshot.Uri.AbsoluteUri + "?snapshot=" + blob2Snapshot.SnapshotTime.Value.UtcDateTime.ToString("o")); ICloudBlob blob2SnapshotReference = await container.ServiceClient.GetBlobReferenceFromServerAsync(blob2SnapshotUri); AssertAreEqual(blob2Snapshot.Properties, blob2SnapshotReference.Properties); ICloudBlob blob3 = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlob.Uri); Assert.IsInstanceOfType(blob3, typeof(CloudBlockBlob)); ICloudBlob blob4 = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlob.Uri); Assert.IsInstanceOfType(blob4, typeof(CloudPageBlob)); string blockBlobToken = blockBlob.GetSharedAccessSignature(policy); StorageCredentials blockBlobSAS = new StorageCredentials(blockBlobToken); Uri blockBlobSASUri = blockBlobSAS.TransformUri(blockBlob.Uri); string pageBlobToken = pageBlob.GetSharedAccessSignature(policy); StorageCredentials pageBlobSAS = new StorageCredentials(pageBlobToken); Uri pageBlobSASUri = pageBlobSAS.TransformUri(pageBlob.Uri); ICloudBlob blob5 = await container.ServiceClient.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob5, typeof(CloudBlockBlob)); ICloudBlob blob6 = await container.ServiceClient.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob6, typeof(CloudPageBlob)); CloudBlobClient client7 = new CloudBlobClient(container.ServiceClient.BaseUri, blockBlobSAS); ICloudBlob blob7 = await client7.GetBlobReferenceFromServerAsync(blockBlobSASUri); Assert.IsInstanceOfType(blob7, typeof(CloudBlockBlob)); CloudBlobClient client8 = new CloudBlobClient(container.ServiceClient.BaseUri, pageBlobSAS); ICloudBlob blob8 = await client8.GetBlobReferenceFromServerAsync(pageBlobSASUri); Assert.IsInstanceOfType(blob8, typeof(CloudPageBlob)); } finally { container.DeleteIfExistsAsync().AsTask().Wait(); } }