我希望使用 PHP 脚本和 Google Drive API(将设置为 cron 作业)将 MySQL 备份从网络服务器推送到 Google Drive。
我知道这是一个服务器到服务器的应用程序,因此需要 Google 服务帐户(我知道可以使用用户帐户并刷新访问令牌 - 但我认为这不是正确的用例因为我不希望有任何用户交互?)。
我已在 Cloud Console 中设置了 Google 服务帐号。使用我的常规帐户,我与服务帐户共享一个目录,并且我尝试使用以下脚本上传测试文件:
function getClient()
{
$client = new Google_Client();
if ($credentials_file = checkServiceAccountCredentialsFile())
{
// set the location manually
$client->setAuthConfig($credentials_file);
}
else
{
echo missingServiceAccountDetailsWarning();
return;
}
$client->setApplicationName('Backups');
$client->setScopes(Google_Service_Drive::DRIVE);
$guzzleClient = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, ), ));
$client->setHttpClient($guzzleClient);
return $client;
}
Run Code Online (Sandbox Code Playgroud)
$file_to_upload = 'test.txt';
if(file_exists($file_to_upload))
{
// Get the API client and construct the service …
Run Code Online (Sandbox Code Playgroud) php google-api google-api-client google-drive-api google-api-php-client