Unable to upload to Raw Repo using Powershell - Sonatype Nexus Repository - Sonatype Community

Unable to upload to Raw Repo using Powershell

post by devon.britton on Jan 7, 2021

Devon Britton

Hi there.

I’ve got a Powershell script to upload a specified file to a raw repo in Nexus OSS 3.28.0 but it’s not working.

Not sure why I’m getting the following error…I suspect it’s my “BodyLines” composition but I’m struggling to fix it.

Invoke-WebRequest : [ { “id” : “*”, “message” : “No assets found in upload” }, { “id” : “directory”, “message” : “Missing required component field ‘Directory’” } ]

Powershell is below:

# Check on https://bgstack15.wordpress.com/2020/06/22/nexuslib-ps1-powershell-library-for-uploading-files-to-nexus-raw-repository/ for an example

param(
    [Parameter(Mandatory=$true)][string]$fileLocation, #local file which needs to be uploaded to nexus
    [Parameter(Mandatory=$true)][string]$nexusRepository,
    [Parameter(Mandatory=$true)][string]$nexusFileName, #display name on Nexus repo
    [Parameter(Mandatory=$true)][string]$nexusDirectory, #Destination in Nexus where the file needs to be stored
    [Parameter(Mandatory=$true)][string]$nexusAccount,
    [Parameter(Mandatory=$true)][string]$nexusPassword,
    [Parameter(Mandatory=$true)][ValidateSet('PR','AC')][string]$nexusEnvironment
)

#Define Root uri
switch($nexusEnvironment){
    "AC"{$nexusRootUri = "https://nexus-acc.alm.<XXXX>.be"}
    "PR"{$nexusRootUri = "https://nexus.alm.<XXXX>.be"}
}

#Check if repository exists
$repos = Invoke-RestMethod -Method Get -Uri ("{0}/service/rest/v1/repositories" -f $nexusRootUri)
if($repos.name.Contains($nexusRepository))
{
    #variables
    $encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($nexusAccount):$($nexusPassword)"))
    $headers = @{ Authorization = "Basic $encodedCredentials"; "Content-Type" = "multipart/form-data" ; Accept = "application/json" }

$file = $fileLocation
    $FileContent = get-content($file)

Add-Type -AssemblyName "System.Web"
    $mimeType = [System.Web.MimeMapping]::GetMimeMapping($file)
    $boundary = [System.Guid]::NewGuid().ToString()

$nexusApiUri = "{0}/service/rest/v1/" -f $nexusRootUri
    $uploadUri = ("{0}components?repository={1}" -f $nexusApiUri, $nexusRepository)

#https://help.sonatype.com/repomanager3/rest-and-integration-api/components-api#ComponentsAPI-Raw for more information about the parameters to be sent to the API

$LF = "/r/n"

Try { $fileBin = [System.IO.File]::ReadAlltext($file) }
        Catch { Throw "Unable to read file $File. Aborted." }
        $bodyLines = (
            "--${boundary}",
            "Content-Disposition: form-data; name=`"raw.directory`"",
            "",
            "${nexusDirectory}",
            "--${boundary}",
            "Content-Disposition: form-data; name=`"raw.asset1`"; filename=`"${FileName}`"",
            "Content-Type: application/octet-stream",
            "",
            $fileBin,
            "",
            "--${boundary}",
            "Content-Disposition: form-data; name=`"raw.asset1.filename`"",
            "",
            "${nexusFileName}",
            "--${boundary}--",
            ""
        ) -join $LF

#execute
    $objectResponse = Invoke-WebRequest -Method POST -Uri ($uploadUri) -Headers $headers -Body $bodyLines -ContentType "multipart/form-data;boundary=`"$boundary`""
}else{
    write-error "Repository '$nexusRepository' does not exist!"
    exit 1
}

I’d really appreciate any assistance.

Kind Regards,

Devon Britton.