I want to move a video file from Azure Blob Storage to Sharepoint directly using C#. The code below does not work because there is a problem when getting the blobStream. I have ensured that my containerName and blobName are correct. However, when I checked the existence of the blobClient using blobClient.ExistAsync(), it always returns false. Does anyone know why a blob file cannot be detected while it actually existed in the Azure Blob Storage? Thanks in advance.
Here is the code that I implemented:
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
namespace ZoomRecordingHelper
{
public class GraphHandler
{
public GraphServiceClient GraphClient { get; set; }
public GraphHandler()
{
var clientId = "";
var clientSecret = "";
var tenantId = "";
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
var scopes = new[] { "https://graph.microsoft.com/.default" };
GraphClient = new GraphServiceClient(clientSecretCredential, scopes);
}
public async Task UploadFileToSharePoint(string siteId, string driveId, string fileName, Stream fileStream)
{
try
{
var uploadUrl = $"https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/items/root:/{fileName}:/content";
using (var httpClient = new HttpClient())
{
var accessToken = await GetAccessTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (var httpRequest = new HttpRequestMessage(HttpMethod.Put, uploadUrl))
{
httpRequest.Content = new StreamContent(fileStream);
httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
using (var response = await httpClient.SendAsync(httpRequest))
{
response.EnsureSuccessStatusCode();
Console.WriteLine("File uploaded successfully.");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error uploading file: {ex.Message}");
}
}
public async Task<Stream> GetBlobStreamAsync(string connectionString, string containerName, string blobName)
{
var blobServiceClient = new BlobServiceClient(connectionString); // my conn string : DefaultEndpointsProtocol=https;AccountName=databinuscampussolution;AccountKey=xxxxxxxx;EndpointSuffix=core.windows.net
var containerClient = blobServiceClient.GetBlobContainerClient(containerName); // my containerName : bm5
var blobClient = containerClient.GetBlobClient(blobName); // my blobName : zoom/recording/BBS/2024-8-7/93116161045.mp4
var isExist = await blobClient.ExistsAsync(); //always return false
if (isExist.Value)
{
Console.WriteLine($"Start downloading blob {blobName}");
var response = await blobClient.DownloadAsync();
Console.WriteLine($"Finish downloaded blob {blobName}");
return response.Value.Content;
}
else
{
Console.WriteLine($"Blob {blobName} does not exist in container {containerName}.");
return null;
}
}
private async Task<string> GetAccessTokenAsync()
{
var clientId = "";
var clientSecret = "";
var tenantId = "";
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
var tokenRequestContext = new TokenRequestContext(new string[] { "https://graph.microsoft.com/.default" });
var accessTokenResult = await clientSecretCredential.GetTokenAsync(tokenRequestContext);
return accessTokenResult.Token;
}
}
}
1