Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top