1

我正在运行一个包含 5 个节点的 riak 集群,我通过协议缓冲区客户端riak-erlang-client连接到该集群。正如我在此 stackoverflow 链接中建议的那样,我将riak-erlang-client安装为氮气插件。我知道如何通过氮网络框架上传文件,然后我尝试将它们存储在 riak 数据库中,然后我检索它们。

我已经查看了Basho 资源中显示的这个亮点

curl -XPUT http://localhost:10018/buckets/images/keys/<image_name>.jpg -H 'Content-Type: image/jpeg' --data-binary @<image_name>.jpg

但它根本无法满足我的需求,因为它甚至不使用我正在使用的客户端!

我曾经使用Erlang/OTP 的文件库从氮气的./scratch目录中读取照片文件,以便将照片文件保存为 riak 数据库中的二进制流,但我未能将它们检索到我的氮气驱动的 Web 应用程序中。

欢迎任何帮助。

4

1 回答 1

0

首先使用 rebar 将 riak-erlang-client 作为插件添加到 Nitrogen。在项目中的 rebar.config 中添加如下依赖项。芭蕉资源

{deps, [
    {riakc, "1.4.1",
      {git, "git://github.com/basho/riak-erlang-client", 
         {tag, "1.4.1"}}}
   ]}.

然后在您的应用程序中运行 make

cd ../../myapp
make

使用例如这个Nitrogen 上传示例上传文件。如果在此处找到源代码。

在 finish_upload_event 部分中,捕获暂存文件夹中的 LocalFileName 文件路径。使用文件路径读取文件。

event(_) -> ok.

start_upload_event(Tag) ->
    wf:flash(wf:f("Upload started with tag (~p)", [Tag])).

finish_upload_event(_Tag, undefined, _, _) -> 
   wf:flash("Please select a file."), ok;

finish_upload_event(_Tag, _FileName, LocalFileName, _Node) ->
   {ok, Binary_image} = file:read_file(LocalFileName),

   %% Open a connection to the Riak database
   {ok, Pid} = riakc_pb_socket:start(DBNode,PORT,{connect_timeout,TIMEOUT},auto_reconnect, false}])).

   %% If the bucket where you save your images is called my_images, create the riak object
   Obj = riakc_obj:new(term_to_binary(my_images),term_to_binary(My_key),Binary_image),

   %% Save to the database
   ok = riakc_pb_socket:put(Pid, Obj,[]).

从数据库中读取图像并将其显示在 Web 浏览器中

-module(image).
-include_lib("nitrogen_core/include/wf.hrl").
-compile(export_all).   

main() -> 
   %% Set the content-type of the image
   wf:content_type("image/png"),
   {ok, Pid} = riakc_pb_socket:start(DBNode,PORT,{connect_timeout,TIMEOUT},auto_reconnect, false}])).

   %% Read the image data from the database
   {ok, Fetched} = riakc_pb_socket:get(Pid,     term_to_binary(my_images),term_to_binary(My_key),[]),

   %% Record the image record
   Binary_image = binary_to_term(riakc_obj:get_value(Fetched)),
   Binary_image.        

event(_) -> ok. 

在浏览器结构中,您指向图像文件的 URL,例如http://example.com/image

其中image是渲染图像文件的模块。

于 2015-11-17T09:55:31.273 回答