【问题标题】:Uploading to Artifactory from a Windows Powershell script从 Windows Powershell 脚本上传到 Artifactory
【发布时间】:2016-03-28 15:45:53
【问题描述】:

我已经通过 WebClient 对象从 Artifactory (Generic Repo) 成功下载了一个文件。我在通过相同方法上传文件时遇到问题。我正在尝试找出通过 Powershell 上传到我们服务器的最简单方法。

请注意,此时不能选择安装其他实用程序,例如 Curl。我正在编写自动化脚本并且想坚持使用基本的 Windows 2008 r2 服务器,不安装其他实用程序,因为我不能指望它们在所有服务器上都存在。

如果有人有一个使用 Rest API 的示例脚本,那就完美了!

下载代码示例(有效):

$SOURCE = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_1.0.zip"  
$DESTINATION = ".\BF_1.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER,$AF_PWD)  
$WebClient.DownloadFile($SOURCE,$DESTINATION)  

这是上传代码的示例(不起作用):

$SOURCE = ".\BF_2.0.zip"  
$DESTINATION = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER, $AF_PWD)  
$URI = New-Object System.Uri($DESTINATION)  
$WebClient.UploadFile($URI,$SOURCE)  

这是我从上传中得到的错误:

Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (405) Method Not Allowed."  
At E:\transient\af_put.ps1:8 char:1  
+ $WebClient.UploadFile($URI,$SOURCE)  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException  
    + FullyQualifiedErrorId : WebException  

【问题讨论】:

  • 请提供您迄今为止编写的 PowerShell 示例,并突出显示您收到的任何错误。 SO 通常不会“从头开始”提供代码。
  • 刚刚添加到主要问题

标签: windows rest powershell webclient artifactory


【解决方案1】:

我尝试了 Invoke-WebRequest 选项并且能够让它工作:

$URI = New-Object System.Uri("https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip")  
$SOURCE = ".\BF_2.0.zip"  
$AF_USER = "user"  
$AF_PWD = ConvertTo-SecureString "password" -AsPlainText -Force  
$CREDS = New-Object System.Management.Automation.PSCredential ($AF_USER, $AF_PWD)  

Invoke-WebRequest -Uri $URI -InFile $SOURCE -Method Put -Credential $CREDS  

必须创建一个 PSCrendential 对象,这样它就不会提示输入用户密码。但除此之外,这完全符合我的需要。

【讨论】:

  • 酷!由于我让您走上正确的轨道,因此通常也接受以我的方式投赞成票。像你一样提供你自己的例子也是非常正确的方法。做得很好。
【解决方案2】:

问题的原因是 UploadFile 使用的 HTTP 方法。 UploadFile 默认使用 POST,而将文件上传到 Artifactory 则需要使用 PUT 方法。这就是为什么您会收到 405“Method Not Allowed”响应。

要解决此问题,请使用带有三个参数的 UploadFile 重载,如下所示: https://msdn.microsoft.com/en-us/library/ms144230(v=vs.110).aspx

所以正确版本的代码如下所示:

$SOURCE = ".\BF_2.0.zip"  
$DESTINATION = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER, $AF_PWD)  
$URI = New-Object System.Uri($DESTINATION)
$METHOD = "PUT"  
$WebClient.UploadFile($URI, $METHOD, $SOURCE)  

【讨论】:

  • 如何在URL中设置artifactory的属性?我试图在 URL 中传递查询字符串,但它没有设置属性。
【解决方案3】:

我手边没有 Artifactory,但您可能想尝试 Invoke-RestMethod PowerShell cmdlet,它在 PowerShell v3 及更高版本的框中提供。以下是如何执行此操作的示例。

您将需要凭据,并且根据他们的 REST 文档,我们可以使用 -CredentialInvoke-RestMethod 参数获得的类型的基本身份验证应该涵盖我们那里。

您还需要在请求中提供消息$bodyLook at the JSON sample here from their docs,然后编辑我给出的$body 作为起点。

$credential = Get-Credential
$body = @{action="Upload";param2="Data";param3="Data";} | ConvertTo-Json 
Invoke-RestMethod -uri "http://localhost:8080/artifactory/api/build" `
  -ContentType "application/json" -method POST -body $body -Credential

我必须说,这是我见过的更复杂的 REST API 示例之一,因此为了简化此操作,我将在机器上安装 curl 并使用 Fiddler 捕获成功上传文件的跟踪。为了让事情变得更简单,您还可以使用浏览器中的 Artifactory UI 上传文件,并简单地记录上传步骤​​的痕迹。然后,获取请求中的 JSON 并将其用作起点。

【讨论】:

  • 您可能还想尝试使用 Invoke-WebRequests -Infile 参数上传文件。
  • Invoke-WebRequest 成功了。用这个避免了所有的 API 东西,Artifactory 能够处理它。感谢您的提示/帮助。我对自动化的 Windows 方面非常粗糙/新手,我所做的其他一切都是在 Linux 中完成的。
【解决方案4】:

我的答案肯定是派生的,但当我使用 API 令牌从 powershell 向我的 Artifactory 存储库推送某些内容进行身份验证时,这对我有用:

$credential_bytes = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $api_token)
$credentials = [System.Convert]::ToBase64String($credential_bytes)
$credential_header = "Basic " + $credentials    
Invoke-WebRequest -Uri $artifactory_dest_url -InFile "my_file.zip" -Method Put -Headers @{"Authorization"="$credential_header"}

叹息。我很遗憾我正在使用 Powershell。我的生命在做什么?无论如何,它有效。继续前进。

对 FoxDeploy 和 Maclnos 的支持。

【讨论】:

    【解决方案5】:

    JFrog 知识库提供an example upload to Artifactory via PowerShell。此示例使用 Invoke-RestMethod 与 FoxDeploy 之前的答案一样,但使用 -InFile 参数和不同的内容类型,如下所示:

    Invoke-RestMethod -uri <complete URI to where the artifact will be in Artifactory>
      -Method Put -InFile <path of file to upload> -Credential <PS creds>
      -ContentType "multipart/form-data" -TimeoutSec <in seconds>
    

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2016-11-09
      • 2010-11-09
      • 2019-02-03
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多