Get Filtered Repositories from ECR #2602
Replies: 1 comment 1 reply
-
Hi @ayeletdeRoos , Thanks for reaching out.
I just want to clarify that --query that the CLI has is a client side flitering rule and not a service side filter. So it will not "retrieve only repositories based on a specific prefix". This command will retrieve all repositories and the CLI will filter those on the client side. The SDK's do not have a "Baked-in" client side filter like --query that the CLI has. The CLI has a lot of courtesy functionalities and ease of use customizations, partly because its not as easy to do filtering on JSON from a unix context. If service side filtering does not exist for a specific operation (ECR DescribeRepositories does not support service side filters) you'll have to write your own Go code to filter based on the specific filtering you need for example: package main
import (
"context"
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
client := ecr.NewFromConfig(cfg)
result, err := client.DescribeRepositories(context.TODO(), &ecr.DescribeRepositoriesInput{})
if err != nil {
panic(err)
}
prefix := "prd/test"
for _, repo := range result.Repositories {
if strings.HasPrefix(*repo.RepositoryName, prefix) {
fmt.Println("Repository Name:", *repo.RepositoryName)
}
}
} Regarding
I'm a bit confused about this question. I'm not sure how the SDK is different than the CLI in this case. In your CLI command you also specify the exact image tag. The answer here is the same, using the CLI, you are using jq to achieve some filtering functionality. When using the SDK, you need to use that SDK's respective programming language to perform whichever filtering logic you need, in this case you'll need to use Golang to iterate over the results from the BatchGetImage response and then parse the raw json found on the response like you do using jq. package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/aws/aws-sdk-go-v2/service/ecr/types"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic(err)
}
client := ecr.NewFromConfig(cfg)
input := &ecr.BatchGetImageInput{
RepositoryName: aws.String("prd/test-default"),
ImageIds: []types.ImageIdentifier{
{
ImageTag: aws.String("3bf5abb"),
},
},
AcceptedMediaTypes: []string{"application/vnd.docker.distribution.manifest.v1+json"},
}
result, err := client.BatchGetImage(context.TODO(), input)
if err != nil {
panic(err)
}
}
// iterate over result.Images and apply whichever filtering logic you need. If you didn't know, all of the SDKs (and CLI) are code generated from the API model of each AWS service in which they interact with. If the ECR operation does not have service side filtering supported, the SDK wont have functionality to do service side filtering and you'll have to do that on the client side using the programming language of that specific SDK. Thanks, |
Beta Was this translation helpful? Give feedback.
-
Hi everyone,
I'm currently working on integrating with Amazon Elastic Container Registry (ECR) using the AWS SDK for Go v2 (aws-sdk-go-v2). I'm looking for ways to achieve specific functionalities similar to what I can do with the AWS CLI for ECR.
Filtered Repositories: I'm trying to fetch repositories based on a prefix, similar to the starts_with filtering option available in the AWS CLI. For instance, using the AWS CLI, I can run:
aws ecr describe-repositories --query 'repositories[?starts_with(repositoryName,
prd/test)]' --no-cli-pager
Is there a similar capability or workaround in the aws-sdk-go-v2 for fetching repositories based on a prefix?
From what I searched I saw there's only a way to specify exactly the repository list you wish to get
Listing Images with Labels: Additionally, I'm trying to fetch all images in a repository, including their labels. With the AWS CLI, I can achieve this by running:
aws ecr batch-get-image --repository-name prd/test-default --image-ids imageTag=3bf5abb --accepted-media-types "application/vnd.docker.distribution.manifest.v1+json" --output json | jq -r '.images[].imageManifest' | jq -r '.history[0].v1Compatibility' | jq -r '.conf'
Is there a way to accomplish this with the aws-sdk-go-v2? Specifically, I'm interested in fetching all images in a repository, retrieving their labels, and possibly limiting the result to the 10 most recent tags.
From what I searched I saw there's only a way to specify exactly the image tags you wish to get
I'd appreciate any insights or suggestions on how to achieve these functionalities using the aws-sdk-go-v2 or any alternative approaches that you might suggest.
Thanks in advance for your help!
Beta Was this translation helpful? Give feedback.
All reactions