mirror of
https://github.com/zulip/zulip.git
synced 2025-11-01 20:44:04 +00:00
uploads: Cache boto client in the module and be writable.
The `get_signed_upload_url` code is called for every S3 file serve request, and is thus in the hot path. The boto3 client caching optimization is thus potentially useful as a performance optimization.
This commit is contained in:
committed by
Tim Abbott
parent
1a7b3ef7ed
commit
c1e8ecd08f
@@ -13,6 +13,7 @@ from botocore.client import Config
|
|||||||
from botocore.response import StreamingBody
|
from botocore.response import StreamingBody
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.utils.http import content_disposition_header
|
from django.utils.http import content_disposition_header
|
||||||
|
from mypy_boto3_s3.client import S3Client
|
||||||
from mypy_boto3_s3.service_resource import Bucket
|
from mypy_boto3_s3.service_resource import Bucket
|
||||||
from typing_extensions import override
|
from typing_extensions import override
|
||||||
|
|
||||||
@@ -111,8 +112,20 @@ def upload_content_to_s3(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
BOTO_CLIENT: S3Client | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_boto_client() -> S3Client:
|
||||||
|
"""
|
||||||
|
Creating the client takes a long time so we need to cache it.
|
||||||
|
"""
|
||||||
|
global BOTO_CLIENT
|
||||||
|
if BOTO_CLIENT is None:
|
||||||
|
BOTO_CLIENT = get_bucket(settings.S3_AUTH_UPLOADS_BUCKET).meta.client
|
||||||
|
return BOTO_CLIENT
|
||||||
|
|
||||||
|
|
||||||
def get_signed_upload_url(path: str, force_download: bool = False) -> str:
|
def get_signed_upload_url(path: str, force_download: bool = False) -> str:
|
||||||
client = get_bucket(settings.S3_AUTH_UPLOADS_BUCKET).meta.client
|
|
||||||
params = {
|
params = {
|
||||||
"Bucket": settings.S3_AUTH_UPLOADS_BUCKET,
|
"Bucket": settings.S3_AUTH_UPLOADS_BUCKET,
|
||||||
"Key": path,
|
"Key": path,
|
||||||
@@ -120,7 +133,7 @@ def get_signed_upload_url(path: str, force_download: bool = False) -> str:
|
|||||||
if force_download:
|
if force_download:
|
||||||
params["ResponseContentDisposition"] = "attachment"
|
params["ResponseContentDisposition"] = "attachment"
|
||||||
|
|
||||||
return client.generate_presigned_url(
|
return get_boto_client().generate_presigned_url(
|
||||||
ClientMethod="get_object",
|
ClientMethod="get_object",
|
||||||
Params=params,
|
Params=params,
|
||||||
ExpiresIn=SIGNED_UPLOAD_URL_DURATION,
|
ExpiresIn=SIGNED_UPLOAD_URL_DURATION,
|
||||||
|
|||||||
Reference in New Issue
Block a user