Google云端硬盘api上传失败

时间:2017-05-16 16:41:00

标签: php api upload google-api google-drive-api

我想将一个文件从网址上传到google drive,但我收到了错误消息。我用本地文件测试了它,它的工作原理。这有什么问题?

这是我的代码:

<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/GoogleApi.php';

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
}

/**
 * Get a chunk of a file.
 *
 * @param resource $handle
 * @param int      $chunkSize
 *
 * @return string
 */
function readFileChunk($handle, int $chunkSize) {
    $byteCount = 0;
    $giantChunk = '';
    while (!feof($handle)) {
        $chunk = fread($handle, 8192);
        $byteCount += strlen($chunk);
        $giantChunk .= $chunk;
        if ($byteCount >= $chunkSize) {

            return $giantChunk;
        }
    }

    return $giantChunk;
}

/**
 * Get Remote File Size
 *
 * @param string $url
 *
 * @return int
 */
function remoteFileSize($url) {
    # Get all header information
    $data = get_headers($url, true);
    # Look up validity
    if (isset($data['Content-Length'])) {
        return (int) $data['Content-Length'];
    }

    return 0;
}

/**
 * Get Remote File Size
 *
 * @param string $url
 *
 * @return string
 */
function remoteMimeType($url) {
    $ext = pathinfo($url, PATHINFO_EXTENSION);
    $mimeType = 'application/octetstream';

    switch ($ext) {
        case 'zip'    :
            $mimeType = 'application/zip';
            break;
        case 'rar'    :
            $mimeType = 'application/x-rar-compressed';
            break;
        case '7z'    :
            $mimeType = 'application/x-7z-compressed';
            break;
    }

    return $mimeType;
}

/**
 * Upload a file to the google drive.
 */
try {
    $googleApi = new GoogleApi();
    $client = $googleApi->getClient();
    $service = new Google_Service_Drive($client);
    $getFile = new Google_Service_Drive_DriveFile();
    $url = 'http://ipv4.download.thinkbroadband.com/1MB.zip';
    $getFile->name = 'Test File.zip';
    $chunkSizeBytes = 0.5 * 1024 * 1024;

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

    // Get file mime type
    $mimeType = remoteMimeType($url);

    // Create a media file upload to represent our upload process.
    $media = new Google_Http_MediaFileUpload(
        $client,
        $request,
        $mimeType,
        null,
        true,
        $chunkSizeBytes
    );
    $media->setFileSize(remoteFileSize($url));

    // Upload the various chunks. $status will be false until the process is
    // complete.
    $status = false;

    $handle = fopen($url, '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 = readFileChunk($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.
    $uploadedFileId = '';
    if ($status !== false) {
        $uploadedFileId = $status['id'];
        print_r($uploadedFileId);
    } else {
        throw new Exception('Upload failed');
    }

    fclose($handle);
} catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
}

0 个答案:

没有答案