简体   繁体   中英

Getting TypeError when trying to upload to s3 from heroku

Error: TypeError at /accounts/work_feed/upload/1 sequence item 0: expected str instance, NoneType found

Im getting this error when I try to upload an image from my django app on heroku to an aws s3 bucket.

My settings.py looks like:

ROOT_URLCONF = 'ClassTrail.urls'

DATABASES = {
    'default': dj_database_url.config()
}

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = 'static/'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

django_heroku.settings(locals())

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

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('BUCKET_NAME')

S3_URL = 'https://%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

AWS_QUERYSTRING_AUTH = False

(I cut out some irrelevant bits of settings.py)

My app works perfectly with postgres and aws locally, but when I run the website on heroku, everything works except for uploading files to aws.

I have set environment variables on heroku using heroku config:set.

My bucket policy is:

{
    "Version": "2012-10-17",
    "Id": "Policy1648918943307",
    "Statement": [
        {
            "Sid": "Stmt1648918911640",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::classtrailstorage/*"
        }
    ]
}

and i have tried to set a cors with:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD",
            "POST",
            "PUT"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]

Thank you for any help!

Okay, so after hours of debugging and trying different methods to get it working, the issue was that I had set my heroku config vars as: AAWS_ACCESS_KEY_ID instead of AWS_ACCESS_KEY_ID.

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