使用图API上传大文件到SharePoint Online - ContentType错误



我正在寻找一种使用PowerShell和Graph API上传大文件(超过4MB)到SharePoint Online的方法。我可以上传大小为4MB的文件而没有任何问题,但上传较大的文件时就遇到问题了。

我正在尝试使用此解决方案,但以下部分(第105行,当试图上传文件时)不适合我:

$invokeRestMethodParams = @{
Uri     = $uploadUrl
Method  = "PUT"
Headers = @{
Accept           = "application/json"
"Content-Type"   = "text/plain"
Authorization    = "bearer $($token)"
"Content-Length" = $fileLength
"Content-Range"  =  "bytes 0-$($fileLength-1)/$($fileLength)"
}
Body = $fileInAscii
}
$response = Invoke-RestMethod @invokeRestMethodParams

我一直在Invoke-RestMethod上得到以下错误(在此之前的所有内容都可以正常工作):

The cmdlet cannot run because the -ContentType parameter is not a valid Content-Type header. Specify a valid Content-Type for -ContentType, then retry. To suppress header validation, supply the -SkipHeaderValidation parameter.

如果我使用-SkipHeaderValidation参数,我得到以下错误:

Response status code does not indicate success: 400 (Bad Request).

我尝试过不同类型的文件(它们都小于10MB),如TXT或PDF,甚至不同的内容类型,如"application/octet-stream"或者"文本/明文",但似乎都不起作用……我也尝试了另一个解决方案,我在网上找到(这个),但错误是一样的。

是任何人都经历相同的,或者你有一个脚本上传大文件(/4 mb)在线使用SharePoint PowerShell和图形API ?非常感谢!

问候,努诺-

我已经找到了一个更简单的解决方案,适用于超过3MB的文件(我也测试过100MB大小的文件)。上传URL,我曾经在我的第一篇文章中的代码解决方案。

$fileInBytes = [System.IO.File]::ReadAllBytes($file)
$fileLength = $fileInBytes.Length
$invokeRestMethodParams = @{
Uri     = $uploadUrl
Method  = "PUT"
Body    = $fileInBytes
Headers = @{
'Content-Range' = "bytes 0-$($fileLength-1)/$fileLength"
}
}
Try {
$response = Invoke-RestMethod @invokeRestMethodParams -ErrorAction Stop
Write-Log -Type "INF" -Message "File uploaded to SharePoint Online: '$fileName' ($([Math]::Round($file.length/1MB, 0))MB)"
} Catch {
Write-Log -Type "ERR" -Message "Unable to upload '$fileName' to SharePoint Online: '$($_.Exception.Message)'"
}

你知道我应该怎么做才能摆脱这个错误吗?异常调用ExecuteQuery"与"0";参数:"无法从传输连接读取数据:远程主机已强制关闭现有连接。">

我上传的文件也是10GB大…通常在5GB后会遇到上述错误…任何想法?我还遇到了可能与我们公司VPN有关的问题。我已经命令包括:[System.Threading.Timeout]::无限【净。::SecurityProtocol = [Net. manager]。SecurityProtocolType]:: Tls12谢谢你的帮助。

最新更新