How to upload an image to IBM Cloud object storage (COS) using Node.js?

1.5k views Asked by At

I am using ibm-cos-sdk but I can able to store text files only, but I want to upload images and pdf files. Can someone help me out of that?

1

There are 1 answers

0
data_henrik On

The IBM Cloud solution tutorial on how to apply end to end security to a cloud app features a file sharing app. The files are stored in COS, the app built using Node.js. See these lines of code for the example.

  // upload to COS
  await cos.upload({
    Bucket: COS_BUCKET_NAME,
    Key: `${fileDetails.userId}/${fileDetails.id}/${fileDetails.name}`,
    Body: fs.createReadStream(file.path),
    ContentType: fileDetails.type,
  }).promise();

The snippet uses the mentioned ibm-cos-sdk with the S3 interface. I have used the app and code to upload images and PDFs.