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/"
The first thing that jumps out at me is that your bash script is using a PUT
while your python script is using POST
.
The only other potential problem I see is the way you're constructing the URLs is radically different.
In the bash script the URL you create ends up being
https://artifactory.company.com/artifactory/libs-release-local/com/company/app/app-comp/1.0.0//app.war
In the python script you're using
https://artifactory.company.com/artifactory/yum-local/app-1.0.0-snapshot.el6.noarch.rpm
Also, it's important to note that yum-local
is hard-coded into every URL you generate.
That aside, your get_md5
and get_sha1
functions could be more simply written because file objects are iterable, e.g.,
with open(fin, 'rb') as f:
for chunk in f:
md5.update(chunk)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With