首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Google云存储获取绝对文件路径,以便与pdf-text-extract一起使用

从Google云存储获取绝对文件路径,以便与pdf-text-extract一起使用
EN

Stack Overflow用户
提问于 2018-08-16 04:52:27
回答 2查看 2.8K关注 0票数 0

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

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

下面是我的代码(Node.js):

代码语言:javascript
复制
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'

我还尝试将此函数传递给extract函数:

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

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

代码语言:javascript
复制
const filePath = path.join(__dirname, tempFileName);  
extract(filePath, callback);

EN

回答 2

Stack Overflow用户

发布于 2018-08-16 06:41:02

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

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

然后你就可以使用它了:

代码语言:javascript
复制
const localFile = path.join(destFilename, srcFilename);
extract(localFile, 
    (err, pages) => {
        if (err) {
            console.error(err)
            return
        }

        console.log(pages);
    }
);
票数 1
EN

Stack Overflow用户

发布于 2019-03-18 11:04:05

我需要使用google云存储上的文件路径来打开sqlite数据库,这是我找到并使用的代码,工作起来非常棒。

代码语言:javascript
复制
// 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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51866267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档