0

When I trying to upload the file by the below code and able to do that in my local dev environment by when I use code and upload it to the server I am not able to get the response and I get an error - 500, Internal Server Error. I need help in this I have done some research but only get this link but unable to understand to resolve it.

https://cloud.google.com/storage/docs/exponential-backoff

Code For Upload

'use strict';
const {Storage}= require('@google-cloud/storage');
const config = require("./main");
const fs = require('fs')

const gcs =  new Storage({
  projectId: config.GCLOUD_PROJECT,
  keyFilename: config.GCS_KEYFILE
});

const bucketName = config.bucketName
const bucket = gcs.bucket(bucketName);

function getPublicUrl(filename) {
  return 'https://storage.googleapis.com/' + bucketName + '/' + filename;
}

let imageUpload = {};

imageUpload.uploadToGcs = (req, res, next) => {
  if(!req.file) return next();

  // Can optionally add a path to the gcsname below by concatenating it before the filename
  const gcsname = req.file.originalname;
  const file = bucket.file(gcsname);

  const stream = file.createWriteStream({
    metadata: {
      contentType: req.file.mimetype
    },
    resumable: false
  });

  stream.on('error', (err) => {
    req.file.cloudStorageError = err;
    next(err);
  });

  stream.on('finish', () => {
    req.file.cloudStorageObject = gcsname;
    req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
    next();
  });

  stream.end(req.file.buffer);
}

module.exports = imageUpload;

Route For upload

router.post("/uploadArticleImage",multer.single("image"), imgUpload.uploadToGcs,async function(req,res,next){
        try { 
            let data = req.file;
            //console.log(data)
          if(data){
            if (req.file && req.file.cloudStoragePublicUrl) {
                data.imageUrl = req.file.cloudStoragePublicUrl;
              }
          //  let imageUrl = data.imageUrl;
          //  console.log
          res.send({status:true,message:"File Upload Sucessfull",imagedetails:data.imageUrl,token:null});
            }
        else{
            res.send({status:false,message:"File Upload Unsucessfull",data:null,token:null});  
            }    
        } 
        catch (error) {
            console.log(error);
            return res.send({status:false,message:"Unsucessfull Server Error Occured",data:error,token:null});
        }
     }); 
  • Could you please share mode details such as the complete error trace? nodejs version? are you using a service account with enough permissions? – alan Aug 21 '19 at 16:51
  • There is no error in the log excepts internal error 500 status code. node -version is 10 yes account permission is ok because I am able to send a file from my local system to cloud storage. – Prakash Choudhary Aug 22 '19 at 13:17
  • VM service account is authorized for persist on Cloud storage?, This scenario could be related to permissions settings [1]. [1] https://cloud.google.com/storage/docs/access-control/iam-permissions – Mike Perez Ontiveros Aug 23 '19 at 16:43

0 Answers0