0
votes

I m trying to stream file contents into Cloudinary and use the return url to pass into my database

I have successfully implemented graphql-upload to upload an image file from my client to the server. I am able to retrieve the stream, filename, mimetype, encoding from the server side

After that, I am unable to send the image to Cloudinary. Below are my codes... Appreciate advise

Thanks

import cloudinary from 'cloudinary';

Mutation: {
        uploadPicture: async (_, { file }) => {
          // Works Ok
      const { stream, filename, mimetype, encoding } = await file;

      try {

        cloudinary.config({
          cloud_name: process.env.cloud_name,
          api_key: process.env.CLOUDINARY_API_KEY,
          api_secret: process.env.CLOUDINARY_API_SECRET
        });

// Below is the area where I am unsure
        var upload_stream = cloudinary.uploader.upload_stream(
          { tags: "sample" },
          function(err: any, image: any) {
            console.log();
            console.log("** Stream Upload");
            if (err) {
              console.warn(err);
            }
            console.log("* Same image, uploaded via stream");
            console.log("* " + image.public_id);
            console.log("* " + image.url);
          }
        );
        const file_reader = fs
          .createReadStream(stream)
          .pipe(upload_stream);


      } catch (error) {
        throw error;
      }
      return true;
    },
1

1 Answers

1
votes

Try adding v2 to your call

var upload_stream= cloudinary.v2.uploader.upload_stream({tags: 'basic_sample'},function(err,image) {
  console.log('here');
  console.log("** Stream Upload");
  //if (err){ console.warn(err);}
  console.log("* Same image, uploaded via stream");
  console.log("* "+image.public_id);
  console.log("* "+image.url);
});
var file_reader = fs.createReadStream('picture.jpg').pipe(upload_stream);