You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决Artifactory中CURL执行AQL查询含10+制品文件夹的语法错误

Artifactory 文件夹制品保留策略实现(仅保留最新10个)

问题定位与AQL语法修正

你的AQL报错核心原因是无法在filter函数中直接引用children.items.@count,且嵌套的children查询不符合AQL规范。正确的思路是通过聚合统计每个文件夹下的文件数量,再筛选出超过阈值的文件夹。

修正后的文件夹查询逻辑:

execute_aql() {
    # 查询所有子文件数量超过10的文件夹
    curl_response=$(curl -s -u "admin:$API_KEY" -X POST "$ARTIFACTORY_URL/artifactory/api/search/aql" \
        -H "Content-Type: text/plain" \
        -d 'items.find(
            {"type": "file", "repo": "'"$REPO_NAME"'"}
        ).include("path")
        .aggregate($group: {path: "$path"}, $count: {"$sum": 1})
        .filter({"$count": {"$gt": 10}})')

    echo "$curl_response"
}

该查询先遍历仓库所有文件,按文件夹路径分组统计数量,最终筛选出文件数>10的目标文件夹。

完整保留策略实现(查询+删除最旧制品)

以下是包含按时间排序、删除最旧制品的完整脚本,实现每个文件夹仅保留最新10个制品的需求:

#!/bin/bash

# 配置参数
ARTIFACTORY_URL="http://your-artifactory-instance-url"
REPO_NAME="HLRP_GENERIC_DEV"
API_KEY="your-admin-api-key"
RETAIN_COUNT=10

# 获取超过保留阈值的文件夹列表
get_over_limit_folders() {
    curl -s -u "admin:$API_KEY" -X POST "$ARTIFACTORY_URL/artifactory/api/search/aql" \
        -H "Content-Type: text/plain" \
        -d 'items.find(
            {"type": "file", "repo": "'"$REPO_NAME"'"}
        ).include("path")
        .aggregate($group: {path: "$path"}, $count: {"$sum": 1})
        .filter({"$count": {"$gt": '"$RETAIN_COUNT"'}})' | jq -r '.results[].path'
}

# 清理指定文件夹下的多余旧制品
cleanup_folder() {
    local folder_path="$1"
    # 查询文件夹内所有文件,按创建时间升序排序,提取需删除的旧文件路径
    local files_to_delete=$(curl -s -u "admin:$API_KEY" -X POST "$ARTIFACTORY_URL/artifactory/api/search/aql" \
        -H "Content-Type: text/plain" \
        -d 'items.find(
            {"type": "file", "repo": "'"$REPO_NAME"'", "path": "'"$folder_path"'"}
        ).include("name","created")
        .sort({"$asc": "created"})' | jq -r ".results[:-($RETAIN_COUNT)] | .[].name" | sed "s|^|$folder_path/|")

    # 执行删除操作
    if [ -n "$files_to_delete" ]; then
        echo "Cleaning up folder: $folder_path"
        echo "$files_to_delete" | while read -r file_path; do
            echo "Deleting artifact: $file_path"
            curl -s -u "admin:$API_KEY" -X DELETE "$ARTIFACTORY_URL/artifactory/$REPO_NAME/$file_path"
        done
    fi
}

# 主执行逻辑
main() {
    local target_folders=$(get_over_limit_folders)
    if [ -n "$target_folders" ]; then
        echo "Found folders exceeding retention limit:"
        echo "$target_folders"
        echo "Starting cleanup process..."
        echo "$target_folders" | while read -r folder; do
            cleanup_folder "$folder"
        done
    else
        echo "No folders exceed the retention limit of $RETAIN_COUNT artifacts."
    fi
}

main

关键注意事项

  • 确保环境已安装jq工具(用于解析JSON响应,可通过系统包管理器安装)
  • 测试阶段建议先注释删除命令,确认查询出的文件为正确待删对象
  • 需保证操作账号拥有目标仓库的删除权限
  • 如需按修改时间排序,可将sort({"$asc": "created"})替换为sort({"$asc": "modified"})

内容的提问来源于stack exchange,提问作者Elydasian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:03:14