无法从亚马逊s3

时间:2015-08-26 14:19:00

标签: python amazon-s3 boto

我正在尝试实现两种方法,一种用于将文件上传到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

1 个答案:

答案 0 :(得分:1)

经过几个小时的试验和错误后,我设法修复了这个错误。

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

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

我使用的政策是:

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

这解决了这个问题。