如何使用图形 API 将本地文件上传到共享点?



我正在尝试通过使用图形API将本地PDF文件上传到共享点,但PUT方法只允许我在SharePoint中创建一个新文件,但不允许我上传本地现有的C:/Drive PDF文件

这是我正在使用的API

PUT https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/SharePointFilePath/file-name:/content

根据这里,这是正确的端点。 但是,您需要将文件作为二进制流发送到正文中。 https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

这意味着您需要将本地 pdf 文件读入二进制流,并将其发送到 put 请求正文中的端点。 例如,作为测试,您可以创建一个常规文本文件,其中正文为字符串,内容类型为文本。

此外,如果它大于 4MB,则需要使用其他方法。 https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_createuploadsession 主要是一个上传会话。

以前的测试脚本,用于通过 PnP PowerShell 将文件上传到 SharePoint。

https://social.technet.microsoft.com/Forums/msonline/en-US/25191b51-41dd-4f4b-93aa-a46594c9a184/uploading-a-file-to-sharepoint-online-using-microsoft-graph-api?forum=onlineservicessharepoint

Connect-PnPOnline -AppId 'yourAzure app id' -AppSecret "yourAzure app secret" -AADDomain 'xxx.onmicrosoft.com'
$accessToken= Get-PnPAccessToken
$apiUrl = "https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com,x,x/drives/driveid/items/01AKXHS4ELSEOTLPZHZZDYYBOW57WR6HK6:/test.xlsx:/content"
$path="C:LeeScripttestdata.xlsx"
$file=[IO.File]::ReadAllBytes($path)
Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Put -Body $file -ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

最新更新