Start a new topic
Answered

Using files in collection

Hello, 
I am creating simple Android app displaying few pictures and a short description. I would like to store images in Kinvey (I uploaded them to Files) and right now I would like to connect them to my collection. How can I do that? 


Best Answer

Hi,


You are able to upload images to Files on the console. Now you want to access your uploaded files through Business Logic i.e. collection hooks, check this link. Files aren't stored directly with Kinvey (they're on Google Cloud Storage). That's why accessing them is a little different compared to the data in collections.


Please take a look at following code snippet. I have tested this in my environment and it worked fine:


var context = modules.backendContext;
  var utils = modules.utils;
  var appKey = context.getAppKey();
  var masterSecret = context.getMasterSecret();
  var uri = 'https://' + request.headers.host + '/blob/'+ appKey + '/' + '<"_id" of your file>'; // id variable
  var authString = "Basic " + utils.base64.encode(appKey + ":" + masterSecret);
  var requestOptions = {
    uri:uri, 
    headers: {
      "Authorization":authString
    }
  };
  var auth = modules.request.get(requestOptions,function(error, res, body){
    if (error){
      response.error(error);
    } else {
      response.body = JSON.parse(body);
      response.complete(res.status);
    }
  });



Thanks,

Pranav


1 Comment

Answer

Hi,


You are able to upload images to Files on the console. Now you want to access your uploaded files through Business Logic i.e. collection hooks, check this link. Files aren't stored directly with Kinvey (they're on Google Cloud Storage). That's why accessing them is a little different compared to the data in collections.


Please take a look at following code snippet. I have tested this in my environment and it worked fine:


var context = modules.backendContext;
  var utils = modules.utils;
  var appKey = context.getAppKey();
  var masterSecret = context.getMasterSecret();
  var uri = 'https://' + request.headers.host + '/blob/'+ appKey + '/' + '<"_id" of your file>'; // id variable
  var authString = "Basic " + utils.base64.encode(appKey + ":" + masterSecret);
  var requestOptions = {
    uri:uri, 
    headers: {
      "Authorization":authString
    }
  };
  var auth = modules.request.get(requestOptions,function(error, res, body){
    if (error){
      response.error(error);
    } else {
      response.body = JSON.parse(body);
      response.complete(res.status);
    }
  });



Thanks,

Pranav


Login or Signup to post a comment