简体   繁体   中英

Upload files dynamically into different s3 buckets with django-storages boto3

I'm using django-storages to upload into an s3 bucket. But I want to be able to upload into a different s3 bucket than what I have set in my default in settings.py My app has a different web page for each bucket and can access the contents. Ideally I would want to pull the bucket name from the url and dynamically set the bucket name to where the file is being uploaded in my code.

    def post(self, request, *args, **kwargs):
    if request.method == "POST":
        bucket = kwargs.get('bucket_name')
        form_model = Document()
        form_model.upload.storage = S3Boto3Storage(bucket=bucket)
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
    return HttpResponseRedirect("/bucket/"+bucket)

This only saves to the defaul location

    def post(self, request, *args, **kwargs):
    if request.method == "POST":
        bucket = kwargs.get('bucket_name')
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.instance.upload.storage = S3Boto3Storage(bucket=bucket)
            form.save()
    return HttpResponseRedirect("/bucket/"+bucket)

Found my own answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM