1

I want to shift from AWS to Backblaze, I tried to follow the documentation on django storages to simply modify a little my already exisiting aws configuration but I just kept having errors. My current aws configuration is like this:

   AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY')
   AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
   AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
   AWS_DEFAULT_ACL = None
   AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
   AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
   DEFAULT_FILE_STORAGE = 'myproject.storages.MediaStorage'

   AWS_QUERYSTRING_AUTH = False

   STATIC_LOCATION = 'static'
   STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
   STATICFILES_STORAGE = 'myproject.storages.StaticStorage'

   MEDIA_LOCATION = 'media'
   MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/'
   DEFAULT_FILE_STORAGE = 'myproject.storages.MediaStorage'
Enoch
  • 11
  • 1
  • 3

1 Answers1

0

I got this working a little while ago. My complete sample app is at https://github.com/backblaze-b2-samples/django-storages-backblaze-b2/tree/main/b2-example-static-and-media.

There are also samples for the 'static only' and 'public and private' in the same repository.

Here are the settings I used:

AWS_ACCESS_KEY_ID = '<your b2 application key id>'
AWS_SECRET_ACCESS_KEY = '<your b2 application key>'

AWS_STORAGE_BUCKET_NAME = '<a public bucket>'

AWS_S3_REGION_NAME = '<your b2 region - e.g. us-west-001>'
AWS_S3_ENDPOINT = f's3.{AWS_S3_REGION_NAME}.backblazeb2.com'
AWS_S3_ENDPOINT_URL = f'https://{AWS_S3_ENDPOINT}'

AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}

AWS_LOCATION = 'static'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_ENDPOINT}/"

DEFAULT_FILE_STORAGE = 'mysite.storage_backends.MediaStorage'

The critical settings you might be wrestling with are AWS_S3_REGION_NAME, AWS_S3_ENDPOINT and AWS_S3_ENDPOINT_URL, as these are all different from the AWS defaults. If you follow my settings, you just need to set AWS_S3_REGION_NAME; the remaining items will be set automatically.

I don't think you need to set AWS_S3_CUSTOM_DOMAIN unless you are using a CDN.

If the above doesn't help, please edit your question to include the B2 settings you tried and the errors you saw.

metadaddy
  • 4,234
  • 1
  • 22
  • 46
  • Thank you so much @metadaddy , this solution worked so fine. However I am trying to store media files in the bucket, its serving the images in the bucket and they are being displayed well in my project but I can't see the directory of the images in the bucket. E.g I go to the url of the image like this `https://s3.us-east-005.backblazeb2.com//media/post_images/` and I am getting an error that says ` NoSuchKey Key not found `. I am trying to figure out what could be wrong but if its a problem your'e aware of please help me. Thanks – Enoch Apr 09 '23 at 22:14
  • It sounds like images are being stored and displayed, but they're not in the location you expect? Log in to the B2 web UI (https://secure.backblaze.com/user_signin.htm) and look at the bucket there. The configuration you shared indicates that they should be stored somewhere in `https://s3.us-east-005.backblazeb2.com//media/`. If they are not in `https://s3.us-east-005.backblazeb2.com//media/post_images/` then there must be something in your code or config that is putting them elsewhere. In any case, I don't think the problem is specific to using Backblaze B2. – metadaddy Apr 10 '23 at 11:07
  • 1
    Thanks again, I was reloading the page of my bucket soon after saving images from django admin interface, the directory wasn't showing but I just had to logout and login in again and then after that I could see the directory with my media files. – Enoch Apr 14 '23 at 17:25