-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to put custom filename #76
Comments
Do you mean custom file = File(content="Hello World", filename="hello.txt", content_type="text/plain") The You can also override the File class to provide a custom logic for To make it easier, maybe we can check if the File object contain the property file = File(content=..., filename=..., content_type=..., file_id="custom-file-id") What do you think? |
Here's a sample implementation: import uuid
from sqlalchemy_file.file import File
from sqlalchemy_file.stored_file import StoredFile
from typing import Any, Dict, Optional
class CustomFile(File):
"""Extends the SQLAlchemy `File` in order to provide a
way to customize the storage path.
Retains the attributes available for `File` and adds:
Attributes:
prefix (str): The prefix to add to the stored filename
"""
def __init__(self, prefix: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.filename_prefix = prefix
def store_content(
self,
content: Any,
upload_storage: Optional[str] = None,
name: Optional[str] = None,
metadata: Optional[Dict[str, Any]] = None,
extra: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, str]] = None,
content_path: Optional[str] = None,
) -> StoredFile:
"""Store content into provided `upload_storage`
with additional `metadata`. Can be used by processors
to store additional files.
"""
name = name or f"{self.filename_prefix}{str(uuid.uuid4())}"
return super().store_content(
content,
upload_storage,
name,
metadata,
extra,
headers,
content_path,
) |
So i have a similar requirement. |
Hey, I am also saving my images in S3 but it stores the images in the root but I need them to be saved in a directory. Have you find a solution to this? |
I have a pretty specific library that is file extension oriented. In
sqlalchemy-file
it is not possible to specify a custom file name. However, thesqlalchemy_file.file.File.store_content
function supports thename
parameter, but thesqlalchemy_file.file.File.save_to_storage
does not. So the file name will always beuuid4
onlyThe text was updated successfully, but these errors were encountered: