包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'
我还尝试将此函数传递给extract函数:
文件= storage.bucket('bucketname').file('fileName.pdf');
它处理本地文件的方式(而不是GCS存储桶):
const filePath = path.join(__dirname, tempFileName);
extract(filePath, callback);
发布于 2018-08-16 06:41:02
检查pdf- source code -extract的文本,它只设计用于本地文件(它使用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);
}
);发布于 2019-03-18 11:04:05
我需要使用google云存储上的文件路径来打开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));https://stackoverflow.com/questions/51866267
复制相似问题