【问题标题】:Download File from s3 using Boto via proxy server通过代理服务器使用 Boto 从 s3 下载文件
【发布时间】:2016-11-26 00:47:07
【问题描述】:

我有一个文件到这个地址:

http://s3.amazonaws.com/bucket-name/sdile_pr_2_1_1/pr/0/2/1/1/dile_0_2_1_1.nc

在 s3 存储桶中,我想通过烧瓶应用程序访问它。

为此,我创建了一个如下所示的函数:

@app.route('/select/dile')
def select_dile_by_uri():

    uri=request.args.get('uri')

    if uri is not None:
        if uri.startswith("http://s3.amazonaws.com/"):
            path        = uri.replace("http://s3.amazonaws.com/","")
            bname, kstr = path.split("/",1) # split the bname from the key string
            conn        = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

            try:     
                bucket  = conn.get_bucket(bname)
            except:
                print "BUCKET NOT FOUND"
                return str("ERROR: bucket "+bname+" not found")
            else:
                print "BUCKET CONNECTED"
                try:
                    key = bucket.get_key(kstr)
                    print "KEY: ", key
                except:
                    print "KEY NOT FOUND"
                    return str("ERROR: key "+kstr+"not found")
                else:
                    try: 
                        key.open_read()                         # opens the file
                        headers = dict(key.resp.getheaders())   # request the headers
                        return Response(key, headers=headers)   # return a response                                  
                    except S3ResponseError as e:
                        return Response(e.body, status=e.status, headers=key.resp.getheaders()) 



    abort(400)

下载成功,但下载文件的名称似乎只是“dile”而不是dile_0_2_1_1.nc。

怎么会?有什么需要设置的吗?

【问题讨论】:

    标签: amazon-s3 flask boto


    【解决方案1】:

    我需要做的是在标题中添加一个字段,具体来说:

    headers["Content-Disposition"] = "inline; filename=myfilename"
    

    其中 -myfilename- 是您希望文件具有的名称。

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 2020-12-18
      • 2012-10-15
      • 2014-08-25
      • 2019-09-16
      • 2011-01-07
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多