我正在尝试上传一个zip文件到谷歌驱动器与一个php脚本作为cron作业在Fedora 33 x86-64。我已经尝试了几种不同的方法,但在这一点上我没有任何运气。我已经创建了一个服务帐户并下载了凭据,创建了oauth concent屏幕,安装了Google Drive API,oauth客户端id,等等……无论如何,当我从shell运行脚本时,我得到了一个非常奇怪的。任何帮助都将不胜感激。
<?php
require_once 'vendor/autoload.php';
$googleClient = new Google_Client();
$googleClient->setAuthConfig('***************************.json');
$googleClient->addScope(Google_Service_Drive::DRIVE);
$googleClient->setApplicationName('******************');
$googleClient->setAccessType('offline');
$googleClient->getAccessToken();
$drive = new Google_Service_Drive($googleClient);
$file = new Google_Service_Drive_DriveFile($googleClient);
$file->setName('Mysql-Backup.zip');
$file->setMimeType('application/zip');
try {
$return = $drive->files->create($file, array('data' => file_get_contents("Mysql-Backup.zip"), 'uploadType' => 'multipart', 'supportsAllDrives' => 'true'));
echo "Sucessful upload of " . $file['name'] . ".";
}
catch (Exception $exc) {
echo $exc->getMessage() . "\n";
}
?>如果我在stackoverflow.com上的代码和帖子做得不正确,请原谅我。好的,我已经按照你的建议列出了文件,你确实是正确的,文件上传到了我的服务帐户,而不是我的个人驱动器。我还尝试了您的建议,即使用web界面将文件夹从我的个人帐户共享到服务帐户。然而,当我再次尝试时,它仍然不起作用。这会给我一个错误"PHP致命错误:未捕获的错误:调用/ Google_Service_Drive_Resource_Permissions::insert() /google-drive/google-drive.php:25中未定义的方法驱动器“。我已经参考了https://developers.google.com/resources/api-libraries/documentation/drive/v3/php/latest/class-Google_Service_Drive_Permission.html和https://developers.google.com/drive/api/v2/reference/permissions/insert#php,但我仍然不明白我错过了什么。我不知道这是不是不赞成,但我愿意支付某人,如果他们有一个加仑工作的php类的说明。关于这一点的文档到处都是,旧的东西不能工作,新的东西也不能工作。在这一点上,我正在猛击自己的头。
<?php
require_once 'vendor/autoload.php';
$googleClient = new Google_Client();
$googleClient->setAuthConfig('****');
$googleClient->addScope("https://www.googleapis.com/auth/drive.file");
$googleClient->setApplicationName('MySQL-Backup');
$googleClient->setAccessType('offline');
$googleClient->getAccessToken();
$drive = new Google_Service_Drive($googleClient);
$file = new Google_Service_Drive_DriveFile($googleClient);
$file->setName('Mysql-Backup.zip');
$file->setMimeType('application/zip');
$file->setParents('****');
try
{
$return = $drive->files->create($file, array('data' => file_get_contents("Mysql-Backup.zip"), 'uploadType' => 'multipart', 'supportsAllDrives' => 'true'));
echo "Sucessful upload of " . $file['name'] . " File ID:" . $return->id . "\n";
$newPermission = new Google_Service_Drive_Permission();
//$newPermission->setValue('***');
$newPermission->setType('user');
$newPermission->setRole('editor');
echo $return->id . "\n";
try
{
$drive->permissions->insert($return->id, $newPermission);
}
catch (Exception $exc)
{
echo 'Caught exception: ', $exc->getMessage(), "\n";
}
}
catch (Exception $exc)
{
echo 'Caught exception: ', $exc->getMessage(), "\n";
}
$optParams = array('fields' => '*');
$myfiles = $drive->files->ListFiles($optParams);
foreach( $myfiles as $k => $file )
{
echo "{$file['name']} - {$file['id']} ---- " . $file['mimeType'] . "\n";
try
{
// subfiles
$sub_files = $drive->files->listFiles(array('q' => "'{$file['id']}' in parents"));
foreach( $sub_files as $kk => $sub_file )
{
echo "{$sub_file['name']} - {$sub_file['id']} ---- " . $sub_file['mimeType'] . "\n";
}
}
catch (Exception $exc)
{
echo 'Caught exception: ', $exc->getMessage(), "\n";
}
}
?>发布于 2021-01-08 15:26:08
您的消息说明它已上载。我怀疑如果你做一个file.list
$optParams = array(
'fields' => '*'
);
$service->files->ListFiles($optParams);您会发现该文件实际上已上载。
你的问题是文件上传的位置。你的问题应该是“我的文件是在哪里上传的?”
您需要记住,服务帐户是虚拟用户,他们有自己的google驱动器帐户。您已将文件上载到服务帐户驱动器帐户。要查看上传的文件,唯一的方法是以编程方式执行上面建议的file.list。
尝试使用服务帐户电子邮件地址共享您的个人驱动器帐户上的目录,就像您通过web界面与任何其他用户共享一样。然后,当您上传文件时,在您的驱动器帐户中设置parents =文件夹的文件夹id。文件将上载到您的帐户,而不是服务帐户拥有的帐户。
只需记住在上传后设置文件的权限,因为服务帐户上传了文件,它将是文件的所有者。
发布于 2021-01-22 04:24:41
我建议你遵循这个https://packagist.org/packages/miqoo1996/google-drive
示例中的所有功能都可以在您的平台上轻松实现Google Drive。
非常感谢。
https://stackoverflow.com/questions/65622527
复制相似问题