Hi All,
I am totally new to Python (and coding in general). I am working on a Lambda setup that uses a Python Script to tag items in an AWS s3 bucket based on a csv file being uploaded to a particular directory. I was able to get that working with help from someone i know as far as the python code goes. The one thing i found recently is that if a file is specified to be tagged in the csv file but does not exist in the bucket the process stops and logs errors. It does not go any further. I am hoping there is a way to tell the script to proceed if a file is not found, rather than error out. However, not really having any coding knowledge i was wondering if someone might be able to point me in a direction of what needs to be done?
The script that is working, minus erroring out if file is not found, is below for reference. Any help/input would be appreciated.
Thanks
import csv,boto3,re
# Setting tag Key Name.
tagKeyName = 'Purge'
#----------------------- main -----------------------#
#def main_script(bucketName, fileKey, s3RootUrl):
def main_script(bucketName, fileKey):
# Get data from csv
s3Resource = boto3.resource('s3')
s3Object = s3Resource.Object(bucketName, fileKey)
data = s3Object.get()['Body'].read().decode('utf-8').splitlines()
# Start reading
lines = csv.reader(data)
headers = next(lines)
# Parse files. Tag if not tagged
for line in lines:
# Remove url
#key = line[0].replace(s3RootUrl,"")
key = re.sub(r'https://.*.com/','',line[0])
print(f"Checking key {key}")
# Tag object
response = check_tag(bucketName, key, tagKeyName)
if response == "NotFound":
print(f"Tagging key {key}")
tag_bucket(bucketName,key,tagKeyName)
else:
print(f"Key {key} already tagged")
#----------------------- functions -----------------------#
def check_tag(bucket, key, tagKey):
tagsFiltered = s3.get_object_tagging(
Bucket = bucket,
Key = key
)["TagSet"]
for tag in tagsFiltered:
if tag['Key'] == tagKey:
return "Found"
return "NotFound"
def tag_bucket(bucketName,key,tagKey):
s3.put_object_tagging(
Bucket=bucketName,
Key=key,
Tagging={
'TagSet': [
{
'Key': tagKey,
'Value': 'True'
}
]
}
)
#----------------------- handlers -----------------------#
# Connecting to boto3 client
s3 = boto3.client('s3')
def lambda_handler(event, context):
# Dynamically find file and bucket from event
bucketName = event['Records'][0]['s3']['bucket']['name']
fileKey = event['Records'][0]['s3']['object']['key']
#s3RootUrl = f"https://{bucketName}.s3.amazonaws.com/"
#main_script(bucketName, fileKey, s3RootUrl)
main_script(bucketName, fileKey)