首页 > 解决方案 > 如何衡量每个用户在谷歌云存储上的带宽使用情况?

问题描述

我们希望根据用户数据的流量向用户收费。实际上,他们的数据正在消耗的下行带宽量。

我已经导出了谷歌云存储 access_logs。从日志中,我可以计算文件被访问的次数。(filesize * count 将是带宽使用)

但问题是这不适用于缓存内容。我的计算值远远超过实际使用量。

我采用这种方法是因为我们的流量将是新的并且不会使用缓存,这意味着差异无关紧要。但实际上,这似乎是一个真正的问题。

这是一个常见的用例,我认为应该有更好的方法来解决谷歌云存储的这个问题。

{
  "insertId": "-tohip8e1vmvw",
  "logName": "projects/bucket/logs/cloudaudit.googleapis.com%2Fdata_access",
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "authenticationInfo": {
      "principalEmail": "firebase-storage@system.gserviceaccount.com"
    },
    "authorizationInfo": [
      {
        "granted": true,
        "permission": "storage.objects.get",
        "resource": "projects/_/bucket/bucket.appspot.com/objects/users/2y7aPImLYeTsCt6X0dwNMlW9K5h1/somefile",
        "resourceAttributes": {}
      },
      {
        "granted": true,
        "permission": "storage.objects.getIamPolicy",
        "resource": "projects/_/bucket/bucket.appspot.com/objects/users/2y7aPImLYeTsCt6X0dwNMlW9K5h1/somefile",
        "resourceAttributes": {}
      }
    ],
    "methodName": "storage.objects.get",
    "requestMetadata": {
      "destinationAttributes": {},
      "requestAttributes": {
        "auth": {},
        "time": "2019-07-02T11:58:36.068Z"
      }
    },
    "resourceLocation": {
      "currentLocations": [
        "eu"
      ]
    },
    "resourceName": "projects/_/bucket/bucket.appspot.com/objects/users/2y7aPImLYeTsCt6X0dwNMlW9K5h1/somefile",
    "serviceName": "storage.googleapis.com",
    "status": {}
  },
  "receiveTimestamp": "2019-07-02T11:58:36.412798307Z",
  "resource": {
    "labels": {
      "bucket_name": "bucket.appspot.com",
      "location": "eu",
      "project_id": "project-id"
    },
    "type": "gcs_bucket"
  },
  "severity": "INFO",
  "timestamp": "2019-07-02T11:58:36.062Z"
}

日志条目。

我们现在使用单个存储桶。如果有帮助,也可以使用多个。

标签: google-cloud-storagegcloud

解决方案


一种可能性是为每个用户设置一个单独的存储桶,并通过timeseries api获取存储桶的带宽使用情况。

用于此目的的端点是:

https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.timeSeries/list

以下是实现一小时发送字节数的参数(我们可以指定60s以上的时间范围),其总和将是从桶中发送的总字节数。

{
  "dataSets": [
    {
      "timeSeriesFilter": {
        "filter": "metric.type=\"storage.googleapis.com/network/sent_bytes_count\" resource.type=\"gcs_bucket\" resource.label.\"project_id\"=\"<<<< project id here >>>>\" resource.label.\"bucket_name\"=\"<<<< bucket name here >>>>\"",
        "perSeriesAligner": "ALIGN_SUM",
        "crossSeriesReducer": "REDUCE_SUM",
        "secondaryCrossSeriesReducer": "REDUCE_SUM",
        "minAlignmentPeriod": "3600s",
        "groupByFields": [
          "resource.label.\"bucket_name\""
        ],
        "unitOverride": "By"
      },
      "targetAxis": "Y1",
      "plotType": "LINE",
      "legendTemplate": "${resource.labels.bucket_name}"
    }
  ],
  "options": {
    "mode": "COLOR"
  },
  "constantLines": [],
  "timeshiftDuration": "0s",
  "y1Axis": {
    "label": "y1Axis",
    "scale": "LINEAR"
  }
}


推荐阅读