Microsoft Graph SDK PowerShell,上传文件,有没有办法定义冲突行为,例如,如果文件已存在则失败?

问题描述 投票:0回答:1

我正在使用 Microsoft Graph SDK for PowerShell(v2.9.0,也尝试过 v2.15.0)将文件上传到驱动器(SharePoint 文档库)。我使用以下命令来上传文件。

Set-MgDriveItemContent -DriveId <String> -DriveItemId <String> [-Data <Stream>] -InFile <String> [-Break]
    [-HttpPipelineAppend <SendAsyncStep[]>] [-HttpPipelinePrepend <SendAsyncStep[]>] [-Proxy <Uri>] [-ProxyCredential
    <PSCredential>] [-ProxyUseDefaultCredentials] [-WhatIf] [-Confirm] [<CommonParameters>]

该命令使用此端点

PUT    /drives/{drive-id}/items/{driveItem-id}/content

问题:有没有办法在上面的命令中使用这个参数@microsoft.graph.conflictBehavior?我可以在 Postman 中使用此参数以及上述端点来指定如果文件已存在于 SP 文档库中会发生什么情况。它可以具有以下值之一 -> failreplacerename。但是,到目前为止,我还没有找到使用 Set-MgGraphDriveItemContent 实现此目的的方法。难道就没有办法了吗

文档 https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=powershell 提到了此命令的参数“BodyParameter”,其中可能是我的问题的解决方案。然而,这个参数不存在,至少在较新版本的 Graph SDK for PowerShell >2.8.0 中不存在。

有没有办法使用命令Set-MgDriveItemContent指定此参数@microsoft.graph.conflictBehavior?或者我剩下的唯一解决方案是先进行单独的 GET 调用以检查文件是否已存在或切换到创建上传会话?

对此的任何帮助将不胜感激。

powershell sharepoint file-upload microsoft-graph-api microsoft-graph-files
1个回答
0
投票

您可以参考以下代码

# Import required modules
Import-Module Microsoft.Graph.Authentication, Microsoft.Graph.Filesystems, Microsoft.Graph.Requests

# Define your authentication token
$accessToken = "YOUR_ACCESS_TOKEN"

# Initialize the GraphServiceClient
$client = New-GraphServiceClient -AccessToken $accessToken

# Define file path
$localFilePath = "C:\Path\to\Your\File.txt"

# Define target folder in OneDrive
$targetFolderId = "YOUR_TARGET_FOLDER_ID"

# Define conflict behavior (e.g., fail if file already exists)
$conflictBehavior = "fail"

# Upload file
Invoke-MgUploadContent -GraphClient $client -ConflictBehavior $conflictBehavior -Path $localFilePath -FolderId $targetFolderId
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.