public void ToSasQueryParameters_BlobTest() { // Arrange var constants = new TestConstants(this); var containerName = GetNewContainerName(); var blobName = GetNewBlobName(); BlobSasBuilder blobSasBuilder = BuildBlobSasBuilder(includeBlob: true, includeSnapshot: false, containerName, blobName, constants); var signature = BuildSignature(includeBlob: true, includeSnapshot: false, containerName, blobName, constants); // Act BlobSasQueryParameters sasQueryParameters = blobSasBuilder.ToSasQueryParameters(constants.Sas.SharedKeyCredential); // Assert Assert.AreEqual(SasQueryParameters.DefaultSasVersion, sasQueryParameters.Version); Assert.IsNull(sasQueryParameters.Services); Assert.IsNull(sasQueryParameters.ResourceTypes); Assert.AreEqual(constants.Sas.Protocol, sasQueryParameters.Protocol); Assert.AreEqual(constants.Sas.StartTime, sasQueryParameters.StartsOn); Assert.AreEqual(constants.Sas.ExpiryTime, sasQueryParameters.ExpiresOn); Assert.AreEqual(constants.Sas.IPRange, sasQueryParameters.IPRange); Assert.AreEqual(constants.Sas.Identifier, sasQueryParameters.Identifier); Assert.AreEqual(Constants.Sas.Resource.Blob, sasQueryParameters.Resource); Assert.AreEqual(Permissions, sasQueryParameters.Permissions); Assert.AreEqual(signature, sasQueryParameters.Signature); AssertResponseHeaders(constants, sasQueryParameters); }
public BlobServiceClient GetServiceClient_AccountSas( StorageSharedKeyCredential sharedKeyCredentials = default, BlobSasQueryParameters sasCredentials = default) => InstrumentClient( new BlobServiceClient( new Uri($"{TestConfigDefault.BlobServiceEndpoint}?{sasCredentials ?? GetNewAccountSas(sharedKeyCredentials: sharedKeyCredentials)}"), GetOptions()));
public async Task SetBlobAccessTier_ContainerScoped_Basic_ContainerSas() { await using TestScenario scenario = Scenario(); BlobClient[] blobs = await scenario.CreateBlobsAsync(3); string containerName = scenario.Containers[0].Container.Name; BlobSasBuilder blobSasBuilder = new BlobSasBuilder(BlobContainerSasPermissions.All, Recording.Now.AddDays(1)) { BlobContainerName = containerName }; BlobSasQueryParameters sasQueryParameters = blobSasBuilder.ToSasQueryParameters(GetNewSharedKeyCredentials()); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(scenario.Service.Uri) { BlobContainerName = containerName, Sas = sasQueryParameters }; BlobContainerClient sasContainerClient = InstrumentClient(new BlobContainerClient(blobUriBuilder.ToUri(), GetOptions())); BlobBatchClient blobBatchClient = sasContainerClient.GetBlobBatchClient(); using BlobBatch batch = blobBatchClient.CreateBatch(); Response[] responses = new Response[] { batch.SetBlobAccessTier(new Uri($"{blobs[0].Uri}?{sasQueryParameters}"), AccessTier.Cool), batch.SetBlobAccessTier(new Uri($"{blobs[1].Uri}?{sasQueryParameters}"), AccessTier.Cool), batch.SetBlobAccessTier(new Uri($"{blobs[2].Uri}?{sasQueryParameters}"), AccessTier.Cool) }; Response response = await blobBatchClient.SubmitBatchAsync(batch); scenario.AssertStatus(202, response); scenario.AssertStatus(200, responses); await scenario.AssertTiers(AccessTier.Cool, blobs); }
public void ToSasQueryParameters_ContainerIdentityTest() { // Arrange var constants = new TestConstants(this); var containerName = GetNewContainerName(); var blobName = GetNewBlobName(); BlobSasBuilder blobSasBuilder = BuildBlobSasBuilder(includeBlob: false, includeSnapshot: false, containerName, blobName, constants); var signature = BuildIdentitySignature(includeBlob: false, includeSnapshot: false, containerName, blobName, constants); // Act BlobSasQueryParameters sasQueryParameters = blobSasBuilder.ToSasQueryParameters(GetUserDelegationKey(constants), constants.Sas.Account); // Assert Assert.AreEqual(SasQueryParameters.DefaultSasVersion, sasQueryParameters.Version); Assert.IsNull(sasQueryParameters.Services); Assert.IsNull(sasQueryParameters.ResourceTypes); Assert.AreEqual(constants.Sas.Protocol, sasQueryParameters.Protocol); Assert.AreEqual(constants.Sas.StartTime, sasQueryParameters.StartsOn); Assert.AreEqual(constants.Sas.ExpiryTime, sasQueryParameters.ExpiresOn); Assert.AreEqual(constants.Sas.IPRange, sasQueryParameters.IPRange); Assert.AreEqual(String.Empty, sasQueryParameters.Identifier); Assert.AreEqual(constants.Sas.KeyObjectId, sasQueryParameters.KeyObjectId); Assert.AreEqual(constants.Sas.KeyTenantId, sasQueryParameters.KeyTenantId); Assert.AreEqual(constants.Sas.KeyStart, sasQueryParameters.KeyStartsOn); Assert.AreEqual(constants.Sas.KeyExpiry, sasQueryParameters.KeyExpiresOn); Assert.AreEqual(constants.Sas.KeyService, sasQueryParameters.KeyService); Assert.AreEqual(constants.Sas.KeyVersion, sasQueryParameters.KeyVersion); Assert.AreEqual(Constants.Sas.Resource.Container, sasQueryParameters.Resource); Assert.AreEqual(Permissions, sasQueryParameters.Permissions); Assert.AreEqual(signature, sasQueryParameters.Signature); AssertResponseHeaders(constants, sasQueryParameters); }
public async Task SetBlobAccessTier_ContainerScoped_Basic_Convenience_ContainerSas() { await using TestScenario scenario = Scenario(); BlobClient[] blobs = await scenario.CreateBlobsAsync(3); string containerName = scenario.Containers[0].Container.Name; BlobSasBuilder blobSasBuilder = new BlobSasBuilder(BlobContainerSasPermissions.All, Recording.Now.AddDays(1)) { BlobContainerName = containerName }; BlobSasQueryParameters sasQueryParameters = blobSasBuilder.ToSasQueryParameters(GetNewSharedKeyCredentials()); BlobUriBuilder blobUriBuilder = new BlobUriBuilder(scenario.Service.Uri) { BlobContainerName = containerName, Sas = sasQueryParameters }; BlobContainerClient sasContainerClient = InstrumentClient(new BlobContainerClient(blobUriBuilder.ToUri(), GetOptions())); BlobBatchClient blobBatchClient = sasContainerClient.GetBlobBatchClient(); Uri[] uris = blobs.Select(b => new Uri($"{b.Uri}?{sasQueryParameters}")).ToArray(); Response[] responses = await blobBatchClient.SetBlobsAccessTierAsync(uris, AccessTier.Cool); scenario.AssertStatus(200, responses); await scenario.AssertTiers(AccessTier.Cool, blobs); }
public BlobServiceClient GetServiceClient_AccountSas( StorageSharedKeyCredential sharedKeyCredentials = default, BlobSasQueryParameters sasCredentials = default) => this.InstrumentClient( new BlobServiceClient( new Uri($"{TestConfigurations.DefaultTargetTenant.BlobServiceEndpoint}?{sasCredentials ?? this.GetNewAccountSasCredentials(sharedKeyCredentials ?? this.GetNewSharedKeyCredentials())}"), this.GetOptions()));
public BlobServiceClient GetServiceClient_BlobServiceIdentitySas_Container( string containerName, UserDelegationKey userDelegationKey, BlobSasQueryParameters sasCredentials = default) => InstrumentClient( new BlobServiceClient( new Uri($"{TestConfigOAuth.BlobServiceEndpoint}?{sasCredentials ?? GetNewBlobServiceIdentitySasCredentialsContainer(containerName: containerName, userDelegationKey, TestConfigOAuth.AccountName)}"), GetOptions()));
public BlobServiceClient GetServiceClient_BlobServiceSas_Container( string containerName, StorageSharedKeyCredential sharedKeyCredentials = default, BlobSasQueryParameters sasCredentials = default) => InstrumentClient( new BlobServiceClient( new Uri($"{TestConfigDefault.BlobServiceEndpoint}?{sasCredentials ?? GetNewBlobServiceSasCredentialsContainer(containerName: containerName, sharedKeyCredentials: sharedKeyCredentials ?? GetNewSharedKeyCredentials())}"), GetOptions()));
public BlobServiceClient GetServiceClient_BlobServiceIdentitySas_Blob( string containerName, string blobName, UserDelegationKey userDelegationKey, BlobSasQueryParameters sasCredentials = default) => this.InstrumentClient( new BlobServiceClient( new Uri($"{this.TestConfigOAuth.BlobServiceEndpoint}?{sasCredentials ?? this.GetNewBlobServiceIdentitySasCredentialsBlob(containerName: containerName, blobName: blobName, userDelegationKey: userDelegationKey, accountName: this.TestConfigOAuth.AccountName)}"), this.GetOptions()));
public BlobServiceClient GetServiceClient_BlobServiceSas_Blob( string containerName, string blobName, StorageSharedKeyCredential sharedKeyCredentials = default, BlobSasQueryParameters sasCredentials = default) => this.InstrumentClient( new BlobServiceClient( new Uri($"{TestConfigurations.DefaultTargetTenant.BlobServiceEndpoint}?{sasCredentials ?? this.GetNewBlobServiceSasCredentialsBlob(containerName: containerName, blobName: blobName, sharedKeyCredentials: sharedKeyCredentials ?? this.GetNewSharedKeyCredentials())}"), this.GetOptions()));
public BlobServiceClient GetServiceClient_BlobServiceSas_Snapshot( string containerName, string blobName, string snapshot, StorageSharedKeyCredential sharedKeyCredentials = default, BlobSasQueryParameters sasCredentials = default) => this.InstrumentClient( new BlobServiceClient( new Uri($"{this.TestConfigDefault.BlobServiceEndpoint}?{sasCredentials ?? this.GetNewBlobServiceSasCredentialsSnapshot(containerName: containerName, blobName: blobName, snapshot: snapshot, sharedKeyCredentials: sharedKeyCredentials ?? this.GetNewSharedKeyCredentials())}"), this.GetOptions()));
public void ToSasQueryParameters_IdentifierTest() { // Arrange TestConstants constants = TestConstants.Create(this); string containerName = GetNewContainerName(); string resource = "c"; BlobSasBuilder sasBuilder = new BlobSasBuilder() { Identifier = constants.Sas.Identifier, BlobContainerName = containerName, Protocol = SasProtocol.Https, Resource = resource, }; // Act BlobSasQueryParameters sasQueryParameters = sasBuilder.ToSasQueryParameters(constants.Sas.SharedKeyCredential); // Assert Assert.AreEqual(constants.Sas.Identifier, sasQueryParameters.Identifier); Assert.AreEqual(SasProtocol.Https, sasQueryParameters.Protocol); Assert.AreEqual(resource, sasQueryParameters.Resource); }
/// <summary> /// Initializes a new instance of the <see cref="BlobUriBuilder"/> /// class with the specified <see cref="Uri"/>. /// </summary> /// <param name="uri"> /// The <see cref="Uri"/> to a storage resource. /// </param> public BlobUriBuilder(Uri uri) { this.Scheme = uri.Scheme; this.Host = uri.Host; this.Port = uri.Port; this.AccountName = ""; this.ContainerName = ""; this.BlobName = ""; this.Snapshot = ""; //this.VersionId = ""; this.Sas = null; // Find the account, container, & blob names (if any) if (!String.IsNullOrEmpty(uri.AbsolutePath)) { // If path starts with a slash, remove it var path = (uri.AbsolutePath[0] == '/') ? uri.AbsolutePath.Substring(1) : uri.AbsolutePath; var startIndex = 0; if (IsHostIPEndPointStyle(uri.Host)) { var accountEndIndex = path.IndexOf("/", StringComparison.InvariantCulture); // Slash not found; path has account name & no container name if (accountEndIndex == -1) { this.AccountName = path; startIndex = path.Length; } else { this.AccountName = path.Substring(0, accountEndIndex); startIndex = accountEndIndex + 1; } } // Find the next slash (if it exists) var containerEndIndex = path.IndexOf("/", startIndex, StringComparison.InvariantCulture); if (containerEndIndex == -1) { this.ContainerName = path.Substring(startIndex); // Slash not found; path has container name & no blob name } else { this.ContainerName = path.Substring(startIndex, containerEndIndex - startIndex); // The container name is the part between the slashes this.BlobName = path.Substring(containerEndIndex + 1); // The blob name is after the container slash } } // Convert the query parameters to a case-sensitive map & trim whitespace var paramsMap = new UriQueryParamsCollection(uri.Query); if (paramsMap.TryGetValue(Constants.SnapshotParameterName, out var snapshotTime)) { this.Snapshot = snapshotTime; // If we recognized the query parameter, remove it from the map paramsMap.Remove(Constants.SnapshotParameterName); } //if(paramsMap.TryGetValue(VersionIdParameterName, out var versionId)) //{ // this.VersionId = versionId; // // If we recognized the query parameter, remove it from the map // paramsMap.Remove(VersionIdParameterName); //} if (paramsMap.ContainsKey(Constants.Sas.Parameters.Version)) { this.Sas = new BlobSasQueryParameters(paramsMap); } this.UnparsedParams = paramsMap.ToString(); }