通过 HTTP POST 请求将附件上传到 Azure DevOpsU

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

我使用 n8n 创建集成 Movidesk/AzureDevOps。

我试图将人们放入 Movidesk 的附件添加到 AzureDevOps 工作项,但一开始不知道如何上传附件。将其链接到我成功成功的工作项目的部分。

自从我使用以来,我无法在本地计算机中下载任何内容,所以在这种情况下我必须处理的是:

"attachments":[ 
        { 
          "fileName":"minhaImagem.png",
          "path":"7BDEA1B62A8641FF86982D0CF9F3DEC0",
          "createdBy":{ 
            "id":"CodRefDeQuemEnviouOArquivo",
            "personType":1,
            "profileType":1,
            "businessName":"Nome de quem enviou o arquivo",
            "email":"[email protected]",
            "phone":"(47) 99999-9999"
          },
          "createdDate":"2017-05-29T14:19:50.0129141"
        }
      ],

但我不知道如何使用“path”变量。

为了

我使用了这个post api调用:

  1. POST https://dev.azure.com/%7BmyOrg%7D/%7BmyProject%7D/_apis/wit/attachments?fileName=test.xlsx&api-version=7.1-preview.3

它返回给我一个 id 和一个 url,但当我打开它时它没有内容。

我认为这是 API 主体的问题。它缺少文件内容本身,但我不知道如何仅使用文件名和路径来使用它

在工作项中,它保持这样。如果我尝试下载它,该文件会返回一个错误,表明文件已损坏

enter image description here

json azure-devops n8n
1个回答
0
投票

要将附件上传到 Azure DevOps 并通过 REST API 请求将附件链接到工作项,请按照以下步骤操作:

  1. 将文件内容转换为二进制数据。示例 PowerShell 脚本:
$fileName = "123.png" # replace with your file name and file name extension
$filePath = "C:\Users\xxxx\Pictures\Screenshots\"+$fileName # replace with your file path
$fileContent = [System.IO.File]::ReadAllBytes($filePath)
  1. 将二进制文件作为附件上传到 Azure DevOps
POST https://dev.azure.com/xxxxx/_apis/wit/attachments?fileName=fileName.png&api-version=7.2-preview.4

您将获得一个 id 和一个 url 作为返回。我们将使用响应中的 url。 响应示例:

{
  "id": "a5cedde4-2dd5-4fcf-befe-fd0977dd3433",
  "url": "https://dev.azure.com/xxxxx/_apis/wit/attachments/a5cedde4-2dd5-4fcf-befe-fd0977dd3433?fileName=fileName.png"
}
  1. 向工作项添加附件
PATCH https://dev.azure.com/xxxxx/_apis/wit/workitems/{id}?api-version=7.2-preview.3

[
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "AttachedFile",
      "url": "https://dev.azure.com/xxxxx/_apis/wit/attachments/a5cedde4-2dd5-4fcf-befe-fd0977dd3433?fileName=fileName.png",
      "attributes": {
        "comment": "Attachment uploaded"
      }
    }
  }
]

这是完整的 PowerShell 脚本示例:

$organization = ""
$project = ""
$personalAccessToken=""

$fileName = "123.png" # replace with your file name and file name extension
$filePath = "C:\Users\xxxx\Pictures\Screenshots\"+$fileName # replace with your file path
$workItemId = "583"  # Replace with your work item ID

# Convert file content to binary data
$fileContent = [System.IO.File]::ReadAllBytes($filePath)

# Upload the file to Azure DevOps
$uri = "https://dev.azure.com/$organization/_apis/wit/attachments?fileName=$fileName&api-version=7.2-preview.4"  
$headers = @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$personalAccessToken"))
}

$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $fileContent -ContentType  "application/octet-stream"
$attachmentUrl = $response.url

# Link the attachment to the work item
$uri = "https://dev.azure.com/$organization/_apis/wit/workitems/$($workItemId)?api-version=7.2-preview.3"
$body = @"
[
    {
        "op": "add",
        "path": "/relations/-",
        "value": {
            "rel": "AttachedFile",
            "url": "$attachmentUrl",
            "attributes": {
                "comment": "Attachment uploaded via PowerShell script"
            }
        }
    }
]
"@

$response = Invoke-RestMethod -Uri $uri -Method Patch -Headers $headers -Body $body -ContentType "application/json-patch+json"

测试结果:

test

© www.soinside.com 2019 - 2024. All rights reserved.