【问题标题】:Getting absolute filepath from Google Cloud Storage for use with pdf-text-extract从 Google Cloud Storage 获取绝对文件路径以用于 pdf-text-extract
【发布时间】:2019-01-22 18:39:30
【问题描述】:

pdf-text-extract 需要一个绝对文件路径。我需要在 Google Cloud Storage 存储桶中的文件上使用这个包。

有没有办法将 GCS 存储桶中的文件作为绝对 URL 传递?

这是我的代码(Node.js):

var storage = require('@google-cloud/storage')();
const extract = require('pdf-text-extract');

const bucketFile = 'http://bucketname.storage.googleapis.com/fileName.pdf';

extract(bucketFile, 
    (err, pages) => {
        if (err) {
            console.error(err)
            return
        }

        console.log(pages);
    }
);

这会返回一个错误:

Error: pdf-text-extract command failed: I/O Error: Couldn't open file 'D:\Libraries\Documents\pdf-text-extract-bucket\http:\bucketname.storage.googleapis.com\fileName.pdf'

我也尝试将其传递给提取函数:

file = storage.bucket('bucketname').file('fileName.pdf');

它使用本地文件(而不是 GCS 存储桶)的方式:

const filePath = path.join(__dirname, tempFileName);  
extract(filePath, callback);

【问题讨论】:

    标签: node.js pdf path google-cloud-storage filepath


    【解决方案1】:

    检查 pdf-text-extract 的 source code,它仅设计用于处理本地文件(它使用 path.resolve() 作为参数传递的文件路径)。除非你想改变这个模块的工作方式,你可以download the file 到你的本地系统:

    const Storage = require('@google-cloud/storage');
    const storage = new Storage();
    const options = { destination: destFilename,};
    storage.bucket(bucketName).file(srcFilename).download(options);
    

    然后你就可以使用它了:

    const localFile = path.join(destFilename, srcFilename);
    extract(localFile, 
        (err, pages) => {
            if (err) {
                console.error(err)
                return
            }
    
            console.log(pages);
        }
    );
    

    【讨论】:

    • 我想问题是,有没有办法像使用本地存储一样使用 google-cloud/storage 存储桶?
    • 正如您在GitHub page 中所读到的:“您将需要路径上可用的 pdftotext 二进制文件”。因此,您需要将数据保存在包裹所在的位置。
    【解决方案2】:

    我需要使用谷歌云存储上文件的文件路径来打开一个 sqlite 数据库,这是我找到并使用的代码,就像一个魅力

    // Download file from bucket.
    const bucket = gcs.bucket(fileBucket);
    const tempFilePath = path.join(os.tmpdir(), fileName);
    const metadata = {
      contentType: contentType,
    };
    return bucket.file(filePath).download({
      destination: tempFilePath,
    }).then(() => {
      console.log('Image downloaded locally to', tempFilePath);
      // Generate a thumbnail using ImageMagick.
      return spawn('convert', [tempFilePath, '-thumbnail', '200x200>', tempFilePath]);
    }).then(() => {
      console.log('Thumbnail created at', tempFilePath);
      // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail.
      const thumbFileName = `thumb_${fileName}`;
      const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
      // Uploading the thumbnail.
      return bucket.upload(tempFilePath, {
        destination: thumbFilePath,
        metadata: metadata,
      });
      // Once the thumbnail has been uploaded delete the local file to free up disk space.
    }).then(() => fs.unlinkSync(tempFilePath));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 2015-03-29
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2013-11-17
      相关资源
      最近更新 更多