Download blob from fiware object-storage

与世无争的帅哥 提交于 2019-12-11 03:06:33

问题


I am facing problems using ObjectStorageAPI to manage files in FIWARE object storage. Having written a set of widgets/operators that create images and upload them onto object-storage, I would like to be able to download these files at any given instance. However, it seems that i am not able to do so.

After having been authenticated, i list the container contents and, then, i try to download the file i need, specified by the global variable file_name, as follows:

function onGetAuthTokenSuccess(new_token, data){
    ...
    api = new ObjectStorageAPI(object_storage);
    api.listContainer(container, {
        token: token,
        onSuccess: onListContainterSuccess,
        onFailure: function () {
            alert("Could not find container " + container + " in region " + region);
        }
    });
}

function onListContainterSuccess(file_list) {
    api.getFile(container, file_name, {
        token: token,
        onSuccess: onDownloadFileSuccess,
        onFailure: function () {
            alert("Could not successfully download " + file_name);
        }
    });
}

function onDownloadFileSuccess(blob){
    console.log(JSON.stringify(blob));
    ....
}

Running the above code results in an output like the following:

 {"type":"image/png","size":45590}

Without any other information given.

At the same time, my browser's network monitor suggests that the transaction actually took place correctly as the (unformatted and shrinked) response payload was:

{"mimetype":"image/png","metadata":{},"valuetransferencoding":"base64","value":"iVBORw0KGgoAAAANSUhEUgAAA0MAAAE2CAYAAACjsEm0AAAgAElEQVR4Xux9CZgU1dX26Z5935kZlmGTRTZl00iMS4iExEQ0RI2JCyayCAqCGCIiYlBj0KhgDEKQfMQvGvJnUYwmBv0kMQaMISoo+47sMiyzz3RP.....m1YaNpGAAAAABJRU5ErkJggg=="}

The HTTP status is either 200 or 304, which should be considered normal. Please note that i am able to download the requested file normally via the fiware portal UI.

Any ideas on how i should proceed to actually get the file in a correct manner?


回答1:


When requesting files to the object storage server, the body of the response is directly the content associated to the requested file. In your case your file doesn't contain directly an image. Instead it has been encoded using base64 and wrapped inside a JSON object containing additional metadata. This kind of content is associated with the application/cdmi-object mimetype. There are examples of their use in the Object Storage user and programmers guide.

Although you can make use of those "cdmi-object" files from WireCloud, it doesn't provide any support for creating nor parsing them (you're open to report a issue if you need that support through the issue tracker). If you finally want to use those files using current supported features, you'll need to use the response_type option for indicating you want to download the file as text (instead of downloading the file as a blob) and being able to parse the JSON content and then decode the image:

api.getFile(container, file_name, {
    token: token,
    response_type: "text",
    onSuccess: function (data) {
        var cdmi_object = JSON.parse(data);
        var image = your_code_for_decoding_base64(cdmi_object.data);
        ...
    },
    onFailure: function () {
        alert("Could not successfully download " + file_name);
    }
});

Anyway, the Object Storage GE is capable of storing binary content (blobs). In fact, the FI-WARE Live Demo widgets and operator make use of that support when uploading/downloading images stored in the Object Storage, take a look to the issue-reporter widget and to the issue service operator codes. This eliminates the need to encode/decode files using base64, makes request/response payloads smaller and therefore improving the performance. In addition to this, you can create URLs using the URL.createObjectURL method and load those image blobs directly from <img> elements.

Note: The CDMI standard also supports retrieving final content without having to process the cdmi-object from the client (see section 8.5.8 of the full spec) but it's not implemented in FI-WARE.



来源:https://stackoverflow.com/questions/24436153/download-blob-from-fiware-object-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!