File size: 1,502 Bytes
3bb9361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import boto3
# AWS credentials (if not set in environment variables or AWS CLI config)
from dotenv import load_dotenv
import os
import sys

from utils import doc_processing

# Load .env file
load_dotenv()
# Access variables
AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
print("AWS_ACCESS_KEY_ID",AWS_ACCESS_KEY_ID)
print("AWS_SECRET_ACCESS_KEY",AWS_SECRET_ACCESS_KEY)
# Initialize S3 client

class s3_client:
    def __init__(self):
        self.aws_access_key_id = AWS_ACCESS_KEY_ID
        self.aws_secret_access_key = AWS_SECRET_ACCESS_KEY

    def initialize(self):
        return boto3.client(
                's3',
                aws_access_key_id=self.aws_access_key_id,
                aws_secret_access_key=self.aws_secret_access_key
                            )
    
    def upload_file(self,local_file_path, bucket_name,folder_name,file_name):
        try:
            client = self.initialize()
            client.upload_file(local_file_path, bucket_name, f"{folder_name}/{file_name}")
            print(f"File uploaded successfully to {bucket_name}/{folder_name}{file_name}")
            url = f"https://edgekycdocs.s3.eu-north-1.amazonaws.com/{folder_name}/{file_name}"
            print("file url",url)
            return {"status": 200, "message":"file uploaded successfully" , "url" : url}
        except Exception as e:
            print("Error uploading file:", e)
            return {"status": 400, "message":e}