【问题标题】:How to access file data from firebase storage without downloading如何在不下载的情况下从 Firebase 存储访问文件数据
【发布时间】:2021-02-16 11:43:13
【问题描述】:

我的文件类型为 .ies 的 URL 已上传到 firebase 存储,我想读取文件中的内容,但是当我在浏览器上点击 URL 时,它只返回有关文件的信息,而不是内容 示例链接:https://firebasestorage.googleapis.com/v0/b/westgatedash-d1341.appspot.com/o/documents%2FIES%2FCDL2-45W-M-120V_IESNA2002.IES

但是当我使用查询参数 alt=media 时,它会下载我不想要的。 是否有任何查询参数来获取文件的数据?或任何实现目标的方法?

【问题讨论】:

    标签: firebase google-cloud-storage firebase-storage


    【解决方案1】:

    根据文档,每次调用都是不同的并且用于不同的目的:

    列出存储桶中的对象:

    curl -X GET -H "Authorization: Bearer OAUTH2_TOKEN" \
      "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o"
    
    curl -X GET \
      -H "Authorization: Bearer OAUTH2_TOKEN" \
      -o "SAVE_TO_LOCATION" \
      "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME?alt=media"
    

    到达端点时的区别恰恰是alt=media 参数。我的理解是您可能想要流式传输对象?如果是这样,您可以通过client libraries 实现它。例如:

    要在云函数上处理文件,您可以关注this guide,您将找到更多详细信息。总而言之,下载文件:

    // Download file from bucket.
    const bucket = admin.storage().bucket(fileBucket);
    const tempFilePath = path.join(os.tmpdir(), fileName);
    const metadata = {
      contentType: contentType,
    };
    await bucket.file(filePath).download({destination: tempFilePath});
    console.log('Image downloaded locally to', tempFilePath);
    

    这会将图像保存到一个临时文件夹,因为这是推荐的方法。

    使用 gcs.bucket.file(filePath).download 将文件下载到 Cloud Functions 实例上的临时目录。在此位置,您可以根据需要处理文件。

    编辑

    根据Downloads Documentation

    您可以通过以下方式向云存储发送下载请求:

    • 简单下载:将对象下载到目标。
    • 流式下载:将数据下载到进程。
    • 切片对象下载:下载大型对象。

    Docs 中,在 GitHub Repo 之后:For C++ 它显示了流下载:

    void ReadObject(google::cloud::storage::Client client,
                    std::vector<std::string> const& argv) {
      //! [read object] [START storage_download_file]
      namespace gcs = google::cloud::storage;
      [](gcs::Client client, std::string const& bucket_name,
         std::string const& object_name) {
        gcs::ObjectReadStream stream = client.ReadObject(bucket_name, object_name);
    
        int count = 0;
        std::string line;
        while (std::getline(stream, line, '\n')) {
          ++count;
        }
    
        std::cout << "The object has " << count << " lines\n";
      }
      //! [read object] [END storage_download_file]
      (std::move(client), argv.at(0), argv.at(1));
    }
    

    但是对于NodeJs,出于某种原因,它没有。我找不到任何例子。另一方面,SatckOverflow 中还有其他问题与相同的问题,即:this:

    const {Storage} = require('@google-cloud/storage');
    const storage = new Storage();
    const bucket = storage.bucket(bucket);
    const remoteFile = bucket.file(file);
    
    let buffer = '';
    remoteFile.createReadStream()
      .on('error', function(err) {console.log(err)})
      .on('data', function(response) {
        buffer += response
      })
      .on('end', function() {
        //console.log(buffer);
        res.send(buffer);
      })
    

    【讨论】:

    • 实际上,我不想在任何时候下载该文件。我需要的是直接读取文件内的内容。我托管在另一台服务器上,它给了我我想要的东西,该文件的链接在这里:cnw.hostbumpup.website/wp-content/themes/aduma/… 但是当我点击 Firebase 存储桶提供给我的 URL 时,它只给了我有关文件的信息。而且我现在不处理图像。
    • 乔斯巴伦顺便感谢您的努力。我真的很感激并喜欢它。请查看上述评论并告诉我您是否可以提供帮助。谢谢,伙计(y)
    • 抱歉耽搁了,我用一个例子更新答案。
    猜你喜欢
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    相关资源
    最近更新 更多