从 Nexus 存储库下载目录

问题描述 投票:0回答:1

我正在尝试将 Nexus 存储库中存在的目录下载到我的本地计算机。 我尝试过使用 nexus API,但我似乎无法将文件夹复制到本地计算机并让它保持与 nexus 存储库中显示的文件夹层次结构相同..

我需要它在我的电脑上具有与 Nexus 存储库中的文件夹相同的文件夹结构。

任何帮助将不胜感激:)

我尝试使用 GUI,但下载大小有限,而且我要下载的文件夹太大。

我还尝试制作一个使用nexus API下载文件的bash脚本,但它并没有保留与nexus上显示的相同的文件夹结构,它只是递归下载nexus目录中的所有文件并将它们放入一个大目录中本地目录。

repository devops nexus nexus3
1个回答
0
投票

我编写了这个简单的脚本,您可以使用它来下载整个存储库或仅下载您想要的一个特定目录。
首先,它向 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

前两个参数是强制性的,它们用于访问您的 Nexus 存储库。最后一个参数是可选的。如果通过 - 脚本将仅从您提供的目录下载所有文件。如果未通过 - 脚本将下载整个存储库
默认情况下,它将所有数据下载到单独的目录中
OUTPUT_DIR

脚本使用依赖项

  1. 卷曲
  2. wget
  3. jq

我重写了它 - 现在它不需要任何中间文件来存储链接。它只是下载您需要的内容

© www.soinside.com 2019 - 2024. All rights reserved.