在我的AWS EC2實例上,我可以使用Youtube API(PHP)上傳視頻。我的視頻文件存儲在AWS S3上,所以當上傳到Youtube時,我的PHP腳本會將文件下載到EC2,然後使用Youtube API上傳到Youtube。使用Youtube API v3可恢復上傳(在AWS EC2上)上傳較大視頻時出錯
對於小於100MB的視頻,這看起來可以正常工作,但對於大於此視頻的文件卻沒有問題。
任何人都有任何想法可能是什麼問題?下面是用於執行上傳庫代碼的一個片段:
$snippet = new \Google_Service_YouTube_VideoSnippet();
if (array_key_exists('title', $data))
{
$snippet->setTitle($data['title']);
}
if (array_key_exists('description', $data))
{
$snippet->setDescription($data['description']);
}
if (array_key_exists('tags', $data))
{
$snippet->setTags($data['tags']);
}
if (array_key_exists('category_id', $data))
{
$snippet->setCategoryId($data['category_id']);
}
/* ------------------------------------
#. Set the Privacy Status
------------------------------------ */
$status = new \Google_Service_YouTube_VideoStatus();
$status->privacyStatus = $privacyStatus;
/* ------------------------------------
#. Set the Snippet & Status
------------------------------------ */
$video = new \Google_Service_YouTube_Video();
$video->setSnippet($snippet);
$video->setStatus($status);
/* ------------------------------------
#. Set the Chunk Size
------------------------------------ */
$chunkSize = 1 * 1024 * 1024;
/* ------------------------------------
#. Set the defer to true
------------------------------------ */
$this->client->setDefer(true);
/* ------------------------------------
#. Build the request
------------------------------------ */
$insert = $this->youtube->videos->insert('status,snippet', $video);
/* ------------------------------------
#. Upload
------------------------------------ */
$media = new \Google_Http_MediaFileUpload(
$this->client,
$insert,
'video/*',
null,
true,
$chunkSize
);
/* ------------------------------------
#. Set the Filesize
------------------------------------ */
$media->setFileSize(filesize($path));
/* ------------------------------------
#. Read the file and upload in chunks
------------------------------------ */
$status = false;
$handle = fopen($path, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSize);
$status = $media->nextChunk($chunk);
}
fclose($handle);
/* ------------------------------------
#. Set the defer to false again
------------------------------------ */
$this->client->setDefer(true);
我打開CURLOPT_VERBOSE看到上傳時的輸出和下面是它的一個摘錄:
* Connection #0 to host www.googleapis.com left intact
* Hostname www.googleapis.com was found in DNS cache
* Trying 74.125.130.95...
* Connected to www.googleapis.com (74.125.130.95) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
* Server certificate: *.googleapis.com
* Server certificate: Google Internet Authority G2
* Server certificate: GeoTrust Global CA
> PUT /upload/youtube/v3/videos?part=status%2Csnippet&uploadType=resumable&upload_id=AEnB2Ur4P0Fpf3GsMWAoc6dUIeXmQXuAfdhuIP1jIbQJPyVQm0aqZ0NGuljcT-U0c5hW2wrE-AepZMghHOzpvkUZb-I9zBruAw HTTP/1.1
Host: www.googleapis.com
Accept: */*
content-range: bytes 19922944-20971519/388638417
content-type: application/json; charset=UTF-8
content-length: 1048576
* We are completely uploaded and fine
< HTTP/1.1 308 Resume Incomplete
< X-GUploader-UploadID: AEnB2Ur4P0Fpf3GsMWAoc6dUIeXmQXuAfdhuIP1jIbQJPyVQm0aqZ0NGuljcT-U0c5hW2wrE-AepZMghHOzpvkUZb-I9zBruAw
< Range: bytes=0-20971519
< X-Range-MD5: 5f5ce9766e0b823edb947b9a93ee3fd6
< Content-Length: 0
< Date: Thu, 07 Apr 2016 09:01:06 GMT
< Server: UploadServer
< Content-Type: text/html; charset=UTF-8
< Alternate-Protocol: 443:quic
< Alt-Svc: quic=":443"; ma=2592000; v="32,31,30,29,28,27,26,25"
<
* Connection #0 to host www.googleapis.com left intact
* Hostname www.googleapis.com was found in DNS cache
* Trying 74.125.130.95...
* Connected to www.googleapis.com (74.125.130.95) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
* Server certificate: *.googleapis.com
* Server certificate: Google Internet Authority G2
* Server certificate: GeoTrust Global CA
> PUT /upload/youtube/v3/videos?part=status%2Csnippet&uploadType=resumable&upload_id=AEnB2Ur4P0Fpf3GsMWAoc6dUIeXmQXuAfdhuIP1jIbQJPyVQm0aqZ0NGuljcT-U0c5hW2wrE-AepZMghHOzpvkUZb-I9zBruAw HTTP/1.1
Host: www.googleapis.com
Accept: */*
content-range: bytes 20971520-22020095/388638417
content-type: application/json; charset=UTF-8
content-length: 1048576
* We are completely uploaded and fine
* Operation timed out after 100000 milliseconds with 0 bytes received
* Closing connection 0
[Google_IO_Exception]
Operation timed out after 100000 milliseconds with 0 bytes received
看來上傳後有一段時間,上傳會突然停止。我認爲這可能是由於服務器的內存限制,所以我升級了EC2實例類型,它解決了這個視頻文件的問題,但是當上傳另一個更大的文件時,同樣的事情又會發生。