使用Backblaze B2作为Django的默认存储。

huangapple go评论74阅读模式
英文:

Use Backblaze B2 as Django Default Storage

问题

I want to use Backblaze B2 to serve media files only in Django. I have used django-storages for that purpose, and as per its documentation it supports Backblaze B2.

I used the below configuration in settings.py -

INSTALLED_APPS = [
    ............
    'storages',
    'boto',
]

STORAGES = {
    "default": {
        "BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
    },
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}

AWS_ACCESS_KEY_ID = "<my_backblaze_application_keyID>"
AWS_SECRET_ACCESS_KEY = "<my_backblaze_applicationKey>"
AWS_STORAGE_BUCKET_NAME = "<my_public_bucket_name>"
AWS_S3_REGION_NAME = "<region_set_by_backblaze>"
AWS_S3_ENDPOINT = f"s3.{AWS_S3_REGION_NAME}.backblazeb2.com"
AWS_S3_ENDPOINT_URL = f"https://{AWS_S3_ENDPOINT}"
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_ENDPOINT}'

MEDIA_URL = '/media/'

But the files are not uploading in Backblaze. How to implement that properly?

英文:

I want to use Backblaze B2 to serve media files only in Django. I have used django-storages for that purpose, and as per it's documentation it support Backblaze B2.

I used the below configuration in settings.py -

INSTALLED_APPS = [
    ............
    &#39;storages&#39;,
    &#39;boto&#39;,
]

STORAGES = {
    &quot;default&quot;: {
        &quot;BACKEND&quot;: &quot;storages.backends.s3boto3.S3Boto3Storage&quot;,
    },
    &quot;staticfiles&quot;: {
        &quot;BACKEND&quot;: &quot;whitenoise.storage.CompressedManifestStaticFilesStorage&quot;,
    },
}

AWS_ACCESS_KEY_ID = &quot;&lt;my_backblaze_application_keyID&gt;&quot;
AWS_SECRET_ACCESS_KEY = &quot;&lt;my_backblaze_applicationKey&gt;&quot;
AWS_STORAGE_BUCKET_NAME = &quot;&lt;my_public_bucket_name&gt;&quot;
AWS_S3_REGION_NAME = &quot;&lt;region_set_by_backblaze&gt;&quot;
AWS_S3_ENDPOINT = f&quot;s3.{AWS_S3_REGION_NAME}.backblazeb2.com&quot;
AWS_S3_ENDPOINT_URL = f&quot;https://{AWS_S3_ENDPOINT}&quot;
AWS_S3_CUSTOM_DOMAIN = f&#39;{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_ENDPOINT}&#39;

MEDIA_URL = &#39;/media/&#39;

But the files are not uploading in the Backblaze. How to implement that properly?

答案1

得分: 2

你在评论中提到它会静默失败但会保存数据它也会将上传的文件保存到磁盘上吗如果是的话听起来它仍然在使用默认的存储后端

我刚刚启动了一个Django应用程序来测试这个因为在Django 4.2改变了存储配置之后我之前就一直没有成功过以下是对我有效的设置

`settings.py`

```python
STORAGES = {
    "default": {
        "BACKEND": "storages.backends.s3boto3.S3Boto3Storage"
    },
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
    }
}

AWS_ACCESS_KEY_ID = os.environ['B2_APPLICATION_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['B2_APPLICATION_KEY']
AWS_STORAGE_BUCKET_NAME = os.environ['B2_BUCKET_NAME']
AWS_S3_REGION_NAME = os.environ['B2_REGION']
AWS_S3_ENDPOINT = f's3.{AWS_S3_REGION_NAME}.backblazeb2.com'
AWS_S3_ENDPOINT_URL = f'https://{AWS_S3_ENDPOINT}'
AWS_DEFAULT_ACL = 'public-read'

AWS_LOCATION = 'media/'

关于这些设置的说明:

  • 我不需要在INSTALLED_APPS中添加任何内容。
  • 我使用了默认的静态文件存储后端,因为我关注的是Backblaze B2的配置。
  • 我习惯将凭据设置为环境变量,以降低它们在我将代码推送到GitHub、StackOverflow等平台时泄露的可能性。
  • AWS_S3_REGION_NAME应该是us-west-004eu-central-003或类似的值。
  • 看起来你在使用一个公共存储桶。默认情况下,S3后端使用private访问控制列表(ACL)创建文件,但是B2在存储桶级别设置可见性,对象的ACL必须匹配。
  • 要将文件放入存储桶中的特定文件夹,你需要使用AWS_LOCATION,而不是MEDIA_URL。请注意,在位置(location)中不需要前导的/

我将整个示例放在了https://github.com/backblaze-b2-samples/django-42-example上,你可以看到我如何设置模型、视图等。

最后一点要注意的是检查你的Django版本:

% python -m django --version 
4.2.1

如果你使用的是早于Django 4.2的版本,存储设置的语法会有所不同。你需要将这个:

STORAGES = {
    "default": {
        "BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
    },
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}

改成这个:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

<details>
<summary>英文:</summary>

You mentioned in a comment that it silently fails, but it saves the data? Does it save the uploaded file to disk also? If so, it sounds like it&#39;s still using the default storage backend.

I just spun up a Django app to test this, as I&#39;ve had it working in the past, but not since Django 4.2 changed storage configuration. These are the settings that worked for me. 

`settings.py`

STORAGES = {
"default": {
"BACKEND": "storages.backends.s3boto3.S3Boto3Storage"
},
"staticfiles": {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
}
}

AWS_ACCESS_KEY_ID = os.environ['B2_APPLICATION_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['B2_APPLICATION_KEY']
AWS_STORAGE_BUCKET_NAME = os.environ['B2_BUCKET_NAME']
AWS_S3_REGION_NAME = os.environ['B2_REGION']
AWS_S3_ENDPOINT = f's3.{AWS_S3_REGION_NAME}.backblazeb2.com'
AWS_S3_ENDPOINT_URL = f'https://{AWS_S3_ENDPOINT}'
AWS_DEFAULT_ACL = 'public-read'

AWS_LOCATION = 'media/'


Notes on the settings:

 - I didn&#39;t need to add anything to `INSTALLED_APPS`
 - I used the default static storage backend, since I was focused on the Backblaze B2 configuration
 - I&#39;m in the habit of setting credentials as environment variables, to reduce the chance they leak when I push code to GitHub, StackOverflow etc.
 - `AWS_S3_REGION_NAME` should be `us-west-004`, `eu-central-003` or similar
 - It looks like you&#39;re using a public bucket. By default, the S3 backend creates files with a `private` ACL, but B2 sets visibility at the bucket level, and object ACL&#39;s have to match.
 - To put files in a particular folder in the bucket, you need to use `AWS_LOCATION`, rather than `MEDIA_URL`. Note that you don&#39;t need the leading `/` in the location.

I put the entire sample online at https://github.com/backblaze-b2-samples/django-42-example, so you can see how I set up the model, view etc.

One final thought - check your Django version:

% python -m django --version
4.2.1


If you&#39;re using a version older than Django 4.2, the syntax for storage settings is different. You&#39;ll need to change this:

STORAGES = {
"default": {
"BACKEND": "storages.backends.s3boto3.S3Boto3Storage",
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}


to this:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'


</details>



huangapple
  • 本文由 发表于 2023年5月11日 16:17:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定