1

我有一些文件存储在 OpenStack Swift(或任何其他存储系统)上。我的 django 应用程序提供了基于浏览器的 GUI 供用户从 Swift 后端下载这些文件:

(1) Each file will have a URL such as: http://domain.com/files/file1
(2) User could use browser or any other client program based on HTTP to download files.

我已经测试了静态文件下载为:

def download(request):
    try:
        #TODO
        file_name = 'E:/sample.docx'
        fsock = open(file_name,"r")
        mime_type_guess = mimetypes.guess_type(file_name)
        if mime_type_guess is not None:
            response = HttpResponse(fsock, mimetype=mime_type_guess[0])  
        response['Content-Length'] = os.path.getsize(file_name)         
        response['Content-Disposition'] = 'attachment; filename=' + file_name                        
    except IOError:
        response = HttpResponseNotFound()
    return response

我的问题是:

(1) 我可以从下载视图下载文件,但下载的文件大小始终为 1k。这里有什么问题?

(2) 我仍然不知道如何提供存储在其他对象存储后端(如 OpenStack Swift)上的动态文件下载?

4

1 回答 1

1

首先,在 Swift Storage 中上传的文件就像一个对象。您无法指定对象存储的文件路径。

其次,我们可以使用 python-swiftclient 下载上传的对象(文件)。示例代码在这里。

    def download(request):
        #Get object(file) using swiftclient
        url = str(request.values['url'])
        token = str(request.values['token'])
        container = str(request.values['container'])
        name = str(request.values['name'])
        header, data = Client.get_object(url, token, container, object_name)

        #Send object data to browser
        result = data
        response = Response(result)
        response.headers['Content-Type'] = content['header']['content-type']
        response.headers['Content-Disposition'] = 'attachment; filename="'+filename+'"'
        return response

希望这对你有帮助。更多信息通过http://docs.openstack.org/developer/python-swiftclient/swiftclient.html#swiftclient.client.get_object

于 2013-11-14T06:48:06.753 回答