From 5bc302fc75b2f61a19ddca72b3ebbcca5b0ce7f0 Mon Sep 17 00:00:00 2001 From: dx-tan Date: Tue, 12 Dec 2023 12:49:31 +0700 Subject: [PATCH] Fix: S3 with no endpoint --- cope2n-api/fwd_api/utils/S3_process.py | 35 +++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cope2n-api/fwd_api/utils/S3_process.py b/cope2n-api/fwd_api/utils/S3_process.py index 0cdbb7e..60d8380 100755 --- a/cope2n-api/fwd_api/utils/S3_process.py +++ b/cope2n-api/fwd_api/utils/S3_process.py @@ -1,18 +1,25 @@ import boto3 class MinioS3Client: - def __init__(self, endpoint, access_key, secret_key, bucket_name): + def __init__(self, access_key, secret_key, bucket_name, endpoint=""): self.endpoint = endpoint self.access_key = access_key self.secret_key = secret_key self.bucket_name = bucket_name try: - self.s3_client = boto3.client( - 's3', - endpoint_url=endpoint, - aws_access_key_id=access_key, - aws_secret_access_key=secret_key - ) + if len(endpoint) > 0: + self.s3_client = boto3.client( + 's3', + endpoint_url=endpoint, + aws_access_key_id=access_key, + aws_secret_access_key=secret_key + ) + else: + self.s3_client = boto3.client( + 's3', + aws_access_key_id=access_key, + aws_secret_access_key=secret_key + ) except Exception as e: print(f"[WARM] Unable to create an s3 client, {e}") self.s3_client = None @@ -46,17 +53,17 @@ class MinioS3Client: print(f"Error downloading file from S3: {str(e)}") if __name__=="__main__": - FILE = "/app/media/users/2/subscriptions/34/requests/invoice/Ctel33aed8ecc189447a81c23a86c5262403/temp_Ctel33aed8ecc189447a81c23a86c5262403.jpg" + FILE = "/app/media/users/1/subscriptions/33/requests/sbt_invoice/SAP00c6c229c2954e498b119968a318d366/temp_SAP00c6c229c2954e498b119968a318d366.jpg" s3_client = MinioS3Client( - endpoint='http://minio:9884', - access_key='TannedCung', - secret_key='TannedCung', - bucket_name='ocr-data' + # endpoint='http://minio:9884', + access_key='AKIA3AFPFVWZD77UACHE', + secret_key='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', + bucket_name='ocr-sds' ) # Read the content of the JPG image file - with open(FILE, 'rb') as file: - image_content = file.read() + # with open(FILE, 'rb') as file: + # image_content = file.read() # Update the object in S3 with the new content # res = s3_client.update_object('invoice/viettinbank/image.jpg', image_content)