从Fiware对象存储下载Blob [英] Download blob from fiware object-storage

查看:72
本文介绍了从Fiware对象存储下载Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ObjectStorageAPI管理FIWARE对象存储中的文件时遇到问题. 编写了一组创建图像并将其上传到对象存储的窗口小部件/运算符后,我希望能够在任何给定实例上下载这些文件.但是,看来我做不到.

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.

经过身份验证后,我列出了容器中的内容,然后尝试下载所需的文件,该文件由全局变量file_name指定,如下所示:

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}

未提供任何其他信息.

同时,我的浏览器的网络监视器显示事务实际上正确地进行了,因为(未格式化和缩小的)响应有效载荷为:

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=="}

HTTP状态为200或304,应将其视为正常. 请注意,我可以通过fiware门户UI正常下载请求的文件.

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?

推荐答案

向对象存储服务器请求文件时,响应的主体直接是与所请求文件关联的内容.在您的情况下,您的文件不直接包含图像.相反,它已使用base64进行编码,并包装在包含其他元数据的JSON对象中.这种内容与应用程序/cdmi对象的模仿类型相关联.在对象存储用户中有使用它们的示例.和程序员指南.

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.

尽管您可以使用WireCloud中的那些"cdmi-object"文件,但它不提供创建或解析它们的任何支持(如果您需要通过问题跟踪器).如果最终要使用当前支持的功能来使用这些文件,则需要使用response_type选项来指示您要以文本形式下载文件(而不是以blob形式下载文件)并能够解析JSON内容然后解码图像:

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);
    }
});

无论如何,对象存储GE能够存储二进制内容(斑点).实际上,FI-WARE Live Demo小部件和操作员在上载/下载存储在对象存储中的图像时会利用该支持,请查看

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.

注意: CDMI标准还支持检索最终内容,而不必从客户端处理cdmi对象(请参见

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.

这篇关于从Fiware对象存储下载Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆