UPDATE: Generate S3 url

This commit is contained in:
daovietanh99 2024-02-21 18:20:35 +07:00
parent 3f04c81b6c
commit 532905ca78
3 changed files with 20 additions and 3 deletions

View File

@ -15,7 +15,7 @@ import json
from ..exception.exceptions import InvalidException, RequiredFieldException, NotFoundException from ..exception.exceptions import InvalidException, RequiredFieldException, NotFoundException
from ..models import SubscriptionRequest, Report, ReportFile, SubscriptionRequestFile from ..models import SubscriptionRequest, Report, ReportFile, SubscriptionRequestFile
from ..utils.accuracy import shadow_report, MonthReportAccumulate, first_of_list, extract_report_detail_list, IterAvg from ..utils.accuracy import shadow_report, MonthReportAccumulate, first_of_list, extract_report_detail_list, IterAvg
from ..utils.file import download_from_S3, convert_date_string, build_media_url_v2, build_url from ..utils.file import download_from_S3, convert_date_string, build_media_url_v2, build_url, build_S3_url
from ..utils.redis import RedisUtils from ..utils.redis import RedisUtils
from ..utils.process import string_to_boolean from ..utils.process import string_to_boolean
from ..request.ReportCreationSerializer import ReportCreationSerializer from ..request.ReportCreationSerializer import ReportCreationSerializer
@ -588,8 +588,8 @@ class RequestViewSet(viewsets.ViewSet):
'File Name': subscription_request_file.file_name, 'File Name': subscription_request_file.file_name,
'File Path': subscription_request_file.file_path, 'File Path': subscription_request_file.file_path,
'File Category': subscription_request_file.file_category, 'File Category': subscription_request_file.file_category,
'File URL': build_media_url_v2(subscription_request_file.file_name.split('.')[0], user_id, sub_id, sync_id), 'File URL': build_S3_url("sbt_invoice/" + subscription_request.request_id + "/" + subscription_request_file.file_name, 600),
'Origin_Name': subscription_request_file.origin_name, 'Original Name': subscription_request_file.origin_name,
'Is Bad Image Quality': subscription_request_file.is_bad_image_quality, 'Is Bad Image Quality': subscription_request_file.is_bad_image_quality,
'Doc Type': subscription_request_file.doc_type, 'Doc Type': subscription_request_file.doc_type,
'Processing Time (ms)': subscription_request_file.processing_time, 'Processing Time (ms)': subscription_request_file.processing_time,

View File

@ -429,6 +429,8 @@ def build_media_url_v2(media_id: str, user_id: int, sub_id: int, u_sync_id: str)
token = image_authenticator.generate_img_token_v2(user_id, sub_id, u_sync_id) token = image_authenticator.generate_img_token_v2(user_id, sub_id, u_sync_id)
return f'{settings.BASE_URL}/api/ctel/v2/media/request/{media_id}/?token={token}' return f'{settings.BASE_URL}/api/ctel/v2/media/request/{media_id}/?token={token}'
def build_S3_url(s3_key, exp_time):
return s3_client.create_url_with_expiration(s3_key, exp_time)
def get_value(_dict, keys): def get_value(_dict, keys):
keys = keys.split('.') keys = keys.split('.')

View File

@ -51,6 +51,21 @@ class MinioS3Client:
return res return res
except Exception as e: except Exception as e:
print(f"Error downloading file from S3: {str(e)}") print(f"Error downloading file from S3: {str(e)}")
def create_url_with_expiration(self, s3_key, expiration_time):
try:
res = self.s3_client.generate_presigned_url(
ClientMethod="get_object", ExpiresIn=expiration_time,
Params={
"Bucket": self.bucket_name,
"Key": s3_key,
},
)
# print(f"URL for file '{s3_key}' expires in {expiration_time} seconds")
return res
except Exception as e:
print(f"Error generating URL for file '{s3_key}': {str(e)}")
if __name__=="__main__": if __name__=="__main__":
FILE = "/app/media/users/1/subscriptions/33/requests/sbt_invoice/SAP00c6c229c2954e498b119968a318d366/temp_SAP00c6c229c2954e498b119968a318d366.jpg" FILE = "/app/media/users/1/subscriptions/33/requests/sbt_invoice/SAP00c6c229c2954e498b119968a318d366/temp_SAP00c6c229c2954e498b119968a318d366.jpg"