jfrog AQL 查询指定仓库的镜像信息

该博客展示了如何利用JFrog Artifactory的API和AQL(Artifactory Query Language)查询指定Docker仓库中的镜像详细信息。通过设置URL、认证和过滤参数,代码成功获取了仓库中所有镜像的路径、名称、类型、大小等属性,并以JSON格式输出。验证结果显示与管理界面信息一致。
摘要由CSDN通过智能技术生成

 在jfrog 获取所有仓库 博客中,我们已经拿到了所有关于docker仓库的信息,接着我们使用AQL查询指定仓库中的镜像信息。

代码如下:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/jfrog/jfrog-client-go/artifactory"
	"github.com/jfrog/jfrog-client-go/artifactory/auth"
	"github.com/jfrog/jfrog-client-go/artifactory/services"
	"io/ioutil"

	"github.com/jfrog/jfrog-client-go/config"
	"github.com/jfrog/jfrog-client-go/utils/log"
	"os"
	"time"
)

type Result struct {
	Repo string `json:"repo"`
	Path string `json:"path"`
	Name string `json:"name"`
	Type string `json:"type"`
	Size int    `json:"size"`
	Created time.Time `json:"created"`
	Created_by string `json:"created_by"`
	Modified string `json:"modified"`
	Modified_by string `json:"modified_by"`
	Updated time.Time `json:"updated"`
}

type Range struct {
	Start_pos int `json:"start_pos"`
	End_pos int `json:"end_pos"`
	Total int `json:"total"`
}

type ImagesInfo struct {
	Res[] Result `json:"results"`
	Ran Range `json:"range"`
}

func main() {
	var file *os.File
	log.SetLogger(log.NewLogger(log.INFO, file))
	rtDetails := auth.NewArtifactoryDetails()
	rtDetails.SetUrl("http://localhost:8081/artifactory/")
	rtDetails.SetUser("admin")
	rtDetails.SetPassword("password")

	serviceConfig, err := config.NewConfigBuilder().
		SetServiceDetails(rtDetails).
		// Optionally overwrite the default HTTP timeout, which is set to 30 seconds.
		SetHttpTimeout(180 * time.Second).
		// Optionally overwrite the default HTTP retries, which is set to 3.
		SetHttpRetries(8).
		Build()
	if err != nil {
		fmt.Println(err)
	}
	rtManager, err := artifactory.New(serviceConfig)
	params := services.NewRepositoriesFilterParams()
	params.PackageType = "Docker"
	params.RepoType = "LOCAL"
	reposDetail, err := rtManager.GetAllRepositoriesFiltered(params)


	if err != nil {
		return
	}
	for _, v := range *reposDetail {
		log.Output(v)

		str := fmt.Sprintf("items.find({\"repo\": \"%s\"})", v.Key)
		reader, err := rtManager.Aql(str)
		if nil != err{
			continue
		}
		content, err := ioutil.ReadAll(reader)
		fmt.Println(string(content))

		var imgInfo ImagesInfo
		json.Unmarshal(content, &imgInfo)

		for i := 0; i < imgInfo.Ran.Total; i++ {
			data , _ :=json.Marshal(imgInfo.Res[i])
			fmt.Println(string(data))
		}
	}

}

ImagesInfo :将json格式的数据转换成这个对象

结果如下:

GOROOT=D:\go #gosetup
GOPATH=D:\go_pkg #gosetup
D:\go\bin\go.exe build -o C:\Users\Administrator\AppData\Local\Temp\___1go_build_main_go.exe D:\test\go\jfrog\main.go #gosetup
C:\Users\Administrator\AppData\Local\Temp\___1go_build_main_go.exe #gosetup
{hello_world  LOCAL  http://192.168.0.105:8081/artifactory/hello_world Docker}

{
"results" : [ {
  "repo" : "hello_world",
  "path" : ".",
  "name" : "repository.catalog",
  "type" : "file",
  "size" : 41,
  "created" : "2022-04-22T20:55:45.581+08:00",
  "created_by" : "admin",
  "modified" : "2022-04-22T20:55:45.558+08:00",
  "modified_by" : "admin",
  "updated" : "2022-04-22T20:55:45.592+08:00"
},{
  "repo" : "hello_world",
  "path" : "ubuntu/v2",
  "name" : "manifest.json",
  "type" : "file",
  "size" : 529,
  "created" : "2022-04-22T20:55:45.497+08:00",
  "created_by" : "admin",
  "modified" : "2022-04-22T20:55:45.473+08:00",
  "modified_by" : "admin",
  "updated" : "2022-04-22T20:55:45.498+08:00"
},{
  "repo" : "hello_world",
  "path" : "ubuntu/v2",
  "name" : "sha256__4d32b49e2995210e8937f0898327f196d3fcc52486f0be920e8b2d65f150a7ab",
  "type" : "file",
  "size" : 28565909,
  "created" : "2022-04-22T20:55:44.959+08:00",
  "created_by" : "admin",
  "modified" : "2022-04-22T20:55:35.008+08:00",
  "modified_by" : "admin",
  "updated" : "2022-04-22T20:55:44.964+08:00"
},{
  "repo" : "hello_world",
  "path" : "ubuntu/v2",
  "name" : "sha256__ff0fea8310f3957d9b1e6ba494f3e4b63cb348c76160c6c15578e65995ffaa87",
  "type" : "file",
  "size" : 1461,
  "created" : "2022-04-22T20:55:45.341+08:00",
  "created_by" : "admin",
  "modified" : "2022-04-22T20:55:45.330+08:00",
  "modified_by" : "admin",
  "updated" : "2022-04-22T20:55:45.342+08:00"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 4,
  "total" : 4
}
}

{"repo":"hello_world","path":".","name":"repository.catalog","type":"file","size":41,"created":"2022-04-22T20:55:45.581+08:00","created_by":"admin","modified":"2022-04-22T20:55:45.558+08:00","modified_by":"admin","updated":"2022-04-22T20:55:45.592+08:00"}
{"repo":"hello_world","path":"ubuntu/v2","name":"manifest.json","type":"file","size":529,"created":"2022-04-22T20:55:45.497+08:00","created_by":"admin","modified":"2022-04-22T20:55:45.473+08:00","modified_by":"admin","updated":"2022-04-22T20:55:45.498+08:00"}
{"repo":"hello_world","path":"ubuntu/v2","name":"sha256__4d32b49e2995210e8937f0898327f196d3fcc52486f0be920e8b2d65f150a7ab","type":"file","size":28565909,"created":"2022-04-22T20:55:44.959+08:00","created_by":"admin","modified":"2022-04-22T20:55:35.008+08:00","modified_by":"admin","updated":"2022-04-22T20:55:44.964+08:00"}
{"repo":"hello_world","path":"ubuntu/v2","name":"sha256__ff0fea8310f3957d9b1e6ba494f3e4b63cb348c76160c6c15578e65995ffaa87","type":"file","size":1461,"created":"2022-04-22T20:55:45.341+08:00","created_by":"admin","modified":"2022-04-22T20:55:45.330+08:00","modified_by":"admin","updated":"2022-04-22T20:55:45.342+08:00"}

Process finished with the exit code 0

我们登录管理界面看看是否一致:

 是一致的

我们用curl来操作时,需要注意header需要设置content-type、请求方法、请求体之类的设置

例如:

 curl -u admin:password  http://lcoalhost:8081/artifactory/api/search/aql -X POST -H "Content-Type: text/plain" -d "items.find({\"repo\": \"hello_world\"})"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值