使用 powershell 进行带有校验和的人工制品上传

artifactory upload with checksum using powershell

您将如何使用 powershell 上传工件?

以下适用于 bash

ARTIFACT_MD5_CHECKSUM=$(md5sum /tmp/bar.zip | awk '{print }')
ARTIFACT_SHA1_CHECKSUM=$(shasum -a 1 /tmp/bar.zip | awk '{ print  }') 

curl --upload-file "/tmp/bar.zip" --header "X-Checksum-MD5:${ARTIFACT_MD5_CHECKSUM}" --header "X-Checksum-Sha1:${ARTIFACT_SHA1_CHECKSUM}"  -u "admin:${ARTIFACTORY_API_KEY}" -v https://artifactory.example.com/artifactory/foo/

使用Invoke-RestMethod

$password = ConvertTo-SecureString -AsPlainText -Force -String '<api_key>'
$cred = New-Object Management.Automation.PSCredential ('admin', $password)

$ARTIFACT_SHA1_CHECKSUM=$(Get-FileHash -Algorithm SHA1 c:\bar.zip).Hash
$HEADERS = @{"X-Checksum-SHA1"=$ARTIFACT_SHA1_CHECKSUM;};

Invoke-RestMethod -Uri https://artifactory.example.com/artifactory/foo/bar.zip -Method PUT -InFile c:\bar.zip -cred $cred -Headers $HEADERS

将 SHA1 散列作为 headers 的一部分是可选的。

确保:

  • 使用 'PUT' 而不是 'POST'
  • 文件名必须是 URI 的一部分

Sha256 校验和 can't currently be uploaded, instead their calculation needs to be requested through the api.

$CONTENT = @{"repoKey"="nd-web-zips";"path"=$localFile} | ConvertTo-Json
# For some reason, the api doesn't like $cred, pass the api key in directly to the headers
$HEADERS2 = @{"X-JFrog-Art-Api"=$APIKEY}
Invoke-RestMethod -Uri https://artifactory.example.com/artifactory/api/checksum/sha256 -Method POST -Body $CONTENT -ContentType "application/json" -Headers $HEADERS2