-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
64 lines (51 loc) · 1.66 KB
/
lambda_function.py
File metadata and controls
64 lines (51 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import boto3
import urllib.parse
s3 = boto3.client('s3')
def lambda_handler(event, context):
try:
record = event['Records'][0]
bucket = record['s3']['bucket']['name']
key = urllib.parse.unquote_plus(record['s3']['object']['key'])
print("NEW CODE RUNNING")
print(f"File uploaded: {key} in bucket: {bucket}")
# 🚫 Ignore folders (important)
if key.endswith('/'):
print("Folder detected. Skipping.")
return
# 🚫 Skip already processed files
if key.startswith(('images/', 'documents/', 'videos/', 'others/')):
print("File already organized. Skipping.")
return
# Extract file name
file_name = key.split('/')[-1].lower()
print(f"Detected file name: {file_name}")
# Decide folder
if file_name.endswith(('.jpg', '.jpeg', '.png')):
folder = 'images/'
elif file_name.endswith('.pdf'):
folder = 'documents/'
elif file_name.endswith(('.mp4', '.mov')):
folder = 'videos/'
else:
folder = 'others/'
new_key = folder + file_name
# Copy file
s3.copy_object(
Bucket=bucket,
CopySource={'Bucket': bucket, 'Key': key},
Key=new_key
)
# Delete original
s3.delete_object(
Bucket=bucket,
Key=key
)
print(f"Moved {key} → {new_key}")
return {
'statusCode': 200,
'body': json.dumps('File organized successfully!')
}
except Exception as e:
print(f"Error: {str(e)}")
raise e