Load Nexus Images
python
import requests
import json
## Nexus 服务器的 URL 和凭据
NEXUS_URL = "https://nexus.foobar.work"
NEXUS_USERNAME = "foobar"
NEXUS_PASSWORD = "foobar"
## 要查询的仓库名称
REPOSITORY_NAME = "foobar-test"
## 输出文件名
OUTPUT_FILE = "nexus_images-test.json"
def list_nexus_images():
"""
获取 Nexus 仓库中的所有镜像
"""
url = f"{NEXUS_URL}/service/rest/v1/components?repository={REPOSITORY_NAME}"
images = []
continuation_token = None
total_items = 0
while True:
# 如果有 continuation_token,则添加到 URL 中
params = {}
if continuation_token:
params["continuationToken"] = continuation_token
# 发送 GET 请求并获取响应
response = requests.get(url, auth=(NEXUS_USERNAME, NEXUS_PASSWORD), params=params, timeout=600)
response.raise_for_status()
data = response.json()
# 将当前页的镜像添加到列表中
images.extend(data["items"])
total_items += len(data["items"])
# 更新进度条
# 检查是否还有更多页
continuation_token = data.get("continuationToken")
if not continuation_token:
break
return images
if __name__ == "__main__":
# 创建进度条
all_images = list_nexus_images()
# 将镜像信息写入到文件中
with open(OUTPUT_FILE, "w", encoding="utf-8") as f:
json.dump(all_images, f, indent=2)
# 关闭进度条
print(f"已从 Nexus 仓库中获取 {len(all_images)} 个镜像,并保存到 {OUTPUT_FILE} 文件中。")