在Node.js中使用URL链接而不是文件路径。

huangapple go评论69阅读模式
英文:

Use url link instead of path to file in Node JS

问题

I have a requirement to process a file in node to extract some text from that file, my issue is since i am new to node, i don't understand how to pass the path to the file. I am using cloud Function for Firebase so there is no server, hence no directory for files. Is there a workaround? Like using url links instead?

Here's my Node JS code:

exports.extractTextFromPDF = functions
.https.onCall((data, context) => {
const bucket = firebase.storage().bucket()
const file = bucket.file(data.pathLink) //data.pathlink is 'my-pdf.pdf' which is a file inside my storage
return file.download()
  .then(data => {
    return pdfParse(data[0])
  })
  .then(data => {
    file.delete()
    .then(() => {
      return data.text
    })
    .catch(err => console.log(err))
  })
  .catch(err => console.log(err))
})

I understand i can just pass the path to a file in my server, but i have no server!
Can i use a url link instead?

If that's not possible, is it possible alternatively to upload a file on the front end and pass that file in node?

I've tried a number of things:

  1. I've tried passing a url link instead of the path to file - doesn't work

  2. I've tried passing the firebase storage bucket path as a path to file - doesn't work

  3. I've tried uploading a file from the front end and passing it to node as the file path - doesn't work either

英文:

I have a requirement to process a file in node to extract some text from that file, my issue is since i am new to node, i don't understand how to pass the path to the file. I am using cloud Function for Firebase so there is no server, hence no directory for files. Is there a workaround? Like using url links instead?

Here's my Node JS code:

exports.extractTextFromPDF = functions
.https.onCall((data, context) => {
const bucket = firebase.storage().bucket()
const file = bucket.file(data.pathLink) //data.pathlink is 'my-pdf.pdf' which is a file inside my storage
return file.download()
  .then(data => {
    return pdfParse(data[0])
  })
  .then(data => {
    file.delete()
    .then(() => {
      return data.text
    })
    .catch(err => console.log(err))
  })
  .catch(err => console.log(err))
})

I understand i can just pass the path to a file in my server, but i have no server!
Can i use a url link instead?

If that's not possible, is it possible alternatively to upload a file on the front end and pass that file in node?

I've tried a number of things:

  1. I've tried passing a url link instead of the path to file - doesn't work

  2. I've tried passing the firebase storage bucket path as a path to file - doesn't work

  3. I've tried uploading a file from the front end and passing it to node as the file path - doesn't work either

答案1

得分: 1

正如官方的云函数示例所示,您可以使用云函数的临时目录作为本地磁盘存储。

根据文档的解释,您应该在结束云函数之前删除临时文件:

临时目录中的本地磁盘存储是一个内存中的文件系统。您写入的文件会消耗您的函数可用内存,有时会在调用之间保留。未明确删除这些文件可能最终导致内存不足错误和随后的冷启动

该文档还提醒我们应该使用与平台/操作系统无关的方法来构建文件路径。通过遵循示例中提供的方法,您将遵循这一建议。

英文:

As shown in this official Cloud Function sample, you can use the temporary directory of the Cloud Function as a local disk storage.

As explained in the doc, you should delete temporary files before ending the Cloud Function:

> Local disk storage in the temporary directory is an in-memory
> filesystem. Files that you write consume memory available to your
> function, and sometimes persist between invocations. Failing to
> explicitly delete these files may eventually lead to an out-of-memory
> error and a subsequent cold start
.


The doc also draws our attention on the fact that we should use platform/OS-independent methods to construct file paths. By following the approach presented in the sample you will follow this recommendation.

huangapple
  • 本文由 发表于 2023年1月6日 01:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75021928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定