Search code examples
pythonpython-requestsartifactory

Upload artifacts to artifactory with python


I am trying to upload artifacts to an artfactory repo with requests, but I am getting 405 errors. I have a working bash script that achieves this goal, but I really need a python implementation.

python

import os
import hashlib
import requests
from requests.auth import HTTPBasicAuth

username = 'me'
password = 'secrets'


target_file = '/home/me/app-1.0.0-snapshot.el6.noarch.rpm'

artifactory_url = 'https://artifactory.company.com/artifactory'

def get_md5(fin):
    md5 = hashlib.md5()
    with open(fin, 'rb') as f:
        for chunk in iter(lambda: f.read(8192), ''):
            md5.update(chunk)
    return md5.hexdigest()

def get_sha1(fin):
    sha1 = hashlib.sha1()
    with open(fin, 'rb') as f:
        for chunk in iter(lambda: f.read(8192), ''):
            sha1.update(chunk)
    return sha1.hexdigest()


def upload(fin):
    base_file_name = os.path.basename(fin)
    md5hash = get_md5(fin)
    sha1hash = get_sha1(fin)
    headers = {"X-Checksum-Md5": md5hash, "X-Checksum-Sha1": sha1hash}
    r = requests.post("{0}/{1}/{2}".format(artifactory_url, "yum-local", base_file_name),auth=(username,password), headers=headers, verify=False, data=open(fin, 'rb'))
    return r    

bash

art_url="https://artifactory.company.com/artifactory"
user="user"
pass="password"


function upload {
    local_file_path=$1
    target_folder=$2
    if [ ! -f "$local_file_path" ]; then
    echo "ERROR: local file $local_file_path does not exists!"
    exit 1
    fi

    which md5sum || exit $?
    which sha1sum || exit $?

    md5Value="`md5sum "$local_file_path"`"
    md5Value="${md5Value:0:32}"
    sha1Value="`sha1sum "$local_file_path"`"
    sha1Value="${sha1Value:0:40}"
    fileName="`basename "$local_file_path"`"

    echo $md5Value $sha1Value $local_file_path

    echo "INFO: Uploading $local_file_path to $target_folder/$fileName"
    curl -i  -k -X PUT -u $user:$pass \
    -H "X-Checksum-Md5: $md5Value" \
    -H "X-Checksum-Sha1: $sha1Value" \
    -T "$local_file_path" \
    ${art_url}/"$target_folder/$fileName"
           }

upload "/projects/app.war" "libs-release-local/com/company/app/app-comp/1.0.0/"

Solution

  • Error 405 stands for using wrong HTTP method. According to the documentation, artifact deployment should be done using PUT.

    That means that you can't use requests.post, but need to use requests.put instead.