I am using django 3.1.0 with django-storages 1.12.3. I am using an S3 backend to store media files (Minio). I am trying to post the file to S3, and include additional user metadata to the S3 object. This additional metadata comes from the POST request used to upload the file to django. With S3, custom metadata can be added by including additional key-value pairs to the HTTP header for the POST request sent to the S3 server when the file is uploaded, where the custom keys would be prefixed with 'X-Amz-Meta-'.
I am using a FileField in a django model, and files are uploaded using a REST API endpoint. As far as I understand it, when django receives the file, it stores it temporarily, and then, when saving the FielField on the model instance, the file is posted to the S3 server. I want to modify the flow, so that the custom metadata is taken from the request and included in the post to the S3 server.
Any idea how I can take data from the initial request, and pass it to the header of the POST request being sent to S3?
Update
After trying Option 1 from Helge Schneider's answer, I did a little modification and got it to work.
I am using django rest framework, so I modified the serializer .save() method.
from .models import MyModel
from rest_framework import serializers
class MyModelSerialzer(serializers.ModelSerializer):
class Meta:
model = SessionFile
fields = ['file', 'my_other_field']
def save(self):
file = self.validated_data['file']
my_other_field = self.validated_data['my_other_field']
mymodel = MyModel(file=file, my_other_field=my_other_field)
options_dict = {"Metadata": {"metadata1": "ImageName",
"metadata2": "ImagePROPERTIES",
"metadata3": "ImageCREATIONDATE"}
}
mymodel.file.storage.object_parameters.update(options_dict)
mymodel.save()