About This Page
This page is part of the Azure documentation. It contains code examples and configuration instructions for working with Azure services.
Bias Analysis
Bias Types:
⚠️
windows_first
⚠️
missing_linux_example
Summary:
The documentation provides file path examples exclusively in Windows format (e.g., '@"c:\temp\media.jpg"'), with no mention of Linux or cross-platform file paths. There are no Linux or POSIX-style file path examples, and no guidance for users on non-Windows systems.
Recommendations:
- Include Linux/Unix-style file path examples (e.g., '/tmp/media.jpg') alongside Windows examples.
- Add a note explaining that file paths are platform-dependent and provide guidance for both Windows and Linux users.
- Consider using Path.Combine or similar cross-platform APIs in code examples to construct file paths in a platform-agnostic way.
- Explicitly mention cross-platform compatibility and test the code on both Windows and Linux environments.
Create pull request
Flagged Code Snippets
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Azure;
using Azure.Communication.Messages;
namespace AdvancedMessagingDownloadMediaQuickstart
{
class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Azure Communication Services - Download WhatsApp message media");
// Authenticate the client
string connectionString = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_CONNECTION_STRING");
NotificationMessagesClient notificationMessagesClient =
new NotificationMessagesClient(connectionString);
await DownloadMediaWithStreamAsync(notificationMessagesClient);
await DownloadMediaToFileAsync(notificationMessagesClient);
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadKey();
}
public static async Task DownloadMediaWithStreamAsync(NotificationMessagesClient notificationMessagesClient)
{
// MediaId GUID of the media received in an incoming message.
// Ex. "00000000-0000-0000-0000-000000000000"
var mediaId = "<MediaId>";
Response<Stream> fileResponse;
try
{
// Download media to stream
fileResponse = await notificationMessagesClient.DownloadMediaAsync(mediaId);
Console.WriteLine(fileResponse.ToString());
}
catch (RequestFailedException e)
{
Console.WriteLine(e);
return;
}
// File extension derived from the MIME type in the response headers.
// Ex. A MIME type of "image/jpeg" would mean the fileExtension should be ".jpg"
var contentType = fileResponse.GetRawResponse().Headers.ContentType;
string fileExtension = GetFileExtension(contentType);
// File location to write the media.
// Ex. @"c:\temp\media.jpg"
string filePath = @"<FilePath>" + "<FileName>" + fileExtension;
Console.WriteLine(filePath);
// Write the media stream to the file
using (Stream outStream = File.OpenWrite(filePath))
{
fileResponse.Value.CopyTo(outStream);
}
}
private static string GetFileExtension(string contentType)
{
return MimeTypes.TryGetValue(contentType, out var extension) ? extension : string.Empty;
}
private static readonly Dictionary<string, string> MimeTypes = new Dictionary<string, string>
{
{ "application/pdf", ".pdf" },
{ "image/jpeg", ".jpg" },
{ "image/png", ".png" },
{ "video/mp4", ".mp4" },
// Add more mappings as needed
};
public static async Task DownloadMediaToFileAsync(NotificationMessagesClient notificationMessagesClient)
{
// MediaId GUID of the media received in an incoming message.
// Ex. "00000000-0000-0000-0000-000000000000"
var mediaId = "<MediaId>";
// File extension derived from the MIME type received in an incoming message
// Ex. A MIME type of "image/jpeg" would mean the fileExtension should be ".jpg"
string fileExtension = "<FileExtension>";
// File location to write the media.
// Ex. @"c:\temp\media.jpg"
string filePath = @"<FilePath>" + "<FileName>" + fileExtension;
Console.WriteLine(filePath);
try
{
// Download media to file
Response response = await notificationMessagesClient.DownloadMediaToAsync(mediaId, filePath);
Console.WriteLine(response.ToString());
}
catch (RequestFailedException e)
{
Console.WriteLine(e);
return;
}
}
}
}