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개 답변
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
전문가
Kallu
답변함 9달 전
  • 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>
	)
)
답변함 9달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠