Upload image to specific folder via endpoint API
Hey guys!
I'm working and integrating Clodinary inside a salesforce portal using APEX to a client and doing everything fine so far using only endpoint since gets and posts. But i need to upload to a specific folder on cloudinary instead of just staying on the assets, in order to have a more clean cloudinary and the images on the right folders.
I hope you can help me, thanks!
-
Hi Ana,
You can specify the folder in the request. For example in Node,
var request = require("request");
var options = { method: 'POST',
url: 'https://api.cloudinary.com/v1_1/<cloud_name>/image/upload',
formData: { file: '<my_file>', folder: '<my_folder>', upload_preset:'<my_preset> } };request(options, function (error, response, body) {
if (error) throw new Error(error);console.log(body);
}); -
Hi Raya!
Yes i can but im not using a libary, im only using the endpoint, this means i do post and get through the endpoint like for example:
Http h = new Http();// Instantiate a new HTTP request, specify the method (POST) as well as the endpointHttpRequest req = new HttpRequest();req.setEndpoint('http://api.cloudinary.com/v1_1/'+cloudname+'/image/upload');req.setMethod('POST');And i get the json response and work from there. Thank you,Hope to ear from you soon! -
Hi Raya,
Sure, by the way this is APEX a kind of JAVA programming language for SalesForce:
String cloudName=cloudnameString apikey=apikeyString apisecret=apisecret;
/* upload image to cloudinary */// Instantiate a new http objectHttp h = new Http();// Instantiate a new HTTP request, specify the method (POST) as well as the endpointHttpRequest req = new HttpRequest();req.setEndpoint('http://api.cloudinary.com/v1_1/'+cloudname+'/image/upload');req.setMethod('POST');
//base64encode picture bodyString pictureString = blobFile;//'UTF-8' encodepictureString= EncodingUtil.urlEncode(pictureString, 'UTF-8');
String tiStmp=String.valueOf(System.NOW().getTime() / 1000);String myData = 'public_id='+fileName+'&folder=pastateste'+'×tamp='+tiStmp+apisecret;Blob hash = Crypto.generateDigest('SHA1',Blob.valueOf(myData));String hexDigest = EncodingUtil.convertToHex(hash);String fileString = 'data:image/png;base64,';
String finalBodyString ='folder=pastateste'+'&public_id='+ fileName +'×tamp='+EncodingUtil.urlEncode(tiStmp, 'UTF-8')+'&api_key='+apikey+'&signature='+EncodingUtil.urlEncode(hexDigest, 'UTF-8')+'&file='+pictureString;
req.setBody(finalBodyString);System.debug('time final -> ' + EncodingUtil.urlEncode(tiStmp, 'UTF-8'));System.debug('signature -> ' + EncodingUtil.urlEncode(hexDigest, 'UTF-8'));System.debug('file -> ' + pictureString);HttpResponse res = new HttpResponse();if (!test.isRunningTest()) {res = h.send(req);}
parseJSON(res); -
Hi Ana,
In that case, you can specify the folder name part of the public id. For example, you'll specify that that public_id is <my_folder>/<my_public_id>
For more information please see the following-https://support.cloudinary.com/hc/en-us/articles/202520902-Can-I-create-folders-in-my-Cloudinary-account-
-
Hi Raya,
Already solved the problem but thanks for the help!
String tiStmp=String.valueOf(System.NOW().getTime() / 1000);String myData = 'folder=Educator Pupil Photos&public_id='+fileName+'×tamp='+tiStmp+apisecret;Blob hash = Crypto.generateDigest('SHA1',Blob.valueOf(myData));String hexDigest = EncodingUtil.convertToHex(hash);String fileString = 'data:image/png;base64,';
String finalBodyString = 'folder=Educator Pupil Photos' +'&public_id='+ fileName +'&api_key='+apikey+'×tamp='+EncodingUtil.urlEncode(tiStmp, 'UTF-8')+'&signature='+EncodingUtil.urlEncode(hexDigest, 'UTF-8')+'&file='+pictureString;
Please sign in to leave a comment.
Comments
6 comments