简体   繁体   中英

Google Cloud Storage NodeJS getFilesStream Async

I have a Google Function that never returns; it just hits the timeout limit as a Google Function. It works fine locally within under 60 seconds. Not sure what the issue might be. Code is below:

/**
 * Responds to any HTTP request.
 *
 * @param {!express:Request} req HTTP request context.
 * @param {!express:Response} res HTTP response context.
 */
const {Storage} = require('@google-cloud/storage');
exports.main = async (req, res) => {
  const storage = new Storage({projectId: 'our-project'});
  const store = storage.bucket('our-bucket');
    const incomplete = {
    LT04: [],
    LT05: [],
    LE07: [],
    LC08: []
  };

  store.getFilesStream({prefix : 'prefixToMatch', autoPaginate : true })
    .on('error', (err) => {
      return console.error(err.toString())
    })
    .on('data', (file) => {
      // Find small/bad files
      if (file.metadata.size === 162) {
        const split = file.name.split('/');
        const prefix = split[2].substr(0, 4);
        incomplete[prefix].push(file.name);
      }
    })
    .on('end', () => {
      return JSON.stringify(incomplete, false, '  ');
    });
};

Your code it seems ok. But you need to take into account some additional details about this.

Does your Cloud function's memory is enough for this? I think that you could increase the memory allocated of your CF.

Are you sure that this is due to a timeout issue? If you have not seen the logs you can do it going to the Error reporting section.

In case that you have already confirm this, another option could be to change the timeout duration.

I think the issue was that I needed to send a "res.send" instead a Promise.resolve. As well, I needed to remove the async before the function.

Thanks for the quick response with guidelines, error was easier than that apparently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM