fastapi-storages
fastapi-storages copied to clipboard
Support `upload_to` argument for file/image types
https://docs.djangoproject.com/en/4.2/ref/models/fields/#filefield
U can override method get_name
def get_name(self, name: str) -> str:
filename = secure_filename(Path(name).name)
return str(Path(name).with_name(filename))
https://github.com/aminalaee/fastapi-storages/blob/615167aed103909271da82abff92d5c5cb47612f/fastapi_storages/s3.py#L63C12-L63C12
@aminalaee should we consider using MEDIA_ROOT
just like Django, and add upload_to argument to FileType
or ImageType
? Or would it be good, to have storage argument (like now), plus upload_to?
@mmzeynalli What do you mean by storage arguments? Because I think we have storage class variables, if that's what you mean.
My bad, explanation was not clear. I meant, when we create a FileType
class MyModel(Base):
img = mapped_column(FileType, upload_to="/path/to/file")
is what we want right? Which uses FileType.storage + upload_to
path for file
Yeah I think that makes sense, plus note that it can be a callable: https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.FileField.upload_to
I think we can ignore upload_to="uploads/%Y/%m/%d/"
for now, this can be achieved with the callable.
How can I go about using a callable? I wanted to upload something like /tmp/{user.id}/
As of now, this feature is not implemented. Instead, you can change file name when you receive the file (for ex. API), giving it the needed path