find all S3 objects without tags

0

Hello,

Considering the following Windows 10 bash command from a .bat file (using AWS CLI), that finds all the S3 keys for specified (tagKey,tagValue), could you please specify the command to get all the S3 keys without any tag ?

for /f "tokens=*" %%a in ('aws s3api list-objects-v2 --bucket <bucket> --prefix <prefix> --no-cli-auto-prompt --query Contents[*].[Key] --output text') do (
	for /f "tokens=*" %%b in ('aws s3api get-object-tagging --bucket <bucket> --key %%a --no-cli-auto-prompt --query TagSet[?"Key=='<tagKey>'&&Value=='<tagValue>'"].Key --output text') do (
		@echo %%a >> <file>
	)
)

Thank you,
Mihai ADAM

2 Antworten
0

If you want to test if object doesn't have any tags you can test if TagSet[0] is null.

% aws s3api get-object-tagging --bucket BUCKET --key OBJECT
{
    "TagSet": []
}
% aws s3api get-object-tagging --bucket BUCKET --key OBJECT --query 'TagSet[0]==`null`'
true

As your original script was listing all the tag, but now you'd only want to list object name when there is no tags, you can replace inner for -loop with simple if -cmd than will print object name (%%a) when above cmd return true. Not being Windows professional I will leave the syntax of script for you as an exercise ;-)

profile picture
EXPERTE
Kallu
beantwortet vor 9 Monaten
  • Hello,

    Thank you for your useful hint.

    But, in Windows, the only way I know to run AWS CLI command inside other CMD command (including if -cmd) is that FOR /F (there is no other way to make command substitution, to use the output of AWS CLI environment inside WIN CMD environment). So, the solution that I found is presented below.

    for /f "tokens=" %%a in ('aws s3api list-objects-v2 --bucket <bucket> --prefix <prefix> --no-cli-auto-prompt --query Contents[].[Key] --output text') do ( for /f "tokens=*" %%b in ('aws s3api get-object-tagging --bucket <bucket> --key %%a --no-cli-auto-prompt --query "TagSet[0]==null" --output text') do ( if %%b==True @echo %%a >> <file> ) ) Now that I have the command, I could see that the execution time is very long, as it includes one call for each in hundreds keys. Does it exist any way to shorten the execution time ?

    Thank you, Mihai ADAM

0

Please see the more clear syntax

for /f "tokens=*" %%a in ('aws s3api list-objects-v2 --bucket <bucket> --prefix <prefix> --no-cli-auto-prompt --query Contents[*].[Key] --output text') do (
	for /f "tokens=*" %%b in ('aws s3api get-object-tagging --bucket <bucket> --key %%a --no-cli-auto-prompt --query "TagSet[0]==null" --output text') do (
		if %%b==True @echo %%a >> <file>
	)
)
beantwortet vor 9 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen