nomoney
nomoney

Reputation: 51

How to store an image to riak and get it from riak?

In my project now, I need to store some images to Riak, then get them from Riak and display them in HTML page.

Here is an API I encapsulated:

//inputstream is a stream of an image
byte[] imageContent = IOUtils.toByteArray(inputStream);
String descriptiveId = null;
String storedImageId = UUID.randomUUID().toString();
BlobContent blobContent = new BlobContent(imageContent,descriptiveId);
riakBlobDAO.create(storedImageId,blobContent);

.....

//get the byte[] from riak by the uuid
   BlobContent returnedBlobContent  = riakBlobDAO.get(storedImageId);
   byte[] returnedImageContent = returnedBlobContent.getData();

if i invoke this method,it will return sth,but for this <img src="xxxx">,i can't get the src from the returned sth, so any other solutions?

I know the image stored in riak is binary bytes, but I am confused that how can I get it from riak and display it in HTML page.

I know that <img src="xxxx" />,but if an image stored in Riak,can i generate the image url ?

Upvotes: 3

Views: 498

Answers (1)

Nikhil Rane
Nikhil Rane

Reputation: 103

If I understand your problem correctly, you are storing image's data as a BLOB in Riak and retrieving the same. With this stored BLOB, if you try retrieving it and do something like this in your HTML:

<img src="*<...retrieved BLOB data...>*" /img>

then it's not bound to work. The ideal way is to save this BLOB as a file (maybe a temporary file in /tmp) with proper extension and then use the same path into your HTML.

As an additional point, if your image's BLOB is in a format which is supported by newer browsers under HTML5, then you could also pass the data directly using following format:

<img src="data:image/png;base64, ....." />

More info on it here: Embedding Base64 Images

Although, beware that the above way is subject to varying behaviors on different browsers.

Upvotes: 2

Related Questions