Question

I have some files stored on OpenStack Swift(Or any other Storage Systems). And my django application provide browser based GUI for user to download those files from Swift backend:

(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.

I have tested with static files download as:

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

My questions are:

(1) I could download the file from the download view,but the downloaded file size is always 1k. What's the problem here?

(2) I still have no clue about how to serve the dynamic files download which stored on other Object Storage backend such as OpenStack Swift?

Was it helpful?

Solution

First, In Swift Storage uploaded files are like an object. You couldn't specify file path of object storage.

Second, We can download an uploaded objects(files) using python-swiftclient. The sample code are here.

    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

Hope this is helpful for you. More information go through http://docs.openstack.org/developer/python-swiftclient/swiftclient.html#swiftclient.client.get_object

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top