简体   繁体   English

将 AWS S3 存储桶与 Django 一起使用时出现 TypeError

[英]TypeError when using AWS S3 Bucket with Django

I'm brand new to Django and AWS S3 and seem to have hit a wall.我是 Django 和 AWS S3 的新手,似乎碰壁了。 This is all on localhost at the moment.目前这一切都在 localhost 上。 I'm trying to add a new product through my own admin app for a Art Gallery website i'm working on, all worked fine originally when adding a product, but since using AWS S3 i receive the following TypeError:我正在尝试通过我自己的管理应用程序为我正在开发的 Art Gallery 网站添加新产品,最初在添加产品时一切正常,但由于使用 AWS S3,我收到以下 TypeError:

在此处输入图像描述

在此处输入图像描述

The error states that it's expecting "...a string or bytes-like object."该错误表明它期待“......一个字符串或字节,如 object”。 and refers to the following lines of code in both my mixins.py and my staff views.py并在我的mixins.py和我的员工views.py中引用以下代码行

mixins.py: mixins.py:

from django.shortcuts import redirect


class StaffUserMixin(object):
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_staff:
            return redirect("home")
        return super(StaffUserMixin, self).dispatch(request, *args, **kwargs)

views.py:视图.py:

class ProductCreateView(LoginRequiredMixin, StaffUserMixin, generic.CreateView):
    template_name = 'staff/product_create.html'
    form_class = ProductForm

    def get_success_url(self):
        return reverse("staff:product-list")

    def form_valid(self, form):
        form.save()
        return super(ProductCreateView, self).form_valid(form)

As I stated at the start i'm very new to both Django and AWS S3, so if this is just a schoolboy error, please forgive me but any help would be fantastic.正如我在开始时所说的那样,我对 Django 和 AWS S3 都很陌生,所以如果这只是一个小学生的错误,请原谅我,但任何帮助都会很棒。 And if any additional info is needed i'll be happy to provide.如果需要任何其他信息,我很乐意提供。 Thanks!谢谢!

You need to write some lines in your settings.py file in order to get django-storages to work.你需要在你的settings.py文件中写一些行来让django-storages工作。

Those lines are:这些行是:

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')

and you need to identify those variables in your .bash_profile file (if you are on mac) so that they are not committed to your version control system for security reasons.并且您需要在.bash_profile文件中识别这些变量(如果您在 Mac 上),以便出于安全原因它们不会提交给您的版本控制系统。

You need to follow this syntax:您需要遵循以下语法:

export AWS_ACCESS_KEY_ID="YOUR_ID" **(NO SPACES)**
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
export AWS_STORAGE_BUCKET_NAME="YOUR_BUCKET_NAME"

and then you need to provide Heroku (on which I encountered this error) with this data by using configVars .然后您需要使用configVars向 Heroku (我遇到此错误)提供此数据。

In your Terminal/CommandLine:在您的终端/命令行中:

heroku config:set AWS_ACCESS_KEY_ID="YOUR_ID"
heroku config:set AWS_SECRET_ACCESS_KEY="YOUR_SECRET_KEY"
heroku config:set AWS_STORAGE_BUCKET_NAME="YOUR_BUCKET_NAME"

Make sure to use double-quotes!确保使用双引号!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Boto3客户端将对象放置到AWS S3存储桶时发生TypeError - TypeError when using Boto3 client to put_object to AWS S3 Bucket 使用 Lambda 和 S3 将数据插入存储桶时出现 AWS ClientError - AWS ClientError when using Lambda and S3 to insert data to bucket AWS S3:图像正在上传到存储桶,但文件未显示在heroku Django中 - AWS S3: image is uploading to bucket but file is not showing in heroku Django 将图像从 aws s3 存储桶加载到 django 应用程序 - Load an image from aws s3 bucket to django application TypeError:预期的字符串或类似字节的对象 - 如何使用 AWS 文档中提供的代码将文件复制到 S3 存储桶 - TypeError: expected string or bytes-like object - how to copy files to an S3 bucket using the code provided in AWS's documentation 使用 s3-pit-restore 恢复 AWS S3 存储桶 - Restoring AWS S3 bucket using s3-pit-restore 使用Toil连接到AWS Cloud中的S3存储桶时禁止使用403 - 403 Forbidden when connecting to S3 bucket in AWS Cloud using Toil AWS Lambda 尝试将文件从 S3 存储桶复制到另一个 S3 存储桶时出现无效存储桶名称错误 - Invalid bucket name error when AWS Lambda tries to copy files from an S3 bucket to another S3 bucket 无法使用 boto 连接 aws s3 存储桶 - Unable to connect aws s3 bucket using boto 使用 AWS Lambda 更改 s3 存储桶中 xlsx 文件的目录 - Change directory of xlsx file in s3 bucket using AWS Lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM