etianen / django-s3-storage

Django Amazon S3 file storage.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Save VersionId in model

Meess opened this issue · comments

commented

I was wondering if there is a more appropriate way to save the VersionId in a model when updating it. The way I currently came up with is:

my_model = MyModel()
# write query and S3 PUT request
my_model.my_file_field.save("someName.html", file)

# S3 request
version_id = S3Storage().meta(my_model.my_file_field.name)['VersionId']

# write query
my_model.latest_version_id = version_id
my_model.save()

This requires 2 request and 2 writes, which seems a bit cumbersome, being able to access the VersionId when the file is updated would enable it do everything in one go (i.e. 1 write and 1 request). Looked at the source code but couldn't find a way to access the meta data on the response, only by doing an additional request with .meta() or am I overlooking something?

commented

Thanks for the fast response. The problem is that I want certain versions to be bookmarked, e.g. I want versions saved by an administrator to be bookmarked while versions saved by normal users not. This way I can easily serve the user with the last version the administrator approved, while users can upload new versions for the administrator to be approved.

I was using the boto3 library directly but this library seemed to take away a lot of boiler plate code, and make the codebase more maintainable. But I guess I've to write some magic myself then ;).

Just to clarify my scenario: The general idea is that the user uploads some Html, we run some check on it. Then convert the string into a file with ContentFile (from django.core.files.base import ContentFile) and save it under the original name for the FileField. Each subsequent upload of html goes to the same process, when an administrator uploads the Html we catch the VerionId and put it in the model as last_approved_id. This way we can serve the users the last approved html, while we can serve the admins the last uploaded Html by doing a request without VerionId. We're trying to make a kind of Wikipedia with approval.

Anyway, thanks for the response, I'll close the issue.