Reading from Blob container instead of public uri for azure speech Api
Danial Bakhsheshi
20
Reputation points
I can download the file through the code below from my blob storage:
public async Task<Stream> ReadFileFromBlobStorageAsync(string blobName)
{
try
{
var containerClient = _blobServiceClient.GetBlobContainerClient("audio-input");
var blobClient = containerClient.GetBlobClient(blobName);
if (await blobClient.ExistsAsync())
{
var response = await blobClient.DownloadAsync();
return response.Value.Content;
}
else
{
throw new FileNotFoundException($"File {blobName} not found in blob storage.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading file from Blob Storage: {ex.Message}");
throw;
}
}
but right now the logic below which I took from the call-centre sample code is only accepting the audio public URL. how can I change it in a way to read from the blob without downloading the file to my local?
here is the code :
private async Task<string> CreateTranscription(string inputAudioURL)
{
var uri = new UriBuilder(Uri.UriSchemeHttps, speechEndpoint!);
uri.Path = speechTranscriptionPath;
// Create Transcription API JSON request sample and schema:
// https://westus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/CreateTranscription
// Notes:
// - locale and displayName are required.
// - diarizationEnabled should only be used with mono audio input.
var content = new
{
contentUrls = new string[] { inputAudioURL },
properties = new
{
//diarizationEnabled = !useStereoAudio,
timeToLive = "PT30M"
},
locale = locale,
displayName = $"call_center_{DateTime.Now.ToString()}"
};
var response = await RestHelper.SendPost(uri.Uri.ToString(), JsonSerializer.Serialize(content), speechSubscriptionKey!, new HttpStatusCode[] { HttpStatusCode.Created });
using (JsonDocument document = JsonDocument.Parse(response.content))
{
// Create Transcription API JSON response sample and schema:
// https://westus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/CreateTranscription
// NOTE: Remember to call ToString(), EnumerateArray(), GetInt32(), etc. on the return value from GetProperty
// to convert it from a JsonElement.
var transcriptionUri = document.RootElement.Clone().GetProperty("self").ToString();
// The transcription ID is at the end of the transcription URI.
var transcriptionId = transcriptionUri.Split("/").Last();
// Verify the transcription ID is a valid GUID.
Guid guid;
if (!Guid.TryParse(transcriptionId, out guid))
{
throw new Exception($"Unable to parse response from Create Transcription API:{Environment.NewLine}{response.content}");
}
return transcriptionId;
}
}
public static async Task<(HttpResponseMessage response, string content)> SendPost(string uri, string requestBody, string key, HttpStatusCode[] expectedStatusCodes)
{
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(uri);
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
if (!string.IsNullOrEmpty(requestBody))
{
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
}
var response_1 = await client.SendAsync(request);
if (response_1.Content == null)
{
throw new Exception($"The response from {uri} contains no content.");
}
var response_2 = await response_1.Content.ReadAsStringAsync();
if (!expectedStatusCodes.Contains(response_1.StatusCode))
{
throw new Exception($"The response from {uri} has an unexpected status code: {response_1.StatusCode}. Response:{Environment.NewLine}{response_2}");
}
return (response_1, response_2);
}
}
I can connect and download the file from blob through my connection strings, but the azure speech API needs the file URL. that is my problem, is there anyway to make this work?
thank you for your help in advance.
Sign in to answer