簡體   English   中英

無法從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/*"
        }]}

這就解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM