0

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?

Community
  • 1
  • 1
aldo_vw
  • 608
  • 6
  • 17
  • Do you have your 'AWS_ACCESS_KEY_ID' and 'AWS_SECRET_ACCESS_KEY' set in the settings file? – Mark Galloway Aug 24 '15 at 19:22
  • Yes. I'm actually able to do `python manage.py collectstatic` successfully. – aldo_vw Aug 24 '15 at 19:23
  • Well, your bucket policy seems correct. – Mark Galloway Aug 24 '15 at 19:26
  • 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 Aug 24 '15 at 19:44
  • @garnaat How do you make an object publicly readable when creating it? – aldo_vw Aug 24 '15 at 19:47
  • Well, with boto you would do ``key.set_contents_from_file(fp, policy='public-read')``. How are you uploading the files to S3? – garnaat Aug 24 '15 at 20:04
  • I just updated my question to include fragments of `models.py` and `views.py` – aldo_vw Aug 24 '15 at 20:19

1 Answers1

0

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 !

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
Tiago Almeida
  • 812
  • 9
  • 9