Search code examples
pythonrequestartifactory

Corrupt xml is getting downloaded when downloading .xml file from artifactory using python


I am using below script to download all the xml files present in an artifactory directory (subfoldername contains various .xml files)

from artifactory import ArtifactoryPath
import shutil,os,requests

url="https://artifactory.xxx.com/artifactory/foldername/subfoldername/"
logdirectory='C:\\subfoldername'
if not os.path.exists(logdirectory):
            os.mkdir(logdirectory)
os.chdir(logdirectory)
xmlfiles = ArtifactoryPath(url,auth=(username, password))
for element in xmlfiles:
    print (element )
    xmlname=str(element).split("/")[-1]
    print(xmlname)
    with requests.get(element , auth=(username, password), stream=True) as r:
         with open(xmlname, 'wb') as f:
         shutil.copyfileobj(r.raw, f)

It is downloading the xml files with correct name but when i try to open the files it shows corrupted data. enter image description here

PS: I have also tried below method present on (https://pypi.org/project/artifactory/) but it also results in same corrupted xml using my code for path and authentication. Pasted below code for quick reference

from artifactory import ArtifactoryPath
path = ArtifactoryPath(
    "http://repo.jfrog.org/artifactory/distributions/org/apache/tomcat/apache-tomcat-7.0.11.tar.gz")

with path.open() as fd:
    with open("tomcat.tar.gz", "wb") as out:
        out.write(fd.read())

please suggest how can i download xml files without getting them corrupt


Solution

  • Something like this works:

    for element in xmlfiles:
        print(element)
        xmlname= str(element).split("/")[-1]
        print(xmlname)
        req = requests.get(element , auth=(username, password))
             with open(xmlname, 'wb') as f:
                f.write(req.content)
    

    WIP