我正在尝试将 Nexus 存储库中存在的目录下载到我的本地计算机。 我尝试过使用 nexus API,但我似乎无法将文件夹复制到本地计算机并让它保持与 nexus 存储库中显示的文件夹层次结构相同..
我需要它在我的电脑上具有与 Nexus 存储库中的文件夹相同的文件夹结构。
任何帮助将不胜感激:)
我尝试使用 GUI,但下载大小有限,而且我要下载的文件夹太大。
我还尝试制作一个使用nexus API下载文件的bash脚本,但它并没有保留与nexus上显示的相同的文件夹结构,它只是递归下载nexus目录中的所有文件并将它们放入一个大目录中本地目录。
我编写了这个简单的脚本,您可以使用它来下载整个存储库或仅下载您想要的一个特定目录。
首先,它向 Nexus API 发送请求,并从存储库中获取所有资产。迭代它们并下载资产文件(如果符合您的条件)
#!/bin/bash
USER=$1
PASSWORD=$2
REPO_URL="https://repo.test.com.ua"
REPO_NAME="repository_name"
OUTPUT_DIR="downloaded_from_nexus/" # '/' at the end is mandatory
REPO="$REPO_URL/repository/$REPO_NAME/" # '/' at the end is mandatory
DIRECTORY_TO_DOWNLOAD=$3
DOWNLOAD_ALL=1
if [[ -z "${USER}" ]] || [[ -z "${PASSWORD}" ]]; then
echo "Username or password were not passed"
exit 1
fi
if [[ -z "${DIRECTORY_TO_DOWNLOAD}" ]]; then
DOWNLOAD_ALL=1
echo "Downloading the whole repo"
else
DOWNLOAD_ALL=0
echo "Downloading only ${REPO}${DIRECTORY_TO_DOWNLOAD}"
fi
CONTINUATION_TOKEN=""
URLS_LIST=()
function download_assets() {
local TOKEN="$1"
local API_URL="${REPO_URL}/service/rest/v1/search/assets?repository=${REPO_NAME}"
if [[ ! -z "$TOKEN" ]] && [[ ! "$TOKEN" == "null" ]]; then
API_URL="${API_URL}&continuationToken=${TOKEN}"
fi
echo "Sending request to ${API_URL}" >&2
local assets;
assets=$(curl -s -f --user ${USER}:${PASSWORD} -X GET "${API_URL}")
res=$?
if [[ "$res" -ne 0 ]]; then
echo "Api request failure. Making another one with verbose:" >&2
curl -v --user ${USER}:${PASSWORD} -X GET "${API_URL}" >&2
exit $res
fi
while read item; do
url=$(echo "$item" | jq -r '.downloadUrl')
filename=$(basename $url)
structure=${url%$filename}
structure=${structure##*$REPO}
if [[ $DOWNLOAD_ALL -eq 0 ]] && [[ "${url}" != "${REPO}${DIRECTORY_TO_DOWNLOAD}"* ]]; then
# If that's not the directory we want - skip it
continue
fi
echo "Downloading ${OUTPUT_DIR}${structure}${filename}" >&2
mkdir -p "${OUTPUT_DIR}$structure"
wget --user ${USER} --password ${PASSWORD} ${url} -O "${OUTPUT_DIR}${structure}${filename}"
done <<< "$(echo "$assets" | jq -c '.items[]')"
# Extract continuation token from the response
TOKEN=$(echo "$assets" | jq -r '.continuationToken')
echo "$TOKEN"
}
while true; do
TOKEN=$(download_assets "${CONTINUATION_TOKEN}")
# Break the loop if there's no continuation token
if [[ "$TOKEN" == "null" ]]; then
break
fi
CONTINUATION_TOKEN=${TOKEN}
done
例如将其保存,名称为
nexus_downloader.sh
。之后根据您的需要在脚本中更改 REPO_URL 和 REPO_NAME 变量。./nexus_downloader.sh username_from_nexus password_from_nexus directory_to_download
OUTPUT_DIR
我重写了它 - 现在它不需要任何中间文件来存储链接。它只是下载您需要的内容