【问题标题】:Upload files dynamically into different s3 buckets with django-storages boto3使用 django-storages boto3 将文件动态上传到不同的 s3 存储桶中
【发布时间】:2017-08-30 19:07:54
【问题描述】:

我正在使用 django-storages 上传到 s3 存储桶。 但我希望能够上传到与我在 settings.py 中的默认设置不同的 s3 存储桶中 我的应用程序对每个存储桶都有不同的网页,并且可以访问内容。 理想情况下,我想从 url 中提取存储桶名称,并将存储桶名称动态设置为在我的代码中上传文件的位置。

    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)

这只会保存到默认位置

【问题讨论】:

    标签: python django amazon-web-services amazon-s3 boto3


    【解决方案1】:
        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)
    

    找到我自己的答案

    【讨论】:

      猜你喜欢
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 2020-12-24
      • 1970-01-01
      相关资源
      最近更新 更多