【解決方法】PHP を使用して複数の大きなファイルを Google ドライブ API に分割してアップロードするにはどうすればよいですか?

プログラミングQA


次のコードを使用して、複数の大きなファイルを分割してアップロードする必要があります。

foreach アップロードの最初のファイルは正しく機能しますが、2 番目のファイルのアップロードから問題が発生します。
戻り値の型 $service->files->listFilesGuzzleHTTP\Psr7\Requestおそらく、メディアのアップロードが回線で延期されているためです。 $client->setDefer(true);

スレッドと同様に、ループが各アップロードの完了を待ってから次のファイルに進む解決策を見つけたいと考えています。 あるいは、複数のアップロードを同時に (非同期/並列) 送信することは可能ですか?

私が試したこと:

PHP
foreach ($files as $fileDatas) {
$file = new Google\Service\Drive\DriveFile();
    $file->name = $fileName;
    $chunkSizeBytes = 1 * 1024 * 1024;

    // Call the API with the media upload, defer so it doesn't immediately return.
    $client->setDefer(true);
    $request = $service->files->create($file);

    // Create a media file upload to represent our upload process.
    $media = new Google\Http\MediaFileUpload($client, $request, $type, null, true, $chunkSizeBytes);
    $media->setFileSize($size);

    // Upload the various chunks. $status will be false until the process is complete.
    $status = false;
    $handle = fopen($filePath, "rb");
    while (!$status && !feof($handle)) {
        // read until you get $chunkSizeBytes from TESTFILE
        // fread will never return more than 8192 bytes if the stream is read
        // buffered and it does not represent a plain file
        // An example of a read buffered file is when reading from a URL
        $chunk = readVideoChunk($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
    }

    // The final value of $status will be the data from the API for the object
    // that has been uploaded.
    $result = false;
    if ($status != false) {
        ...
    }

    fclose($handle);
}

コメント

タイトルとURLをコピーしました