![](/img/trans.png)
[英]Google drive SDK, get files uploaded by other user with my app
[英]Files uploaded with Google Drive SDK not viewable
我创建了一个简单的PHP脚本来与Google的Drive SDK一起使用。 该计划是最终我们将使用Google云端硬盘作为某些网络内容的CDN形式(我们的公司已经升级到1TB)。
该代码在一定程度上有效,它可以成功地验证并上传文件。 问题在于,该文件始终是损坏的,无法通过云端硬盘本身或通过下载进行查看。
该代码相对简单,仅从Wikipedia获取图像并尝试上传:
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Drive.php';
require_once 'Google/Service/Oauth2.php';
$client = new Google_Client();
//$client->setUseObjects(true);
//$client->setAuthClass('apiOAuth2');
$client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
$client->setClientId('***');
$client->setClientSecret('***');
$client->setRedirectUri('***');
$client->setAccessToken(authenticate($client));
// initialise the Google Drive service
$service = new Google_Service_Drive($client);
$data = file_get_contents('http://upload.wikimedia.org/wikipedia/commons/3/38/JPEG_example_JPG_RIP_010.jpg');
// create and upload a new Google Drive file, including the data
try
{
//Insert a file
$file = new Google_Service_Drive_DriveFile($client);
$file->setTitle(uniqid().'.jpg');
$file->setMimeType('image/jpeg');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
));
}
catch (Exception $e)
{
print $e->getMessage();
}
print_r($createdFile);
?>
执行print_r
语句,我们获得有关文件的信息。 但是,如前所述,该文件不可查看,并且似乎已损坏。 任何人都可以阐明问题的根源吗?
在对文档进行了更多挖掘之后(当前的公共文档已严重过时),我发现有必要发送另一个参数作为insert()
函数的body
参数(函数调用中的第二个参数)的一部分。
使用以下内容:
$createdFile = $service->files->insert($doc, array(
'data' => $content,
'mimeType' => 'image/jpeg',
'uploadType' => 'media', // this is the new info
));
我能够使一切正常。 我将问题留在这里,因为我认为这将非常有用,直到Google实际更新PHP API的文档为止。
您的代码似乎没问题。 之后,您是否尝试过从Google云端硬盘下载此文件并进行查看? 我也知道它有点愚蠢,但您尝试使用file_get_contents()之后立即在磁盘上写入文件。 您知道只是确定100%变坏的点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.