无法从 amazon s3 下载文件

Can't download files from amazon s3

我正在尝试实现两种方法,一种用于将文件上传到 s3,另一种用于下载文件。

更新功能有效,但是当我尝试下载其中一个更新文件时,出现 404 错误,提示我没有权限。

存储桶权限设置为任何登录用户的所有权限,但是当通过代码创建文件时,创建的文件仅具有一个用户的权限。

有谁知道如何更改创建文件的权限?

更新下载功能如下:

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

经过几个小时的反复试验,我成功地修复了这个错误。

显然,当创建存储桶并为每个经过身份验证的用户设置所有凭据时,这还不够。

我还必须说明存储桶策略才能从中读取。

我使用的策略是:

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

这就解决了问题。