richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 259 forks source link

File not showing when uploaded to google cloud storage #328

Open melitus opened 1 year ago

melitus commented 1 year ago

Get the error below when trying to view file uploaded on google cloud storage Seem express-fileupload does not work with google cloud storage when useTempFiles : true,

I tried setting the useTempFiles: true and passing the tempFIlePath to my google cloud storage functions it did not work. I got this when an image is saved - https://storage.googleapis.com/temp-hold-eu/Z0cuwDoD6-Screenshot%25202022-08-05%2520at%252017.21.28.png. This is my code shown below

const uploadToGCS = async (
  file: Buffer,
  fileName: string,
  mimeType: string,
): Promise<string> => {
  const storage = new Storage({
    keyFilename: config.googleBucket.serviceKeyPath,
  });
  const bucket = storage.bucket(config.googleBucket.publicBucket);
  const gcsFileName = `${Date.now()}-${fileName}`;
  // Create a new blob in the bucket and upload the file data.
  const fileToUpload = bucket.file(gcsFileName);
  const fileStream = fileToUpload.createWriteStream({
    metadata: {
      contentType: mimeType,
    },
    resumable: false,
  });
  return new Promise((resolve, reject) => {
    fileStream.on('finish', () => {
      const publicUrl = getPublicUrl(encodeURIComponent(fileToUpload.name));
      fileToUpload
        .makePublic()
        .then(() => resolve(publicUrl))
        .catch(reject);
    });
    fileStream.on('error', (ex) => {
      Logger.log(ex, 'GCS');
      return reject;
    });
    fileStream.end(file);
  });
};
RomanBurunkov commented 10 months ago

Hi,

Have you done any troubleshooting with your google environment?

This doesn't looks like related to the upload middleware at all.