You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何程序化检测Google Cloud Video Intelligence免费月度使用额度及实时用量?

如何程序化查询Google Cloud Video Intelligence的免费额度使用情况

你可以通过两种核心方式来程序化检测Video Intelligence的免费额度使用状态,或是查询实时使用量:利用Cloud Billing API直接查询,或是通过Cloud Usage Export结合BigQuery做更灵活的统计分析。下面是具体的实现步骤和代码示例:

一、使用Cloud Billing API查询(适合程序化实时检测)

Cloud Billing API能让你直接获取计费账号下的资源使用数据,包括Video Intelligence各功能的累计使用时长,刚好满足你检测免费额度的需求。

1. 前期准备

  • 先在GCP控制台启用Cloud Billing API(搜索"Cloud Billing API"并完成启用)。
  • 确保你的Django应用使用的服务账号(GAE Flex默认用App Engine默认服务账号)拥有Billing Account Viewer或Billing Account User权限——在IAM页面给该账号添加对应角色即可。

2. 安装依赖库

在你的Django项目环境中安装Google Cloud Billing的Python客户端库:

pip install google-cloud-billing

3. 编写查询代码

以下是适配Django环境的Python代码示例,用于查询当前计费周期内Object Tracking功能的累计使用量,并判断是否超过1000分钟的免费额度:

from google.cloud import billing_v1
from google.cloud.billing_v1.types import ListUsageReportsRequest
from datetime import datetime, timedelta

def check_video_intelligence_free_limit(billing_account_id, feature_name="Object Tracking"):
    client = billing_v1.CloudBillingClient()
    
    # 自动生成当前月份的起始和结束时间(免费额度按月重置)
    now = datetime.now()
    month_start = datetime(now.year, now.month, 1).strftime("%Y-%m-%dT00:00:00Z")
    next_month = now.replace(day=28) + timedelta(days=4)
    month_end = (next_month - timedelta(days=next_month.day)).strftime("%Y-%m-%dT23:59:59Z")
    
    # 构造查询请求:过滤Video Intelligence的目标功能
    request = ListUsageReportsRequest(
        name=f"billingAccounts/{billing_account_id}",
        filter=f'service.id="video-intelligence.googleapis.com" AND sku.description:"{feature_name}"',
        start_time=month_start,
        end_time=month_end
    )
    
    total_minutes = 0.0
    for report in client.list_usage_reports(request):
        # 提取使用时长(Video Intelligence的使用量单位为分钟)
        for metric in report.metrics:
            if metric.name == "usage.amount":
                total_minutes += float(metric.value)
    
    # 输出额度状态并返回结果
    if total_minutes >= 1000:
        status = f"{feature_name}免费额度已用完,累计使用{total_minutes:.2f}分钟"
        print(status)
        return False, status
    else:
        remaining = 1000 - total_minutes
        status = f"{feature_name}剩余免费额度:{remaining:.2f}分钟"
        print(status)
        return True, status

# 使用示例:替换为你的计费账号ID(格式:XXXX-XXXX-XXXX)
check_video_intelligence_free_limit("1234-5678-9012")

注意:计费API的数据通常有15-30分钟的延迟,无法做到完全实时,但完全能满足额度检测的需求。

二、使用Cloud Usage Export到BigQuery(适合复杂统计)

如果需要更灵活的查询(比如按天统计使用量、多功能对比分析),可以配置Cloud Usage Export将账单数据导出到BigQuery,再通过SQL查询:

  1. 在GCP控制台的Billing页面,找到你的计费账号,开启「Usage export to BigQuery」,指定一个BigQuery数据集。
  2. 账单数据会自动同步到BigQuery的gcp_billing_export_v1_<BILLING_ACCOUNT_ID>表中。
  3. 编写SQL查询当前月份Object Tracking的累计使用量:
SELECT
  SUM(usage.amount) AS total_minutes
FROM
  `your-project.your-dataset.gcp_billing_export_v1_XXXX_XXXX_XXXX`
WHERE
  service.description = "Cloud Video Intelligence API"
  AND sku.description LIKE "%Object Tracking%"
  AND usage_start_time >= DATE_TRUNC(CURRENT_DATE(), MONTH)
  AND usage_end_time < DATE_ADD(DATE_TRUNC(CURRENT_DATE(), MONTH), INTERVAL 1 MONTH)

三、使用Cloud Monitoring获取近实时指标

如果需要更接近实时的使用量(延迟约几分钟),可以利用Cloud Monitoring的专属指标:

  • 指标名称:video_intelligence.googleapis.com/api/annotations_duration
  • 维度:feature(对应Video Intelligence的功能,比如OBJECT_TRACKING)
  • 你可以安装google-cloud-monitoring库,通过Cloud Monitoring API查询该指标的累计值,代码逻辑类似Billing API,只需按时间范围和维度过滤统计即可。

内容的提问来源于stack exchange,提问作者london_utku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:12:51