【发布时间】:2023-01-23 17:08:01
【问题描述】:
我正在尝试从 React 应用程序读取 Google Cloud Storage 存储桶中的数据。 我的存储桶不能在互联网上公开。我已经创建了一个服务帐户来授权我的应用程序访问存储桶。 我可以访问存储桶并列出文件,但无法下载文件的内容:我收到以下错误:类型错误:可读不是异步可迭代的
我使用 create-react-app 创建了我的应用程序,我的节点模块版本是:
"react": "^18.2.0",
"@google-cloud/storage": "^6.8.0",
我的代码如下:
import React, {useState} from 'react';
import {Storage} from "@google-cloud/storage";
import jsonKey from '../keys/`[my-json-key].json';
export default function TestsLight() {
const [fileData, setFileData] = useState(null);
/* Files and Bucket details: */
const file_name = 'my-file.csv';
const bucketName = 'my-bucket.appspot.com';
/* Storage instantiation: works: */
const storage = new Storage({credentials: jsonKey});
const bucket = storage.bucket(bucketName);
const myFile = bucket.file(file_name);
/* file download: DOES NOT WORK: returns `TypeError: readable is not async iterable` */
myFile.download(function (err, contents) {
console.log('err: ', err);
console.log('contents: ', contents);
contents && setFileData(contents);
});
return (
fileData ?
<div>
{fileData}
</div> :
<div>
<span>no data</span>
</div>
)
}
我按照以下步骤操作
https://cloud.google.com/nodejs/docs/reference/storage/latest
我尝试过了:
- 创建一个桶:作品
- 列出存储桶中的文件:作品
- 下载内存或本地文件中的文件内容:不起作用:类型错误:可读不是异步可迭代的
知道有什么问题吗? 非常感谢
2023 年 1 月 13 日编辑:添加标签 node.js
【问题讨论】:
标签: node.js reactjs google-cloud-storage create-react-app