如何使用 Google 的 API 从 Google 云存储获取报告

How can I get reports from Google Cloud Storage using the Google's API

我必须创建一个程序,每天获取有关在 AppStore 和 PlayStore 上安装一组应用程序的信息。

对于 PlayStore,使用 Google Cloud Storage 我按照此页面上的说明使用客户端库和服务帐户方法以及 Python 代码示例进行操作: https://support.google.com/googleplay/android-developer/answer/6135870?hl=en&ref_topic=7071935

我稍微更改了给定的代码以使其工作,因为文档看起来不是最新的。我使连接到 API 成为可能,它似乎连接正确。

我的问题是我不明白我得到的是什么对象以及如何使用它。这不是报告,它看起来像是字典中的文件属性。

这是我的代码(私有数据"hidden"):

import json
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build

client_email = '************.iam.gserviceaccount.com'
json_file = 'PATH/TO/MY/JSON/FILE'
cloud_storage_bucket = 'pubsite_prod_rev_**********'
report_to_download = 'stats/installs/installs_****************_202005_app_version.csv'
private_key = json.loads(open(json_file).read())['private_key']

credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scopes='https://www.googleapis.com/auth/devstorage.read_only')

storage = build('storage', 'v1', http=credentials.authorize(Http()))

supposed_to_be_report = storage.objects().get(bucket=cloud_storage_bucket, object=report_to_download).execute()

当我打印 supposed_to_be_report - 这是一本字典 - 我只得到我所理解的关于他这样报告的元数据:

{'kind': 'storage#object', 'id': 'pubsite_prod_rev_***********/stats/installs/installs_****************_202005_app_version.csv/1591077412052716', 
'selfLink': 'https://www.googleapis.com/storage/v1/b/pubsite_prod_rev_***********/o/stats%2Finstalls%2Finstalls_*************_202005_app_version.csv', 
'mediaLink': 'https://storage.googleapis.com/download/storage/v1/b/pubsite_prod_rev_***********/o/stats%2Finstalls%2Finstalls_****************_202005_app_version.csv?generation=1591077412052716&alt=media', 
'name': 'stats/installs/installs_***********_202005_app_version.csv', 
'bucket': 'pubsite_prod_rev_***********', 
'generation': '1591077412052716', 
'metageneration': '1', 
'contentType': 'text/csv; 
charset=utf-16le', 'storageClass': 'STANDARD', 'size': '378', 'md5Hash': '*****==', 'contentEncoding': 'gzip'......

我不确定我是否正确使用了它。你能解释一下我哪里错了吗 and/or 如何正确获取安装报告? 谢谢。

我可以看到您正在使用 googleapiclient.discovery 客户端,这不是问题,但以编程方式访问 Google Cloud API 的推荐方法是使用 client libraries

其次,您只是在检索对象的元数据。您可以下载对象以访问文件内容,这是使用客户端库的示例。

from google.cloud import storage


def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""
    # bucket_name = "your-bucket-name"
    # source_blob_name = "storage-object-name"
    # destination_file_name = "local/path/to/file"

    storage_client = storage.Client()

    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

    print(
        "Blob {} downloaded to {}.".format(
            source_blob_name, destination_file_name
        )
    )

样本取自 official docs