[英]How to upload a file on google drive with php Google Drive API
[英]Google Drive API upload 1 fiile from php
打開頁面時在 output 上 - 文件 ID:1qG8tteyVhAbB_rbu_VUvaE9ReqnSjEAh ......但在谷歌驅動器上沒有新文件......我想將文件上傳到 cron 但現在我只想下載 ASB-Background-3.png 並結束......
require_once './google-api-php-client/vendor/autoload.php';
use Google\Client;
use Google\Service\Drive;
function uploadBasic()
{
try {
$client = new Client();
//$client->useApplicationDefaultCredentials();
$client->setAuthConfig('./google-api-php-client/1710-6c50418be6b2.json');
$client->addScope(Drive::DRIVE);
$driveService = new Drive($client);
$fileMetadata = new Drive\DriveFile(array(
'name' => 'ASB-Background-3.png'));
$mimeType=mime_content_type($fileMetadata);
$content = file_get_contents('./google-api-php-client/ASB-Background-3.png');
$file = $driveService->files->create($fileMetadata, array([
'data' => $content,
'mimeType' => $mimeType,
'uploadType' => 'multipart',
'fields' => 'id']));
printf("File ID: %s\n", $file->id);
return $file->id;
} catch(Exception $e) {
echo "Error Message: ".$e;
}
}
uploadBasic();
調試它的最快方法是執行File.list 。 這將告訴您文件是否已上傳。
您沒有設置父母 yin 您的元數據,因此該文件將已上傳到根目錄。
請記住,如果您使用的是服務帳戶,文件將上傳到服務帳戶谷歌驅動器帳戶,而不是您的個人驅動器帳戶。
要上傳到您的個人驅動器帳戶,您需要在驅動器帳戶上創建一個目錄,使用服務帳戶 email 地址與您的服務帳戶共享該目錄。 服務帳戶 email 地址可以在 json 密鑰文件中找到,它是唯一一個帶有@的。
然后將元數據中的父母設置為您驅動器帳戶上的文件夾
$fileMetadata = new Drive\DriveFile(array(
'parents' => { 'FolderId' }
'name' => 'ASB-Background-3.png'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.