feat(app-assets): add file download functionality with pre-signed URLs and enhance asset management

This commit is contained in:
Harry
2026-01-15 17:20:10 +08:00
parent 33f3374ea6
commit 6bb09dc58c
8 changed files with 296 additions and 47 deletions
+3
View File
@@ -118,6 +118,9 @@ class Storage:
def scan(self, path: str, files: bool = True, directories: bool = False) -> list[str]:
return self.storage_runner.scan(path, files=files, directories=directories)
def get_download_url(self, filename: str, expires_in: int = 3600) -> str:
return self.storage_runner.get_download_url(filename, expires_in)
storage = Storage()
+8
View File
@@ -85,3 +85,11 @@ class AwsS3Storage(BaseStorage):
def delete(self, filename):
self.client.delete_object(Bucket=self.bucket_name, Key=filename)
def get_download_url(self, filename: str, expires_in: int = 3600) -> str:
url: str = self.client.generate_presigned_url(
ClientMethod="get_object",
Params={"Bucket": self.bucket_name, "Key": filename},
ExpiresIn=expires_in,
)
return url
+19
View File
@@ -38,3 +38,22 @@ class BaseStorage(ABC):
If a storage backend doesn't support scanning, it will raise NotImplementedError.
"""
raise NotImplementedError("This storage backend doesn't support scanning")
def get_download_url(self, filename: str, expires_in: int = 3600) -> str:
"""
Generate a pre-signed URL for downloading a file.
Storage backends that support pre-signed URLs (e.g., S3, Azure Blob, GCS)
should override this method to return a direct download URL.
Args:
filename: The file path/key in storage
expires_in: URL validity duration in seconds (default: 1 hour)
Returns:
Pre-signed URL string
Raises:
NotImplementedError: If this storage backend doesn't support pre-signed URLs
"""
raise NotImplementedError("This storage backend doesn't support pre-signed URLs")