Django对接DigitalOcean Spaces失败,执行collectstatic报403错误
问题:Django对接DigitalOcean Spaces时执行collectstatic出现403 Forbidden错误
我按照多个教程尝试在Django项目中使用DigitalOcean对象存储服务DigitalOcean Spaces,但始终无法成功。此前曾正常使用该服务,之后将静态文件迁移至Contabo Object Storage,但其CDN速度过慢,因此决定换回DigitalOcean Spaces,却发现无法正常工作。我甚至复制了之前能正常运行的提交版本中的代码,问题依然存在。
settings.py配置代码
from storages.backends.s3boto3 import S3Boto3Storage class MediaStorage(S3Boto3Storage): bucket_name = "XXXX" class StaticStorage(S3Boto3Storage): bucket_name = "XXXX" DEFAULT_FILE_STORAGE = "nafssi.settings.MediaStorage" STATICFILES_STORAGE = "nafssi.settings.StaticStorage" AWS_ACCESS_KEY_ID = "XXXX" AWS_SECRET_ACCESS_KEY = "XXX/XXXX/XXXXX" AWS_S3_ENDPOINT_URL = "https://XXXX.fra1.digitaloceanspaces.com" AWS_S3_OBJECT_PARAMETERS = { "CacheControl": "max-age=86400", } MEDIA_URL = f"{AWS_S3_ENDPOINT_URL}/media/" STATIC_URL = f"{AWS_S3_ENDPOINT_URL}/static/"
执行python manage.py collectstatic时的错误日志
You have requested to collect static files at the destination location as specified in your settings. This will overwrite existing files! Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: yes Traceback (most recent call last): File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\manage.py", line 22, in <module> main() File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\manage.py", line 18, in main execute_from_command_line(sys.argv) File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line utility.execute() File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\core\management\__init__.py", line 436, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\core\management\base.py", line 412, in run_from_argv self.execute(*args, **cmd_options) File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\core\management\base.py", line 458, in execute output = self.handle(*args, **options) File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 209, in handle collected = self.collect() File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 135, in collect handler(path, prefixed_path, storage) File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 368, in copy_file if not self.delete_file(path, prefixed_path, source_storage): File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 278, in delete_file if self.storage.exists(prefixed_path): File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\storages\backends\s3boto3.py", line 463, in exists self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name) File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\botocore\client.py", line 535, in _api_call return self._make_api_call(operation_name, kwargs) File "D:\Data\Dropbox\D Drive\Toubib\Nafssi\Code\nafssienv\lib\site-packages\botocore\client.py", line 980, in _make_api_call raise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
已配置的CORS权限
已在DigitalOcean中配置CORS权限,配置截图如下:
内容的提问来源于stack exchange,提问作者Kaïss Bouali
相关产品推荐
相关产品推荐

