【问题标题】:storageclient class can't be found?找不到 storageclient 类?
【发布时间】:2019-03-02 21:01:58
【问题描述】:

我正在拼命想弄清楚如何通过 PHP(我知道的唯一语言)创建一个简单的音频转录脚本(用于更长的音频文件)。我收到错误找不到类“Google\Cloud\Storage\StorageClient”

我正在使用 gcloud 控制台代码编辑器,并且应该安装所有内容(除非有一个单独的 composer 安装仅用于云存储,尽管我无法在文档中找到任何关于它的内容(如果有的话))。

我还输入了 gcloud auth application-default print-access-token,它打印出一个访问令牌,但我不知道我应该如何处理其他令牌(如果有的话)而不是我复制并粘贴到控制台 shell 提示符中的“set GOOGLE_APPLICATION_CREDENTIALS”命令。

这是 php 代码:

<?php
namespace Google\Cloud\Samples\Speech;
require __DIR__ . '/vendor/autoload.php';
use Exception;
# [START speech_transcribe_async_gcs]
use Google\Cloud\Speech\SpeechClient;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\ExponentialBackoff;

$projectId = 'xxxx';
$speech = new SpeechClient([
    'projectId' => $projectId,
    'languageCode' => 'en-US',
]);

$filename = "20180925_184741_L.mp3";

# The audio file's encoding and sample rate
$options = [
    'encoding' => 'LINEAR16',
    'sampleRateHertz' => 16000,
    'languageCode' => 'en-US',
    'enableWordTimeOffsets' => false,
    'enableAutomaticPunctuation' => true,
    'model' => 'video',
];

function transcribe_async_gcs($bucketName, $objectName, $languageCode = 'en-US', $options = [])
{
    // Create the speech client
    $speech = new SpeechClient([
        'languageCode' => $languageCode,
    ]);
    // Fetch the storage object
    $storage = new StorageClient();
    $object = $storage->bucket($bucketName)->object($objectName);
    // Create the asyncronous recognize operation
    $operation = $speech->beginRecognizeOperation(
        $object,
        $options
    );
    // Wait for the operation to complete
    $backoff = new ExponentialBackoff(10);
    $backoff->execute(function () use ($operation) {
        print('Waiting for operation to complete' . PHP_EOL);
        $operation->reload();
        if (!$operation->isComplete()) {
            throw new Exception('Job has not yet completed', 500);
        }
    });
    // Print the results
    if ($operation->isComplete()) {
        $results = $operation->results();
        foreach ($results as $result) {
            $alternative = $result->alternatives()[0];
            printf('Transcript: %s' . PHP_EOL, $alternative['transcript']);
            printf('Confidence: %s' . PHP_EOL, $alternative['confidence']);
        }
    }
}
# [END speech_transcribe_async_gcs]

transcribe_async_gcs("session_audio", $filename, "en-US", $options);

【问题讨论】:

    标签: php google-cloud-platform google-cloud-storage


    【解决方案1】:

    抱歉,PHP 不是我精通的语言,但我怀疑您没有(而且必须)安装 Cloud Storage 客户端库,以便您的代码可以访问它。这将解释其报告缺少该类的原因。

    PHP 客户端库page 包括两个备选方案。一个适用于您使用 Composer,第二个 - 可能是您想要的 - 直接下载,您需要为您的代码正确路径。

    前段时间,我写了一篇简短的博客post,为 Google 支持的每种语言提供了一个简单的示例(使用云存储)。也许它也会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多