Moh*_*eeq 4 c# onedrive microsoft-graph-api
我正在尝试使用 Graph API 将文件上传到 OneDrive。当我上传小于 4MB 的文件时,下面的代码工作正常,但当我尝试上传大于 4MB 的文件时它显示错误。我浏览了此文档,但仍然不确定如何才能完成这项工作。
以下是我对小于 4MB 的文件的工作代码。
using (var client = new HttpClient())
{
var url = "https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/content";
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + GetAccessToken());
byte[] sContents = System.IO.File.ReadAllBytes(filePath);
var content = new ByteArrayContent(sContents);
var response = client.PutAsync(url, content).Result.Content.ReadAsStringAsync().Result;
}
Run Code Online (Sandbox Code Playgroud)
请帮忙
Moh*_*eeq 11
我们需要按长度(即 4MB)拆分字节数组并将它们传递给 OneDrive API。工作版本如下:
using (var client = new HttpClient())
{
var url = "https://graph.microsoft.com/v1.0" + $"/drives/{driveID}/items/{folderId}:/{originalFileName}:/createUploadSession";
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + GetAccessToken());
var sessionResponse = client.PostAsync(apiUrl, null).Result.Content.ReadAsStringAsync().Result;
byte[] sContents = System.IO.File.ReadAllBytes(filePath);
var uploadSession = JsonConvert.DeserializeObject<UploadSessionResponse>(sessionResponse);
string response = UploadFileBySession(uploadSession.uploadUrl, sContents);
}
Run Code Online (Sandbox Code Playgroud)
UploadFileBySession 如下:
private string UploadFileBySession(string url, byte[] file)
{
int fragSize = 1024 * 1024 * 4;
var arrayBatches = ByteArrayIntoBatches(file, fragSize);
int start = 0;
string response = "";
foreach (var byteArray in arrayBatches)
{
int byteArrayLength = byteArray.Length;
var contentRange = " bytes " + start + "-" + (start + (byteArrayLength - 1)) + "/" + file.Length;
using (var client = new HttpClient())
{
var content = new ByteArrayContent(byteArray);
content.Headers.Add("Content-Length", byteArrayLength.ToProperString());
content.Headers.Add("Content-Range", contentRange);
response = client.PutAsync(url, content).Result.Content.ReadAsStringAsync().Result;
}
start = start + byteArrayLength;
}
return response;
}
internal IEnumerable<byte[]> ByteArrayIntoBatches(byte[] bArray, int intBufforLengt)
{
int bArrayLenght = bArray.Length;
byte[] bReturn = null;
int i = 0;
for (; bArrayLenght > (i + 1) * intBufforLengt; i++)
{
bReturn = new byte[intBufforLengt];
Array.Copy(bArray, i * intBufforLengt, bReturn, 0, intBufforLengt);
yield return bReturn;
}
int intBufforLeft = bArrayLenght - i * intBufforLengt;
if (intBufforLeft > 0)
{
bReturn = new byte[intBufforLeft];
Array.Copy(bArray, i * intBufforLengt, bReturn, 0, intBufforLeft);
yield return bReturn;
}
}
Run Code Online (Sandbox Code Playgroud)
UploadSessionResponse 类文件,用于在创建上传会话时反序列化 JSON 响应
public class UploadSessionResponse
{
public string odatacontext { get; set; }
public DateTime expirationDateTime { get; set; }
public string[] nextExpectedRanges { get; set; }
public string uploadUrl { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
对于大于 4MB 的文件,您需要创建一个您发布到此 URL的 uploadSession:
https://graph.microsoft.com/v1.0/me/drive/root:/{item-path}:/createUploadSession
Run Code Online (Sandbox Code Playgroud)
传递一组项目,
{
"item": {
"@odata.type": "microsoft.graph.driveItemUploadableProperties",
"@microsoft.graph.conflictBehavior": "rename",
"name": "largefile.dat"
}
}
Run Code Online (Sandbox Code Playgroud)
我使用"@microsoft.graph.conflictBehavior": "overwrite", 而不是rename.
响应将提供一个上传 url 以批量上传文件
{
"uploadUrl": "https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337",
"expirationDateTime": "2015-01-29T09:21:55.523Z"
}
Run Code Online (Sandbox Code Playgroud)
我没有 ac# 示例,但这是一个 PHP 示例:
https://graph.microsoft.com/v1.0/me/drive/root:/{item-path}:/createUploadSession
Run Code Online (Sandbox Code Playgroud)