Maxim Dunavicher
Maxim Dunavicher

Reputation: 635

Can't download files from amazon s3

I am trying to implement two methods, one for uploading files to s3, and the other for downloading the files.

The update functions work, however when i'm trying to download one of the updated files, i get 404 error that says I don't have permission.

The bucket permission are set for all the permissions for any logged-in user, but when a file is being created through the code the file is being created with permission for one user only.

Does anyone know how to change the permissions on the created file?

here are the update and download functions:

from boto.s3.connection import S3Connection
from boto.s3.key import Key

def upload_file(bucket_name, new_file_name_in_bucket, local_file_path):

    print "connecting to s3"
    conn = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
    print 'successfully connected to s3'
    print 'getting bucket'
    amazon_bucket = conn.get_bucket(bucket_name)
    print 'successfully got bucket'

    print 'uploading the file'
    key = Key(amazon_bucket)
    key.key = new_file_name_in_bucket

    # this line will crash
    # if this line would not exist the code would pass, however the file credentials would be for one user only.
    key.set_acl('authenticated-read-write')

    key.set_contents_from_filename(local_file_path)


def download_file(bucket_name, file_name):

    print "connecting to s3"
    conn = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
    print 'successfully connected to s3'
    print 'getting bucket'
    amazon_bucket = conn.get_bucket(bucket_name)
    print 'successfully got bucket'

    print 'downloading file'

    # Note the if validate will not be set to False, it will crash here
    key = amazon_bucket.get_key(file_name, validate=False)

    # This is the line where the error is raised
    key.get_contents_to_filename(key.name)
    conn.close()

    return key

Upvotes: 0

Views: 3986

Answers (1)

Maxim Dunavicher
Maxim Dunavicher

Reputation: 635

After a few hours of trial and error I have managed to fix the bug.

Apparently, when a bucket is created, and the all credentials are set for every authenticated user, that is not enough.

I also had to state the bucket policy in order to read from it.

The policy I used is:

{"Version": "2008-10-17",
        "Statement": [{"Sid": "AllowPublicRead",
        "Effect": "Allow",
        "Principal": {
        "AWS": "*"
        },
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::YOUR-BUCKET-NAME/*"
        }]}

And that fixed the problem.

Upvotes: 1

Related Questions