Azure文件存在却检测失败及上传SharePoint超时问题求助
问题1:Azure Blob Storage 404 BlobNotFound异常
尝试用C#将视频文件从Azure Blob Storage直接迁移至SharePoint,已确认containerName与blobName无误,但blobClient.ExistsAsync()始终返回false,收到BlobNotFound的404错误:
{StatusCode: 404, ReasonPhrase: 'The specified blob does not exist.', Version: 1.1, Content: <null>, Headers: { Transfer-Encoding: chunked Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0 x-ms-request-id: 53a1762c-901e-0025-3360-e93687000000 x-ms-client-request-id: 78983a80-de24-44a4-b2ab-e8f34a5ce508 x-ms-version: 2024-05-04 x-ms-error-code: BlobNotFound Date: Thu, 08 Aug 2024 07:00:20 GMT }}
问题2:Azure File Storage获取流后上传SharePoint超时
发现文件实际存储在Azure File Storage,将GetBlobStreamAsync()修改为GetFileStreamAsync()后成功解决流获取问题,但上传至SharePoint时出现超时错误:
System.Threading.Tasks.TaskCanceledException HResult=0x8013153B Message=The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing. Source=System.Net.Http StackTrace: at System.Net.Http.HttpClient.HandleFailure(Exception e, Boolean telemetryStarted, HttpResponseMessage response, CancellationTokenSource cts, CancellationToken cancellationToken, CancellationTokenSource pendingRequestsCts) at System.Net.Http.HttpClient.<<SendAsync>g__Core|83_0>d.MoveNext() at Binus.LMS.Course2.BackEndAPI.ZoomRecordingScheduler.ZoomRecordingHelper.GraphHandler.<UploadFileToSharePoint>d__5.MoveNext() in C:\Users\angel\OneDrive\Desktop\Work\Backend\Course-Service\Course\Course2.BackEndAPI\ZoomRecordingScheduler\ZoomRecordingHelper\GraphHelper.cs:line 55 This exception was originally thrown at this call stack: [External Code] Inner Exception 1: TimeoutException: The operation was canceled. Inner Exception 2: TaskCanceledException: The operation was canceled.
相关实现代码
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); // 连接字符串: DefaultEndpointsProtocol=https;AccountName=databinuscampussolution;AccountKey=xxxxxxxx;EndpointSuffix=core.windows.net var containerClient = blobServiceClient.GetBlobContainerClient(containerName); // containerName: bm5 var blobClient = containerClient.GetBlobClient(blobName); // blobName: zoom/recording/BBS/2024-8-7/93116161045.mp4 var isExist = await blobClient.ExistsAsync(); // 始终返回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; } } }
修改后的GetFileStreamAsync()代码
public async Task<Stream> GetFileStreamAsync(string connectionString, string sharedReference, string destFilePath, string fileName) { var shareClient = new ShareClient(connectionString, sharedReference); ShareDirectoryClient directoryClient = shareClient.GetDirectoryClient(destFilePath); // 获取文件引用 ShareFileClient fileClient = directoryClient.GetFileClient(fileName); var isExist = fileClient.ExistsAsync().Result; try { if (isExist.Value) { // 获取文件属性以确定文件大小 ShareFileProperties properties = await fileClient.GetPropertiesAsync(); var response = await fileClient.DownloadAsync(); return response.Value.Content; } else { return null; } } catch(Exception ex) { throw ex; } }
内容的提问来源于stack exchange,提问作者ngel_lala
相关产品推荐
相关产品推荐

