找不到storageclient类?

时间:2018-09-27 02:44:46

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

我正拼命地试图找出如何通过PHP(我知道的唯一语言)创建一个简单的音频转录脚本(对于更长的音频文件)。我收到错误找不到类'Google \ Cloud \ Storage \ StorageClient'

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

我还输入了 gcloud auth application-default print-access-token ,它打印出访问令牌,但是我不知道我应该如何处理其他令牌比我复制并粘贴到控制台外壳提示符中的“ 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);

1 个答案:

答案 0 :(得分:0)

道歉,PHP不是我精通的语言,但是我怀疑您没有(也必须)安装Cloud Storage客户端库,以便您的代码可以访问它。这可以解释其有关该类丢失的报告。

PHP客户端库page包括两种选择。如果您使用的是Composer,则一种方法适用,另一种(可能是您想要的方法)适用于直接下载,您需要为该代码正确路径。

不久前,我写了一个简短的博客post,其中提供了一个简单的示例(使用Cloud Storage),介绍了Google支持的每种语言。也许它也会帮助您。

相关问题