繁体   English   中英

如何从谷歌云存储下载文件?

[英]How to download file from Google Cloud Storage?

我正在跟进这篇文章以从 GCP 云存储桶下载对象: https://cloud.google.com/storage/docs/downloading-objects#storage-download-object-nodejs

 const {Storage} = require('@google-cloud/storage'); // Creates a client const storage = new Storage(); async function downloadIntoMemory() { // Downloads the file into a buffer in memory. const contents = await storage.bucket(bucketName).file(fileName).download(); return contents; ); } downloadIntoMemory().catch(console.error);

我目前正在contents中获取缓冲区数据。 我已将此代码连接到 NodeJS 后端的 API。 我在前端使用 React Typescript。 调用 API,给我数据缓冲区。 我如何使用它来下载文件而不是数据缓冲区?

我尝试了上述明确提供文件目的地的方法,但我仍然收到以下错误:EISDIR:目录上的非法操作,打开'{file_path_which_i_was_set}。 错误:-21

正如@John Hanley 正确指出的那样,您指的是文档,其中代码示例将 object 下载到 memory 中的内存/缓冲区中。如果您想将 object 从存储桶下载到文件,请参阅此代码示例,其中“选项”参数必须传递给 download() 方法。 代码是这样的:

// The ID of your GCS bucket
const bucketName = 'your-unique-bucket-name';

// The ID of your GCS file
const fileName = 'your-file-name';

// The path to which the file should be downloaded
const destFileName = '/local/path/to/file.txt';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function downloadFile() {
  const options = {
    destination: destFileName,
  };

  // Downloads the file to the destination file path
  await storage.bucket(bucketName).file(fileName).download(options);

  console.log(
    `gs://${bucketName}/${fileName} downloaded to ${destFileName}.`
  );
}

downloadFile().catch(console.error);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM