imageblobaerospikec-api

Storing images using Blob data type in aerospike


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.


Solution

  • 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).