0
votes

I can't upload media files to Amazon S3 using django-storages and boto, and following this tutorial. I've also checked these questions:

boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden

Getting boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden when uploading file

But I am not able to use them successfully. I always get the error: S3ResponseError: 403 Forbidden

My bucket policy is:

{
"Version": "2008-10-17",
"Statement": [
    {
        "Sid": "PublicReadForGetBucketObjects",
        "Effect": "Allow",
        "Principal": {
            "AWS": "*"
        },
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::<bucket name>/*"
    },
    {
        "Sid": "",
        "Effect": "Allow",
        "Principal": {
            "AWS": "<arn info>"
        },
        "Action": "s3:*",
        "Resource": [
            "arn:aws:s3:::<bucket name>",
            "arn:aws:s3:::<bucket name>/*"
        ]
    }
]

}

models.py

class Usuario(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    titulo = models.CharField(max_length=100, null=True, blank=True)
    avatar = models.ImageField(upload_to='avatars', null=True, blank=True)

views.py

@require_POST
@login_required(login_url='/acceso/')
def configuracion_usuario_guardar(request):
    u = User.objects.get(pk=request.user.id)
    c = Usuario.objects.get(pk=request.user.id)
    nombre = request.POST.get('nombre', '')
    apellido = request.POST.get('apellido', '')
    cargo = request.POST.get('cargo', '')
    email = request.POST.get('email', '')
    avatar = request.FILES.get('avatar', '')
    u.first_name = nombre
    u.last_name = apellido
    u.email = email
    u.save()
    c.titulo = cargo
    c.avatar = avatar
    c.save()
    return HttpResponseRedirect('/perfil/')

Any ideas?

1
Do you have your 'AWS_ACCESS_KEY_ID' and 'AWS_SECRET_ACCESS_KEY' set in the settings file? - Mark Galloway
Yes. I'm actually able to do python manage.py collectstatic successfully. - aldo_vw
Well, your bucket policy seems correct. - Mark Galloway
Regardless of what policy you place on a bucket, if the object in question is not publicly readable itself, you will get a 403 when you access it. Each object needs to be made publicly readable when it is created. Or you can update the ACL after it is created. - garnaat
@garnaat How do you make an object publicly readable when creating it? - aldo_vw

1 Answers

0
votes

For me the problem was that in my template i was using:

{% load static %}

when i change to

{% load static from staticfiles %} 

I got all working ! Hope i helps others !