3

I've been using Basho Riak for a few weeks now. I've only had to store string data.

However, I'm looking at using it to store Images and I would like some idea how I can do this with the PHP Client.

Below is the basic code to store data:

require_once('riak-php-client/riak.php');

# Connect to Riak
$client = new RiakClient('127.0.0.1', 8098);

# Choose a bucket name
$bucket = $client->bucket('test');

# Supply a key under which to store your data
$person = $bucket->newObject('string_key', 'string_data');

# Save the object to Riak
$person->store();

Do I just base64_encode the image and then store the resulting string?! Or is there a better way?!

Thanks in advance.

MightyE
  • 2,679
  • 18
  • 18
ObiHill
  • 11,448
  • 20
  • 86
  • 135

1 Answers1

5

You'll want to use RiakBucket::newBinary() and RiakBucket::getBinary() if you want to store unencoded binary data into Riak with the PHP client.

$image = file_get_contents("images/TagLabs-Logo-White-240x60.png");
$md5 = md5($image);

$riak->bucket("test")
    ->newObject("image_base64", base64_encode($image))
    ->store();

$riak->bucket("test")
    ->newBinary("image_raw", $image, 'image/png')
    ->store();

$b64Read = $riak->bucket("test")->get("image_base64");
echo "B64 md5 comparison: original=$md5, b64=".md5(base64_decode($b64Read->getData()))."\n";
$rawRead = $riak->bucket("test")->getBinary("image_raw");
echo "Raw md5 comparison: original=$md5, raw=".md5($rawRead->getData())."\n";

Produces output:

B64 md5 comparison: original=6749cfaf1516b01db9792e119d53177a, b64=6749cfaf1516b01db9792e119d53177a 
Raw md5 comparison: original=6749cfaf1516b01db9792e119d53177a, raw=6749cfaf1516b01db9792e119d53177a

In my performance tests, both approaches have basically the same overhead from Riak's perspective. Spending cycles on base64 encoding / decoding (plus under the hood, the base64 data is then json encoded/decoded) puts the binary approach ahead overall.

Edit: Also note that there's a ~50mb upper limit for data stored in a Riak binary object (see this post) due to a limitation in the Erlang backend. Realistically if you're getting anywhere near that, you might want to rethink how you're storing those images, that's a lot of data to send on the pipe if you're accessing those frequently, something like NFS or another local filesystem cache is probably a better idea.

Community
  • 1
  • 1
MightyE
  • 2,679
  • 18
  • 18
  • Thanks. I was leaning towards base64_encode. If I had my way I would never store raw image data in Riak. If they are images to be accessed online I would put them in Rackspace Cloud Files and then put a record of their location in Riak. If they were private files I would probably consider getting some dedicated servers with fusion-io cards and save the images there and do the same linking. But for now, I'll go with y[our] option. Cheers. – ObiHill Feb 24 '12 at 04:08
  • 1mb is the recommended limit for data stored in a Riak binary object, anything above that and you should consider Riak CS. – bryan_basho Nov 17 '14 at 23:25