mirror of
https://github.com/langgenius/dify.git
synced 2026-09-24 23:22:26 +08:00
feat(app-assets): add file download functionality with pre-signed URLs and enhance asset management
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user