在Node Google Cloud Storage下载方法中,可读不可异步迭代

jhkqcmku  于 2023-01-25  发布在  Node.js
关注(0)|答案(1)|浏览(113)

我正在尝试从React应用程序的Google Cloud Storage存储桶中读取数据。我的存储桶不能对Internet公开。我已创建服务帐户,以授予我的应用访问存储桶的授权。我可以访问存储桶并列出文件,但无法下载文件的内容:出现以下错误:* * 类型错误:可读不是可异步迭代的**
我使用create-react-app创建了我的应用,我的节点模块版本为:一个月一个月一个月一个月一个月
我的代码如下:

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 *
ccrfmcuu

ccrfmcuu1#

我已经和负责存储客户端库的Google团队确认过了,它不工作的原因是我的代码在浏览器中运行,而存储库只能在服务器端运行。有关完整的讨论和细节,请参见here

相关问题