Reputation: 826
I'm using Azure Storage to log the conversations between my bot and the users. I want to store the attachments the user sends inside an Azure Blob Container. I'm using the ContentUrl attributin this way:
foreach (Attachment item in message.Attachments)
{
/// creo una sottodirectory in cui verranno salvate tutte le immagini con quel conversation Id, quindi di quella conversazione
CloudBlockBlob targetBlob = _alturasbotChatAttachmentBlobsContainer.GetBlockBlobReference("conv#" + message.Conversation.Id.ToLower() + "/" + item.Name);
/// carico il file dall'url datomi dall'utente
try
{
await targetBlob.StartCopyAsync(new Uri(item.ContentUrl));
}
catch (Exception e)
{
throw;
}
}
The problem is that inside the try-catch an exception is thrown if the url is not an absolute one (for example everything works if I insert a image url from google instead of the contentUrl). Maybe the ContentUrl is not usable in this way because it is a local address. There is a way to solve the problem? Do I have to use the Content attribute (if so, how?). Thanks
Upvotes: 0
Views: 1127
Reputation: 826
Thanks to who replied. In the end I've found out that the Content of the attachment is null so I've to use a HttpClient for downloading the content and then I've uploaded the file on Azure Blob Container. Now it works. For the most curious:
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync(item.ContentUrl))
using (Stream streamToReadFrom = await response.Content.ReadAsStreamAsync())
{
await targetBlob.UploadFromStreamAsync(streamToReadFrom);
}
}
Upvotes: 2
Reputation: 136336
Part answer to your problem (because I haven't worked with Bot framework).
Essentially copy blob operation requires the source item's URL should be publicly accessible as copy operation is an asynchronous server-side operation and Azure Storage should be able to read from source once a request to copy blob has been submitted. That's why your code worked when you took a URL from google and used that instead of ContentUrl of the attachment.
Since I have not worked with Bot framework (thus I will be purely speculating), one thing you could do is read the content of attachment you're trying to save in your code as a stream (not sure how you would do that) and then use CloudBlockBlob.UploadFromStream
method to upload the blob.
Upvotes: 3
Reputation: 847
If you read API reference it says directly that CloudBlockBlob does everything using an absolute URI to the blob.
You can find the URI in CloudBlob.StorageUri Property
Upvotes: 0