public async Task UploadFromUrl(string siteUrl, string root, string path, string filename, string originUrl)
{
using ClientContext clientContext = authManager.GetContext(siteUrl);
string text = _downloadDirectory + filename;
Folder uploadDirectory = GetUploadDirectory(clientContext, root, path);
//new WebClient().DownloadFile(new Uri(originUrl), text);
var client = new HttpClient();
GrantAccess(_downloadDirectory);
await DownloadFileTaskAsync(client, new Uri(originUrl), text);
long length = new FileInfo(text).Length;
int num = 10485760;
if (length загрузить файл на локальное устройство -> загрузить файл в Sharepoint. Однако теперь я хочу, чтобы файл Azure был загружен непосредственно в Sharepoint без необходимости предварительной загрузки файла в локальное хранилище. Как мне этого добиться?
Обновленный прогресс:
Я пытаюсь реализовать метод, предложенный ниже, следующим образом:
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/ ... }:/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}");
}
}
private async Task 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;
}
}
}
Однако при применении GraphServiceClient возникают ошибки.
- невозможно преобразовать Azure.Identity.ClientSecretCredential в Microsoft.Graph. IAuthenticationProvider
- невозможно преобразовать из string[] в Microsoft.Graph.IHttpProvider в строке GraphClient = new GraphServiceClient(clientSecretCredential,scopes);
net6.0
v4
Подробнее здесь: https://stackoverflow.com/questions/788 ... in-c-sharp
Мобильная версия