【问题标题】:/tmp/ directory not found in cloud function在云函数中找不到 /tmp/ 目录
【发布时间】:2020-05-15 12:37:51
【问题描述】:

我正在尝试从云存储加载 csv 文件,更改标题并将其重新上传到云存储。

// Import the Google Cloud client libraries
const { Storage } = require('@google-cloud/storage');
const lineReplace = require('line-replace');

const storage = new Storage();
const bucketName = 'bucket';
const srcFilename = 'bq/cost.csv';
const localFilename = 'tmp/cost.csv';
const bucketFilename = 'ga/cost.csv';

exports.properGAHeaders = async (data, context) => {
  try {
    const file = data;

    // Downloads the file
    if (file.name = srcFilename) {
      const options = {
        // The path to which the file should be downloaded, e.g. "./file.txt"
        destination: localFilename,
      };
      await storage.bucket(bucketName).file(srcFilename).download(options);

      await lineReplace({
        file: localFilename,
        line: 1,
        text: 'ga:date,ga:medium,ga:source,ga:adClicks,ga:adCost,ga:impressions,ga:referralPath,ga:adContent,ga:campaign,ga:adFinalUrl',
        addNewLine: false,
        callback: ({
          file, line, text, replacedText, error,
        }) => {
          if (error) { console.log(error); }
          console.log(`${replacedText} is replaced with ${text}!`);
        },
      });
      await storage.bucket(bucketName).upload(localFilename, {
        destination: bucketFilename,
        // Support for HTTP requests made with `Accept-Encoding: gzip`
        gzip: false,
        // By setting the option `destination`, you can change the name of the
        // object you are uploading to a bucket.
        metadata: {
          // Enable long-lived HTTP caching headers
          // Use only if the contents of the file will never change
          // (If the contents will change, use cacheControl: 'no-cache')
          cacheControl: 'no-cache',
        },
      });

      console.log(`${localFilename} uploaded to ${bucketName}`);
    }
  } catch (err) { console.error(err); }
};

执行此操作时,我收到错误 Error: ENOENT: no such file or directory, open 'tmp/cost.csv'

可能是什么问题? 我正在使用 await 运行这些函数,因此它们应该同步运行。

【问题讨论】:

    标签: node.js google-cloud-functions google-cloud-storage


    【解决方案1】:

    tmp 的完整路径应以斜杠开头。例如:/tmp/cost.csv

    更好的是,使用os.tmpdir() 构建路径,而不是硬编码理论上可能会改变的值。

    【讨论】:

    • 我在尝试使用 os.tmpdir() 时收到错误消息,os 未在 Object. (/srv/index.js:25:17) 中定义。我尝试要求操作系统,然后我收到消息说它被调用了 2 次。
    • 问题是开头缺少 /。
    • 这很奇怪,我目前正试图在写出文件(使用 mmpjeg)后列出 /tmp 中的文件,并且.. dir 似乎为空。我现在正在尝试使用 os.TempDir() 在 go..
    猜你喜欢
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 2021-01-04
    • 2022-09-25
    • 1970-01-01
    相关资源
    最近更新 更多