简体   繁体   English

无法从Amazon S3下载文件

[英]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. 我正在尝试实现两种方法,一种用于将文件上传到s3,另一种用于下载文件。

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. 更新功能正常工作,但是,当我尝试下载更新的文件之一时,出现404错误,提示我没有权限。

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

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. 这就解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM