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

Viewed 853

I have to create a program that get informations on a daily basis about installations of a group of apps on the AppStore and the PlayStore.

For the PlayStore, using Google Cloud Storage I followed the instructions on this page using the client library and a Service Account method and the Python code example : https://support.google.com/googleplay/android-developer/answer/6135870?hl=en&ref_topic=7071935

I slightly changed the given code to make it work since documentation looks not up-to-date. I made it possible to connect to the API and it seems to connect correctly.

My problem is that I don't understand what object I get and how to use it. It's not a report it just looks like files properties in a dict.

This is my code (private data "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()

When I print the supposed_to_be_report - which is a dictionary- I only get what I understand as Metadata about he report like this:

{'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'......

I am not sure I'm using it correctly. Could you please explain me where am I wrong and/or how to get installs reports correctly ? Thanks.

1 Answers

I can see that you are using googleapiclient.discovery client, this is not an issue, but the recommended way to access Google Cloud APIs programmatically is by using the client libraries.

Second, you are just retrieving the object's metadata. You can download the object to have access to the file contents, this is a sample using the client library.

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
        )
    )

Sample taken from official docs.

Related