看了artifactory的文档以及python有关artifactory的第三方库,发现只有上传文件的方法,但是要将以前的仓库迁移到artifactory上手动上传文件太困难,于是想写一个递归上传文件夹的方法,以下是代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
from pyartifactory import Artifactory
"""
上传文件夹步骤:
参数 本地路径+文件夹名称 远程路径
eg:E:/pycharm/get_commitNum_from_gitlab/Artifactory/ep40 EP40/
1.远程路径创建 本地文件夹名称的文件夹
2.进入本地文件夹获取文件列表 然后list=路径加文件名称
"""
import os
art = Artifactory(url='', auth=('',''),api_version = 1)
def upload(LOCAL_DIRECTORY_PATH,ARTIFACT_PATH_IN_ARTIFACTORY):
artifact = art.artifacts.deploy(LOCAL_DIRECTORY_PATH, ARTIFACT_PATH_IN_ARTIFACTORY)
def uploadDir1(localdir, remotedir):
dirname = 'F:/APP'.split('/')[-1]
remotedir=remotedir+'/'+dirname
create_file(remotedir)
uploadDir2(localdir,remotedir)
def create_file(filepath):
res = requests.put(''%(filepath),auth=('',''))
print(res.text)
def uploadDir2(localdir, remotedir):
if not os.path.isdir(localdir):
return
for file in os.listdir(localdir):
#本地文件路径带文件名
src = os.path.join(localdir, file)
# 远程文件路径带文件名
# filepath = os.path.join(remotedir, file)
filepath=remotedir+'/'+file
if os.path.isfile(src):
upload(src,filepath)
elif os.path.isdir(src):
try:
create_file(filepath)
except:
print(filepath,'文件夹创建失败')
uploadDir2(src, filepath)
uploadDir1('F:/APP','EP11')
|