Lambda function for S3 tagging with multiple tags

0

Hi Guys,

I am trying put multiple tags on objects when they are copied on a particular folder, it works perfectly fine with single tags or multiple tags if I specify them explicitly, how to do it using a loop.

Below works perfectly fine.

response = s3.put_object_tagging(
Bucket='mybucket',
Key='Test/Test_test_xyz.csv',
Tagging={
'TagSet': [
{
'Key': 'key1',
'Value': 'value1'
},
{
'Key': 'key2',
'Value': 'value2'
},
]
}
)

However I want to achieve it using a loop, Below is what I am trying, any help would be greatly appreciated.

import boto3
s3 = boto3.client("s3")
all_objects = s3.list_objects(Bucket = 'Test')

tag={'TEST':{'test': {'key1': 'value1' , 'key2': 'value2'}}}

j=1

for i in all_objects['Contents']:
x=i['Key']
try:
y=(x.split('.')[0])
if y:
z=(y.split('/')[-1])
if z:
p=(z.split('')[0])
q=(z.split('
')[1])
if p == 'Test':
for s, t in tag[p][q].items():
# Put S3 object tagging
response = s3.put_object_tagging(
Bucket='Test',
Key=x,
Tagging={
'TagSet': [
{
'Key': s,
'Value': t
},
]
}
)
except IndexError:
pass

Thanks
Sid

sid1987
질문됨 5년 전1241회 조회
1개 답변
0

Got it, you just need to create a list of dictionaries and assign it to tag_set

sid1987
답변함 5년 전

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

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

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

관련 콘텐츠