Upload packages to Nexus repository using python scripts - Sonatype Nexus Repository / Best Practices - Sonatype Community

Upload packages to Nexus repository using python scripts

Hello,

I am trying to upload packages (APT,YUM,PYPI,RAW,DOCKER) to nexus using python. I have only found a way to upload maven packages. I would like to upload some other formats of packages using similar scripts as below example for maven.

import requests

params = (
    ('repository', 'maven-releases'),
)

files = {
    'maven2.groupId': (None, 'com.example'),
    'maven2.artifactId': (None, 'test-app'),
    'maven2.version': (None, '1.0.0'),
    'maven2.asset1': ('example.json', open('example.json', 'rb')),
    'maven2.asset1.extension': (None, 'json'),
}

response = requests.post('http://localhost:8081/service/rest/v1/components', params=params, files=files, auth=('admin', 'admin123'))

Here is an example for uploading raw packages with a python script.

import requests

params = (
    ('repository', 'hosted-raw'),  # repository name
)

files = {
    'raw.asset1': ('package.tar.gz')
}

data = {
    'raw.directory': ('packages/raw/'),
    'raw.asset1.filename': ('package.tar.gz')
}

response = requests.post('http://localhost:8081/service/rest/v1/components', params=params, files=files, data=data, auth=('admin', 'admin123'))

I still have a problem with APT and YUM packages.


Thank you for this Niko. I used your code as a reference, but instead of ending up with the actual package.tar.gz file in e.g. repository/hosted-raw/packages/raw, I have a text file containing the path & filename. Can you confirm your package is uploaded successfully or if you also just have a text file? Thanks again and good work.

Edit:

I came across this post and had to do the following:

files = {
    'raw.asset1': (myfile, open(myfile, 'rb'))
}

This got mine working, but only for text-based files (meaning now it’s the proper contents inside, rather than just the path/filename.ext inside). I’m getting <Response [500]> for binary files. I’m still pretty new to python but I suspect it’s the way I’m opening the file, or maybe open isn’t the function I need. I’ll update this thread if I figure it out.


Hello,

try using:

# [Your suggested code here] 

I also found the code, you might find it useful: