簡體   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