Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#6798] Not able to create instance of BlobsTranscriptStore using TokenCredential instead of connectionString and containerName #565

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
Expand Down Expand Up @@ -51,6 +52,22 @@ public BlobsTranscriptStore(string dataConnectionString, string containerName, J
{
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobsTranscriptStore"/> class.
/// </summary>
/// <param name="blobServiceUri">A Uri referencing the blob container that includes the name of the account and the name of the container.</param>
/// <param name="tokenCredential">The token credential to authenticate to the Azure storage.</param>
/// <param name="containerName">Name of the Blob container where entities will be stored.</param>
/// <param name="jsonSerializer">If passing in a custom JsonSerializer, we recommend the following settings:
/// <para>jsonSerializer.TypeNameHandling = TypeNameHandling.None.</para>
/// <para>jsonSerializer.NullValueHandling = NullValueHandling.Include.</para>
/// <para>jsonSerializer.ContractResolver = new DefaultContractResolver().</para>
/// </param>
public BlobsTranscriptStore(Uri blobServiceUri, TokenCredential tokenCredential, string containerName, JsonSerializer jsonSerializer = null)
: this(blobServiceUri, tokenCredential, containerName, default, jsonSerializer)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobsTranscriptStore"/> class.
/// </summary>
Expand Down Expand Up @@ -99,6 +116,59 @@ public BlobsTranscriptStore(string dataConnectionString, string containerName, S
}, isThreadSafe: true);
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobsTranscriptStore"/> class.
/// </summary>
/// <param name="blobServiceUri">A Uri referencing the blob container that includes the name of the account and the name of the container.</param>
/// <param name="tokenCredential">The token credential to authenticate to the Azure storage.</param>
/// <param name="containerName">Name of the Blob container where entities will be stored.</param>
/// <param name="storageTransferOptions">Used for providing options for parallel transfers <see cref="StorageTransferOptions"/>.</param>
/// <param name="jsonSerializer">If passing in a custom JsonSerializer, we recommend the following settings:
/// <para>jsonSerializer.TypeNameHandling = TypeNameHandling.None.</para>
/// <para>jsonSerializer.NullValueHandling = NullValueHandling.Include.</para>
/// <para>jsonSerializer.ContractResolver = new DefaultContractResolver().</para>
/// </param>
public BlobsTranscriptStore(Uri blobServiceUri, TokenCredential tokenCredential, string containerName, StorageTransferOptions storageTransferOptions, JsonSerializer jsonSerializer = null)
{
if (blobServiceUri == null)
{
throw new ArgumentNullException(nameof(blobServiceUri));
}

if (tokenCredential == null)
{
throw new ArgumentNullException(nameof(tokenCredential));
}

if (string.IsNullOrEmpty(containerName))
{
throw new ArgumentNullException(nameof(containerName));
}

_storageTransferOptions = storageTransferOptions;

_jsonSerializer = jsonSerializer ?? JsonSerializer.Create(new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented,
MaxDepth = null,
});

// Triggers a check for the existance of the container
_containerClient = new Lazy<BlobContainerClient>(
() =>
{
var containerClient = new BlobContainerClient(blobServiceUri, tokenCredential);
if (!_checkedContainers.Contains(containerName))
{
containerClient.CreateIfNotExistsAsync().Wait();
_checkedContainers.Add(containerName);
}

return containerClient;
}, isThreadSafe: true);
}

/// <summary>
/// Initializes a new instance of the <see cref="BlobsTranscriptStore"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Bot.Builder.Azure.Blobs;
Expand Down Expand Up @@ -50,13 +51,30 @@ public void ConstructorValidation()
"containerName",
JsonSerializer.Create(new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }));

var blobServiceUri = new Uri("https://storage.net/");

var mockCredential = new Mock<TokenCredential>();
mockCredential
.Setup(c => c.GetTokenAsync(It.IsAny<TokenRequestContext>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new AccessToken("fake-token", DateTimeOffset.UtcNow.AddHours(1)));

// No dataConnectionString. Should throw.
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(null, "containerName"));
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(string.Empty, "containerName"));

// No containerName. Should throw.
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(ConnectionString, null));
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(ConnectionString, string.Empty));

// No URI. Should throw.
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(blobServiceUri: null, mockCredential.Object, "containerName"));

// No tokenCredential. Should throw.
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(blobServiceUri, null, "containerName"));

// No containerName. Should throw.
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(blobServiceUri, mockCredential.Object, null));
Assert.Throws<ArgumentNullException>(() => new BlobsTranscriptStore(blobServiceUri, mockCredential.Object, string.Empty));
}

[Fact]
Expand Down
Loading