Upload image to specific folder via endpoint API

Comments

6 comments

  • Avatar
    Raya Straus

    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);
    });

     

    2
    Comment actions Permalink
  • Avatar
    Ana Nunes

    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 endpoint
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
     
    And i get the json response and work from there. Thank you,
    Hope to ear from you soon!
    0
    Comment actions Permalink
  • Avatar
    Raya Straus

    Hi Ana,

    Can you provide us with your full implementation? 

    0
    Comment actions Permalink
  • Avatar
    Ana Nunes

    Hi Raya,

     

    Sure, by the way this is APEX  a kind of JAVA programming language for SalesForce:

     

    String cloudName=cloudname
        String apikey=apikey
        String apisecret=apisecret;

        /* upload image to cloudinary */
        // Instantiate a new http object
        Http h = new Http();
        // Instantiate a new HTTP request, specify the method (POST) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');


        //base64encode picture body
        String pictureString = blobFile;
        //'UTF-8' encode
        pictureString= EncodingUtil.urlEncode(pictureString, 'UTF-8');

        String tiStmp=String.valueOf(System.NOW().getTime() / 1000);
        String myData = 'public_id='+fileName+'&folder=pastateste'+'&timestamp='+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 +
                                '&timestamp='+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);
    0
    Comment actions Permalink
  • Avatar
    Raya Straus

    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-

    0
    Comment actions Permalink
  • Avatar
    Ana Nunes

    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+'&timestamp='+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+
             '&timestamp='+EncodingUtil.urlEncode(tiStmp, 'UTF-8')+
             '&signature='+EncodingUtil.urlEncode(hexDigest, 'UTF-8')+
             '&file='+pictureString;
    0
    Comment actions Permalink

Please sign in to leave a comment.