Search code examples
google-cloud-platformgoogle-cloud-storage

google cloud storage per file statistics/downloads


Is it possible to get per file statistics (or at least download count) for files in google cloud storage?

I want to find the number of downloads for a js plugin file to get an idea of how frequently these are used (in client pages).


Solution

  • Yes, it is possible, but it has to be enabled.

    The official recommendation is to create another bucket for the logs generated by the main bucket that you want to trace.

    gsutil mb gs://<some-unique-prefix>-example-logs-bucket
    

    then assign Cloud Storage the roles/storage.legacyBucketWriter role for the bucket:

    gsutil iam ch group:[email protected]:legacyBucketWriter gs://<some-unique-prefix>-example-logs-bucket
    

    and finally enable the logging for your main bucket:

    gsutil logging set on -b gs://example-logs-bucket gs://<main-bucket>
    

    Generate some activity on your main bucket, then wait for one hour at most, hence the reports are generated hourly and daily. You will be able to browse these events on the logs-bucket created at step 1:

    logs-files-saved-in-aux-bucket

    More info, can be found at https://cloud.google.com/storage/docs/access-logs