【问题标题】:Upload file to google cloud storage use link上传文件到谷歌云存储使用链接
【发布时间】:2021-01-08 21:10:46
【问题描述】:

我尝试将文件从链接上传到 GCS,但我不知道该怎么做,

这是我的代码

app.post("/uploads", upload.any("tmparr"), async (req, res) => {
  await storage
    .bucket(bucketName)
    .upload(
      "https://lalalai.s3-us-west-2.amazonaws.com/media/split/9f6f7cb6-43b2-45ee-bec7-16ae2f7214b5/vocals",
      {
        gzip: true,
        metadata: {
          cacheControl: "public, max-age=31536000",
        },
      }
    );
  console.log("done");
}); 

我遇到了这样的错误

(node:56016) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat 'D:\reactjs\crud-mern\server\https:\lalalai.s3-us-west-2.amazonaws.com\media\split\9f6f7cb6-43b2-45ee-bec7-16ae2f7214b5\vocals'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:56016) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This 
error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:56016) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the 
Node.js process with a non-zero exit code.

有人可以帮我解决它吗?谢谢

【问题讨论】:

  • 如果您查看路径,它会尝试从本地计算机获取文件
  • 是的,我知道,但是如何从链接中获取文件?
  • 用nodejs获取文件,然后放到gcs上
  • 你能举个例子吗?我是 javascript @MehdiBenmoha 的新手
  • 是的,欢迎来到 javascript 世界,示例如下:stackoverflow.com/questions/11944932/…

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


【解决方案1】:

Cloud Storage 不接受文件的 url 链接作为上传参数,正如您在 documentation 中看到的 bucket.upload() 它需要的那样:

您希望上传到存储桶的文件的完全限定路径。

您收到的错误消息就是证明,因为它试图将 URL 解释为您本地系统上的路径。

因此,为了完成这项工作,您必须首先从链接下载所述文件,然后才调用 GCS SDK 并上传它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2014-08-03
    • 2018-09-18
    相关资源
    最近更新 更多