Fix: S3 with no endpoint

This commit is contained in:
dx-tan 2023-12-12 12:49:31 +07:00
parent 2cd04d8296
commit 5bc302fc75

View File

@ -1,18 +1,25 @@
import boto3 import boto3
class MinioS3Client: 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.endpoint = endpoint
self.access_key = access_key self.access_key = access_key
self.secret_key = secret_key self.secret_key = secret_key
self.bucket_name = bucket_name self.bucket_name = bucket_name
try: try:
self.s3_client = boto3.client( if len(endpoint) > 0:
's3', self.s3_client = boto3.client(
endpoint_url=endpoint, 's3',
aws_access_key_id=access_key, endpoint_url=endpoint,
aws_secret_access_key=secret_key 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: except Exception as e:
print(f"[WARM] Unable to create an s3 client, {e}") print(f"[WARM] Unable to create an s3 client, {e}")
self.s3_client = None self.s3_client = None
@ -46,17 +53,17 @@ class MinioS3Client:
print(f"Error downloading file from S3: {str(e)}") print(f"Error downloading file from S3: {str(e)}")
if __name__=="__main__": 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( s3_client = MinioS3Client(
endpoint='http://minio:9884', # endpoint='http://minio:9884',
access_key='TannedCung', access_key='AKIA3AFPFVWZD77UACHE',
secret_key='TannedCung', secret_key='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',
bucket_name='ocr-data' bucket_name='ocr-sds'
) )
# Read the content of the JPG image file # Read the content of the JPG image file
with open(FILE, 'rb') as file: # with open(FILE, 'rb') as file:
image_content = file.read() # image_content = file.read()
# Update the object in S3 with the new content # Update the object in S3 with the new content
# res = s3_client.update_object('invoice/viettinbank/image.jpg', image_content) # res = s3_client.update_object('invoice/viettinbank/image.jpg', image_content)