# Upload packages to Nexus repository using python scripts

**URL:** https://community.sonatype.com/t/upload-packages-to-nexus-repository-using-python-scripts/9807
**Category:** Best Practices
**Tags:** help, nexus-repository, api
**Created:** [December 22, 2022, 8:36am UTC](https://community.sonatype.com/t/upload-packages-to-nexus-repository-using-python-scripts/9807 "2022-12-22T08:36:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pogacnik](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@pogacnik](https://community.sonatype.com/u/pogacnik)
#### Post date: [December 22, 2022, 8:36am UTC](https://community.sonatype.com/t/upload-packages-to-nexus-repository-using-python-scripts/9807/1 "2022-12-22T08:36:43Z")

</div>

Hello,

I am trying to upload packages (APT,YUM,PYPI,RAW,DOCKER) to nexus using python. I have only find 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://requests.post/)(‘[http://localhost:8081/service/rest/v1/components](http://localhost:8081/service/rest/v1/components)’, params=params, files=files, auth=(‘admin’, ‘admin123’))

---

<div class="post-metadata">

### Author: ![pogacnik](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@pogacnik](https://community.sonatype.com/u/pogacnik)
#### Post date: [January 12, 2023, 2:06pm UTC](https://community.sonatype.com/t/upload-packages-to-nexus-repository-using-python-scripts/9807/2 "2023-01-12T14:06:55Z")

</div>

Here is example for uploading raw packages with 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’](http://localhost:8081/service/rest/v1/components%E2%80%99), params=params, files=files, data=data, auth=(‘admin’, ‘admin123’))

I still have problem with apt and yum packages

---

<div class="post-metadata">

### Author: ![static](https://yyz1.discourse-cdn.com/flex047/user_avatar/community.sonatype.com/static/32/2612_2.png) [@static](https://community.sonatype.com/u/static)
#### Post date: [March 2, 2023, 6:21pm UTC](https://community.sonatype.com/t/upload-packages-to-nexus-repository-using-python-scripts/9807/3 "2023-03-02T18:21:00Z")

</div>

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](http://community.sonatype.com/t/uploading-packages-to-nexus3-using-python-script/10227) and had to do the following:

```auto
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.

---

<div class="post-metadata">

### Author: ![pogacnik](https://avatars.discourse-cdn.com/v4/letter/p/8dc957/32.png) [@pogacnik](https://community.sonatype.com/u/pogacnik)
#### Post date: [March 6, 2023, 7:09am UTC](https://community.sonatype.com/t/upload-packages-to-nexus-repository-using-python-scripts/9807/4 "2023-03-06T07:09:19Z")

</div>

> [@static](#):
>
> ng up with the actual package.tar.gz file in e.g. repository/hosted-raw/packages/raw I have a text file containing the

Hello,  
try using

> **[Client Challenge](https://pypi.org/project/nexus3-cli/)**

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

> **[thiagoconde-oss / Nexus 3 CLI · GitLab](https://gitlab.com/thiagoconde-oss/nexus3-cli)**
>
> Python CLI and bindings for Sonatype's Nexus OSS 3
