【问题标题】:Google Cloud Platform file to node server via gcloud谷歌云平台文件通过 gcloud 到节点服务器
【发布时间】:2016-03-15 16:23:37
【问题描述】:

我在 Google Cloud Platform 上有一个存储桶,我的应用程序的一部分在其中添加了具有唯一名称(无扩展名)的小文本文件。

第二个应用需要检索单个文本文件(一次只能一个)以插入到模板中。

我找不到正确的 api 调用。

按要求配置:

var gcloud = require('gcloud');

var gcs = gcloud.storage({
    projectId: settings.bucketName,
    keyFilename: settings.bucketKeyfile 
});
var textBucket = gcs.bucket(settings.bucketTitle);

保存到存储桶效果很好:

textBucket.upload(fileLocation, function(err, file) {
    if(err) {
        console.log("File not uploaded: " + err);
    } else {
        // console.log("File uploaded: " + file);
    }
});

以下内容看似合乎逻辑,但仅返回元数据,而不是回调中使用的实际文件;

textBucket.get(fileName, function(err, file) {
    if(err) {
        console.log("File not retrieved: " + err);
    } else {
        callback(file);
    }
});

这可能并不奇怪,因为它实际上不在官方文档中,但话又说回来,一个简单的 asnyc 函数也不是返回您要求的文档。

【问题讨论】:

    标签: node.js google-cloud-storage gcloud-node


    【解决方案1】:

    Bucket 对象上的方法 get 记录在此:https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.29.0/storage/bucket?method=get

    如果您只想将文件下载到内存中,请尝试download 对象上的方法downloadhttps://googlecloudplatform.github.io/gcloud-node/#/docs/v0.29.0/storage/file?method=download。如果使用流工作流,您也可以使用createReadStream

    如果您有改进文档的想法,最好在 https://github.com/googlecloudplatform/gcloud-node 上打开问题,这样我们可以让下一个人更轻松。

    【讨论】:

    • 史蒂文,谢谢你 - 实际上我刚回来写我的解决方案,这基本上就是你提出的;在 File 对象上调用下载。此外,这些文件是 JSON,但返回时添加了反斜杠 - 需要 JSON.parse()。文档可能很好;我只是不是一个程序员......
    猜你喜欢
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 2022-01-10
    • 2018-04-07
    • 1970-01-01
    • 2021-01-10
    相关资源
    最近更新 更多