[英]Django collect static S3 An error occurred (403) when calling the HeadObject operation: Forbidden
I have created a S3 bucket for my Django static files.我为我的 Django static 文件创建了一个 S3 存储桶。 I am able to display the static files from S3 however when I run the
python manage.py collectstatic
command I get the error "An error occurred (403) when calling the HeadObject operation: Forbidden"我能够显示来自 S3 的 static 文件,但是当我运行
python manage.py collectstatic
命令时,我收到错误消息“调用 HeadObject 操作时发生错误 (403):禁止访问”
error traceback错误回溯
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
collected = self.collect()
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
handler(path, prefixed_path, storage)
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 338, in copy_file
if not self.delete_file(path, prefixed_path, source_storage):
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 248, in delete_file
if self.storage.exists(prefixed_path):
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/storages/backends/s3boto3.py", line 469, in exists
self.connection.meta.client.head_object(Bucket=self.bucket_name, Key=name)
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/botocore/client.py", line 391, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/john/Environments/PRM/lib/python3.6/site-packages/botocore/client.py", line 719, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
base_settings.py基本设置.py
# aws settings
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
AWS_DEFAULT_ACL = 'public-read'
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
# s3 static settings
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
Your settings.py looks fine to me.你的 settings.py 对我来说很好。 The problem is with your AWS IAM policy.
问题出在您的 AWS IAM 策略上。 403:Forbidden meaning that you do not have proper rights to access the S3 resources/collect static files to your root.
403:Forbidden 意味着您没有适当的权限访问 S3 资源/将 static 文件收集到您的根目录。 You need to check AWS IAM user policy.
您需要检查 AWS IAM 用户策略。 Unless I see your IAM user policy, can't say much.
除非我看到您的 IAM 用户策略,否则不能说太多。 in the mean time do check this link: Looks similar to the problem you are facing
同时请检查此链接: 看起来与您面临的问题相似
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.