【问题标题】:How to access objects in google cloud storage?如何访问谷歌云存储中的对象?
【发布时间】:2017-07-27 00:38:56
【问题描述】:

我正在尝试构建一个应用程序,该应用程序将访问我的谷歌云存储中的文件,而存储桶是私有的。文档并不清楚如何才能从服务器访问存储桶。我有他们提供的 json 文件。我计划使用 nodejs 公开存储桶上的文件,但我不确定如何授权对存储桶的请求。顺便说一句,我正在使用 axios。我是创建 API 密钥还是使用密钥作为查询字符串?一些帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

    您想要 Google Cloud 的 Node.JS 库:https://googlecloudplatform.github.io/google-cloud-node/#/

    这是一个使用它来访问 GCS 中的对象的示例:

    var config = {
      projectId: 'grape-spaceship-123',
      keyFilename: '/path/to/keyfile.json'
    };
    var storage = require('@google-cloud/storage')(config);
    
    var bucket = gcs.bucket('my-existing-bucket');
    var remoteReadStream = bucket.file('giraffe.jpg').createReadStream();
    remoteReadStream.pipe(someLocalWriteStream);
    

    GitHub 页面上还有其他几个示例:https://github.com/GoogleCloudPlatform/google-cloud-node#preview-1

    以及此处的大量文档: https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/0.56.0/storage

    【讨论】:

    • 这就是我想要的!非常感谢!
    【解决方案2】:

    试试这个:

            scopes = ['https://www.googleapis.com/auth/devstorage.read_only']
            cred = ServiceAccountCredentials.from_json_keyfile_name('<path to .json credential file>', scopes)
            bucket = '<your bucket name>'
            google_service = build('storage', 'v1', credentials=cred)
            req = google_service.objects().get_media(bucket=bucket,object='<your cloud object name>')
            media = MediaIoBaseDownload(<local file name>, req, chunksize=1024*1024)
            resp = None
            while resp is None:
                status, resp = media.next_chunk()
    

    您的文件将是您提到的“本地文件名”。上面的代码是用 Python 编写的。

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 2019-09-08
      • 1970-01-01
      • 2018-05-11
      • 2019-08-27
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多