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

如何查询Google Cloud Text-to-Speech API的免费配额使用情况?

查询Google Cloud Text-to-Speech API字节使用量的方法

你需要用Cloud Monitoring API来获取Text-to-Speech的字节使用明细,Billing API仅负责计费账单统计,无法返回按语音类型拆分的配额使用数据。

核心要点

  • 对应的监控指标是googleapis.com/text_to_speech/synthesized_bytes,该指标会记录所有合成请求的字节数,还可通过voice_type标签区分标准语音(对应400万字节免费配额)和神经语音(对应100万字节免费配额)。
  • PHP开发需使用google/cloud-monitoring客户端库,通过Composer安装:
    composer require google/cloud-monitoring
    

PHP示例代码

以下代码可查询当月内按语音类型拆分的已使用字节数:

use Google\Cloud\Monitoring\V3\MetricServiceClient;
use Google\Cloud\Monitoring\V3\ListTimeSeriesRequest;
use Google\Cloud\Monitoring\V3\TimeInterval;
use Google\Protobuf\Timestamp;

// 初始化客户端
$metricServiceClient = new MetricServiceClient();
$projectName = $metricServiceClient->projectName('你的项目ID');

// 设置时间范围:当月第一天到当前时间
$now = new Timestamp();
$now->setSeconds(time());
$monthStart = new Timestamp();
$monthStart->setSeconds(strtotime(date('Y-m-01 00:00:00')));
$interval = (new TimeInterval())
    ->setStartTime($monthStart)
    ->setEndTime($now);

// 构建查询请求
$request = (new ListTimeSeriesRequest())
    ->setName($projectName)
    ->setFilter('metric.type = "googleapis.com/text_to_speech/synthesized_bytes"')
    ->setInterval($interval)
    ->setAggregation(
        (new \Google\Cloud\Monitoring\V3\Aggregation())
            ->setGroupByFields(['resource.label.voice_type'])
            ->setCrossSeriesReducer(\Google\Cloud\Monitoring\V3\Aggregation\Reducer::SUM)
            ->setAlignmentPeriod(['seconds' => 86400]) // 按天聚合后求和
    );

// 执行查询并处理结果
$timeSeriesList = $metricServiceClient->listTimeSeries($request);
foreach ($timeSeriesList as $timeSeries) {
    $voiceType = $timeSeries->getResource()->getLabels()['voice_type'];
    // 获取累计使用字节数
    $points = $timeSeries->getPoints();
    $usedBytes = end($points)->getValue()->getInt64Value();
    
    // 匹配对应配额
    $quota = $voiceType === 'standard' ? 4000000 : 1000000;
    echo "{$voiceType}语音合成已使用{$usedBytes}字节,配额为{$quota}字节\n";
}

$metricServiceClient->close();

注意事项

  • 确保你的服务账号拥有Monitoring Viewer权限,否则无法查询监控数据。
  • 免费配额统计周期为自然月,与代码中设置的时间范围一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 07:27:06