0

I've setup a cloud function with guidance from this post Cloud Functions for Firebase - Converting PDF to image

When running the function it runs without errors but the image is not created.

const tempDir = os.tmpdir()
await new Promise(function (resolve, reject) {
  gs()
    .batch()
    .nopause()
    .executablePath('lambda-ghostscript/bin/./gs')
    .option('-dTextAlphaBits=4')
    .res(300)
    .device('jpg')
    .output(`${tempDir}/page-01.jpg`)
    .input(filepath)
    .exec(function (err, stdout, stderr) {
        if (!err) {
          console.log('gs executed w/o error');            
          console.log('stdout',stdout);            
          console.log('stderr',stderr);            
          resolve();
        } else {
          console.log('gs error:', err);
          reject(err);
        }
    });
});
fs.lstat(tempDir+'/page-01.jpg', (err, stats) => {
  if(err)
      return console.log(err); //Handle error
  console.log(`Is file: ${stats.isFile()}`);
});

This is the output when checking if the new file was created:

gs command: -dBATCH,-dNOPAUSE,-dTextAlphaBits=4,-r300,-sDEVICE=jpg,-sOutputFile=/var/folders/dd/zdnq1dcn2vb328ntxxt7_97c0000gn/T/page-01.jpg,/var/folders/dd/zdnq1dcn2vb328ntxxt7_97c0000gn/T/m5YF7ipQsLCuTE9c0pWC_raw.pdf
info: gs executed w/o error
info: stdout 
stderr undefined
info: { Error: ENOENT: no such file or directory, lstat '/var/folders/dd/zdnq1dcn2vb328ntxxt7_97c0000gn/T/page-01.jpg'
  errno: -2,
  code: 'ENOENT',
  syscall: 'lstat',
  path: '/var/folders/dd/zdnq1dcn2vb328ntxxt7_97c0000gn/T/page-01.jpg' }```
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
findev
  • 135
  • 1
  • 2
  • 6
  • Please edit the question to show the entire Cloud Function code, not just a snippet of it. – Doug Stevenson Oct 09 '19 at 02:41
  • 1
    On cloud functions the only [writable part of the filesystem](https://cloud.google.com/functions/docs/concepts/exec#file_system) is /tmp, so you shouldn't be trying to write into /var. – robsiemb Oct 09 '19 at 03:00

1 Answers1

0

The error you are getting is because you are trying to write in a file which does not exist, or it is not writable.

I suppose that the "fs" variable comes from filesystem. Note that, as it is stated in the official documentation:

The only writeable part of the filesystem is the /tmp directory, which you can use to store temporary files in a function instance. This is a local disk mount point known as a "tmpfs" volume in which data written to the volume is stored in memory. Note that it will consume memory resources provisioned for the function.

The "var" directory in which you are trying to write is read-only and accessible to the function.

My suggestion is to try to modify the parameter for the "gs().output(param)" function with something like :

 tempPath = path.join(os.tmpdir(), newName);

Where "newName" would be the name of the image you want to create.

Andrei Tigau
  • 2,010
  • 1
  • 6
  • 17