2

I am trying to write image ( .png/.jpeg/...etc) files to aerospike. From aerospike documentation (https://docs.aerospike.com/server/guide/data-types/blob) I understand that this can be achieved using blob data type. However there is no documentation on using blob data type in C API library (https://docs.aerospike.com/apidocs/c/, https://developer.aerospike.com/client/c) or in aql. I know writing other data types (like int, string, CDT..etc) to aerospike using C API library but using blob data type I am not getting from where to start. Can someone help with documentation on using blob data type in aerospike C API library.

Gopi
  • 689
  • 1
  • 8
  • 17

2 Answers2

3

Did you look at: as_record_set_bytes() https://docs.aerospike.com/apidocs/c/df/dd6/structas__record.html#aea70facda82be0916d0a5342d81ceb2b

https://docs.aerospike.com/apidocs/c/d9/d3c/structas__bytes.html

pgupta
  • 5,130
  • 11
  • 8
  • Yes Sir. I am using as_bytes data type while writing to aerospike. However while reading from aerospike is it necessary to convert as_bytes to string using as_bytes_to_str function and then in application side convert string to byte array to display the image? Or is there a way to pass as_bytes as is to application? – Gopi Nov 30 '22 at 09:06
  • That is outside the scope of Aerospike client library. You will have to look for specific image to byte-array and byte-array to image conversion utilities for your application and programming environment. For e.g. https://social.msdn.microsoft.com/Forums/vstudio/en-US/305b07d1-d1e1-4e10-9363-21ee835957ea/how-to-convert-jpeg-byte-array-to-bitmap-byte-array-in-visual-studio-c?forum=vcgeneral – pgupta Nov 30 '22 at 14:49
  • I think I misunderstood your question but you got a response at https://discuss.aerospike.com/t/retriving-blob-from-aerospike/9887/3 – pgupta Dec 01 '22 at 03:12
3

As answered by @pgupta as_record_set_bytes() can be used to write bytes to aerospike. C code snippet to write is as follows:

as_bytes b;
    
//Reading file into char array
fp = fopen(img_path, "rb"); //Open the file in binary read mode.
fseek(fp, 0, SEEK_END); //jump to end of file
filelen=ftell(fp); //Get current byteoffset
rewind(fp); //jump back to begining of the file
buffer = (uint8_t *)malloc(filelen*sizeof(uint8_t));
fread(buffer, filelen, 1, fp);
fclose(fp);
    
//set as_bytes
as_bytes_init(&b, filelen);
as_bytes_set(&b, 0, buffer, filelen);
    
as_record_set_bytes(&rec, "attachment", &b); //Set blob data

Bytes can be retrived using as_record_get_bytes(). as_bytes can be converted into uint8_t* type using as_bytes_copy(). C code snippet to read is as follows:

as_bytes* b;
uint8_t* res;
unint32_t res_size;
aerospike_key_get(as, &err, NULL, &key, &rec);
b = as_record_get_bytes(rec, bin_name);
res = malloc((b->size)*sizeof(uint8_t));
res_size = as_bytes_copy(b, 0, res, b->size);

Detailed description of each of the aerospike functions can be found at C API Client Aerospike documentation (https://docs.aerospike.com/apidocs/c/index.html).

Gopi
  • 689
  • 1
  • 8
  • 17