我有一个Django项目,其中有艺人注册,部分过程是上传个人资料图片以及最多5个图库图片。 我的项目托管在Heroku上,静态和媒体文件存储在S3存储桶中。这是我第一次使用s3存储桶,所以我已尽可能多地授予访问权限以尝试使其工作 我试图根据艺人的标题动态生成图像的位置,所以我的艺人/ models.py设置如下:
def profile_image_path(instance,filename):
upload_dir = os.path.join('profile/', instance.title)
if not os.path.exists(upload_dir):
os.makedirs(upload_dir)
return os.path.join(upload_dir, filename)
def img1_image_path(instance,filename):
upload_dir = os.path.join('img1/', instance.title)
if not os.path.exists(upload_dir):
os.makedirs(upload_dir)
return os.path.join(upload_dir, filename)
title = models.CharField(
max_length = 15
)
profile_image = models.ImageField(
upload_to = profile_image_path,
default = 'no_image.png'
)
image1 = models.ImageField(
upload_to = img1_image_path,
default = 'no_image.png'
)
等
这个想法是图像将存储在S3存储桶中,如下所示:
然而,当我注册一个艺人时,我可以在heroku数据库以及我的应用程序的界面上看到正在使用默认图像而不是我上传的内容。之前手动将默认图像(no_image.png)上传到存储桶,我的应用程序可以看到它。此外,如果我手动上传图像并更新heroku数据库中的配置文件图像位置,我的应用程序也可以看到它。 基本上看起来我已经读取了对存储桶的访问权限,并且无法从我的应用程序写入它。
我的S3存储桶策略如下
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::bookanentertainer"
]
},
{
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::bookanentertainer/*"
]
}
]
}
我的CORS配置
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
我的存储桶设置为“公共访问” 有什么想法吗?