我上传文件的某些格式,并且在谷歌驱动器中有空文件的文件。为什么不明白
我安装google api客户端
"name": "google/apiclient",
"version": "1.1.7",
并尝试在谷歌dics中上传文件 我获得了HWIO bundle的访问令牌帮助,添加范围
scope:
"https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive"
并像这样获取accesToken
{"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
"expires_in":3600, "id_token":"TOKEN", "created":1320790426}
beign我尝试上传存在的文件(doc,pdf,img)
$client = $this->get('artel.google.api.client');
/** @var CodeUserReference[] $accessToken */
$accessToken = $this->get('doctrine.orm.entity_manager')->getRepository('ArtelProfileBundle:CodeUserReference')
->getReferenceAccessClient($user);
if ($accessToken) {
$client->setAccessToken($accessToken[0]->getAccessTokenClient());
} else {
$view = $this->view(['No accessToken was found for this user id'], 400);
return $this->handleView($view);
}
$service = new \Google_Service_Drive($client->getGoogleClient());
$file = new \Google_Service_Drive_DriveFile();
$file->setName($request->request->get('title')
? $request->request->get('title')
: $request->files->get('file')->getClientOriginalName()
);
$file->setDescription($request->request->get('description'));
$file->setMimeType($request->request->get('mimeType')
? $request->request->get('mimeType')
: $request->files->get('file')->getClientMimeType()
);
// Set the parent folder.Google_Service_Drive_ParentReferen, this class not find in google/apiclient, version": "1.1.7", I don\'t know why..(
if ($request->request->get('parentId') != null) {
$parent = new Google_Service_Drive_ParentReferen();
$parent->setId($request->request->get('parentId'));
$file->setParents(array($parent));
}
try {
$data = $request->files->get('file');
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => $request->request->get('mimeType')
? $request->request->get('mimeType')
: $request->files->get('file')->getClientMimeType(),
'uploadType' => 'media'
));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
return View::create()
->setStatusCode(200)
->setData([$createdFile]);
} catch (\Exception $e) {
$view = $this->view((array) $e->getMessage(), 400);
return $this->handleView($view);
}
有回复
{
"internal_gapi_mappings": [],
"model_data": [],
"processed": [],
"collection_key": "spaces",
"capabilities_type": "Google_Service_Drive_DriveFileCapabilities",
"capabilities_data_type": "",
"content_hints_type": "Google_Service_Drive_DriveFileContentHints",
"content_hints_data_type": "",
"id": "0B2_i_Tc5Vr8UWV9Jc2psQkhqS3M",
"image_media_metadata_type": "Google_Service_Drive_DriveFileImageMediaMetadata",
"image_media_metadata_data_type": "",
"kind": "drive#file",
"last_modifying_user_type": "Google_Service_Drive_User",
"last_modifying_user_data_type": "",
"mime_type": "application/msword",
"name": "Resume — Symfony Backend Developer, PHP, Shuba Ivan.doc",
"owners_type": "Google_Service_Drive_User",
"owners_data_type": "array",
"permissions_type": "Google_Service_Drive_Permission",
"permissions_data_type": "array",
"sharing_user_type": "Google_Service_Drive_User",
"sharing_user_data_type": "",
"video_media_metadata_type": "Google_Service_Drive_DriveFileVideoMediaMetadata",
"video_media_metadata_data_type": ""
}
我所有人都提到了这个:
/tmp/phpaPpHBp
我使用此doc v3因为在“name”中:“google / apiclient”,“version”:“1.1.7”没有找到函数“insert”,
所以,mime_type正确但在emty中,只有这样 - / tmp / phpaPpHBp 我尝试了一些uploadType - media,multipart,resumable但stil empty 当尝试pdf mime_type = application / pdf但仍然是emty 我做错了什么以及为什么我在谷歌驱动器中有emty文件?
任何人,谁知道,帮助
答案 0 :(得分:0)
添加file_get_content并正常工作:
if ($request->request->get('parentId') !== null) {
$parent = new \Google_Service_Drive_ParentReference();
$parent->setId($request->request->get('parentId'));
$file->setParents(array($parent));
}
$insertArray = [
'mimeType' => isset($fileUpload) ? $fileUpload->getClientMimeType() : $request->request->get('mimeType')
];
if (isset($fileUpload)) {
$insertArray ['uploadType'] = 'media';
$insertArray ['data'] = file_get_contents($fileUpload);
}
try {
$createdFile = $service->files->insert($file, $insertArray);
return View::create()
->setStatusCode(200)
->setData([$createdFile]);
} catch (\Exception $e) {
$view = $this->view((array) self::ERROR_OCCURRED . $e->getMessage(), 400);
return $this->handleView($view);
}