Comments

5 comments

  • Avatar
    Awank
    const crypto = require('crypto-js');
    const axios = require('axios');
    const FormData = require('form-data');

    const API_SECRET = 'API_SECRET';
    const API_KEY = 'API_KEY';
    const CLOUD_NAME = 'CLOUD_NAME';

    function generateHash(timestamp) {
    const string = `timestamp=${timestamp}${API_SECRET}`;
    const hash = crypto.SHA1(string);
    return crypto.enc.Hex.stringify(hash);
    }

    async function uploadImage() {
    const timestamp = newDate().getTime()/1000;
    const signature = generateHash(timestamp);

    const { data } = await axios.get('FILE_URL', { responseType: 'arraybuffer' })
    const file=Buffer.from(data, 'binary').toString('base64');

    const formData = newFormData();
    formData.append('file', 'data:image/png;base64,'+file);
    formData.append('api_key', API_KEY);
    formData.append('timestamp', timestamp);
    formData.append('signature', signature);

    await axios.post(`https://api.cloudinary.com/v1_1/${CLOUD_NAME}/image/upload`, formData, {
    headers: {
    ...formData.getHeaders()
    }
    }).then((response) => {
    console.log('response', response.data);
    }).catch((error) => {
    console.log('error', error.response.data);
    });
    }

    uploadImage();
    0
    Comment actions Permalink
  • Avatar
    Awank

    Here, is my example function and code

    0
    Comment actions Permalink
  • Avatar
    Tia Esguerra

    Hi there, 

    Thanks so much for reaching out. 

    You can specify the resource type as follows:
    https://api.cloudinary.com/v1_1/<cloud name>/<resource_type>/upload

    So to upload a video, the endpoint would be:
    https://api.cloudinary.com/v1_1/<cloud name>/video/upload

    You can reference the documentation on uploading with a direct call to the REST API here: https://cloudinary.com/documentation/upload_images#uploading_with_a_direct_call_to_the_rest_api 

    I hope this helps. If you have any questions, do not hesitate to ask.

    Kind Regards, 

    Tia

    0
    Comment actions Permalink
  • Avatar
    Awank

    Thats Great!

    But, can i upload media (image/video) from buffer with HTTP Request?

    I try to upload, but got response 

    { error: { message: 'Invalid URL for upload' } }

    Here is my example code/function

     

    async function uploadImage() {
    const timestamp = newDate().getTime()/1000;
    const signature = generateHash(timestamp);
    const { data } = await axios.get('IMAGE_URL', {responseType: 'arraybuffer'})

    const formData = newFormData();
    formData.append('file', data);
    formData.append('api_key', API_KEY);
    formData.append('timestamp', timestamp);
    formData.append('signature', signature);

    await axios.post(`https://api.cloudinary.com/v1_1/${CLOUD_NAME}/image/upload`, formData, {
    headers: {
       ...formData.getHeaders()
     }
    }).then((response) => {
     console.log('response', response.data);
    }).catch((error) => {
     console.log('error', error.response.data);
    });
    }

    uploadImage();
    0
    Comment actions Permalink
  • Avatar
    Tamara Yulevich

    Hi there, 

    Please note that we have our in-house SDKs which you can use to perform your uploads with a single line of code, e.g. in Node.js:

    cloudinary.v2.uploader.upload("/home/sample.jpg").then(result=>console.log(result));

    For more information:
    https://cloudinary.com/documentation/cloudinary_sdks

    If you'd still like to perform it manually, then note that files will need to be sent as a multipart form-data.
    You can use this Postman example to help you.

    If all of that didn't solve the issue, can you please share your cloud name so I can take a look at our logs? 

    Thanks, 

    Tamara

    0
    Comment actions Permalink

Please sign in to leave a comment.