问题
Environment: PHP
Working with folders and files: I created a folder and 4 sub folders within that folder in my google drive.
Google DRIVE API
- Create a new console project and automatically enable the Drive API
- Service account: Create Service account and download the configuration file.
Install the Google Client Library.
composer require google/apiclient:^2.0
I shared these folder and 4 sub folders with service account.
Expected Result: Folders and files should be create in sub Folders.
Actual Result:
1 day it works properly but in next day 404 error updated in error log.
{ "error": {"errors": [{ "domain": "global","reason": "notFound","message": "File not found: .","locationType": "parameter","location": "fileId"} ],"code": 404, "message": "File not found: ." } }
MY Code:
//======Send To Google Drive======//
function send_google_drive($id,$fileno,$filename1,$filename2){
global $wpdb;
require(ABSPATH.'/wp-content/themes/enemat/googledrives/vendor/autoload.php');
$client = getClient();
$service = new Google_Service_Drive($client);
$folderslink = "";
$docusignorgs = "";
$contractdrivelink = "";
$parentfolders = get_option("parentfolders");
$crmfolders = get_option("crmfolders");
try{
//===Root Folder's Sub Folder======//
$results = $service->files->listFiles();
$parents = "";
foreach ($results->getFiles() as $item) {
if ($item['name'] == $parentfolders) {
$parents = $item['id'];
break;
}
}
$optParams = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
'q' => "'".$parents."' in parents"
);
$results = $service->files->listFiles($optParams);
$parentsarray = array();
foreach ($results->getFiles() as $item) {
$parentsarray[$item['name']] = $item->getId();
}
$parentid = $parentsarray[$crmfolders];
//===Root Folder's Sub Folder======//
//===Sub Folder's Folders======//
$optParams1 = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
'q' => "'".$parentid."' in parents"
);
$results = $service->files->listFiles($optParams1);
//===Sub Folder's Folders======//
//==========CREATE FOLDER====//
$childid = "";
foreach ($results->getFiles() as $item) {
if ($item['name'] == $fileno) {
$childid = $item['id'];
break;
}
}
if(empty($childid)){
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $fileno,
'parents'=>array($parentid),
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array(
'fields' => 'id'));
$folderId = $file->id;
}else{
$folderId = $childid;
}
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($folderId, $newPermission);
//==========CREATE FOLDER====//
if(!empty($filename1)){
try{
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => "OFFRE DE PRIME - ".$fileno."",
'parents' => array($folderId)
));
$content = file_get_contents($filename1);
$files = $service->files->create($fileMetadata, array(
'data' => $content,
'uploadType' => 'resumable',
'fields' => 'id'));
$fileids = $files->id;
$docusignorgs = "https://drive.google.com/open?id=".$fileids."";
$folderslink = "https://drive.google.com/drive/folders/".$folderId."";
@unlink(ABSPATH."wp-content/themes/enemat/pdfs/".basename($filename1));
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($fileids, $newPermission);
}catch (Google_ServiceException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Google_IOException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
} catch (Google_Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}
}
if(!empty($filename2)){
try{
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => "contrat projet - ".$fileno."",
'parents' => array($folderId)
));
$content = file_get_contents($filename2);
$files = $service->files->create($fileMetadata, array(
'data' => $content,
'uploadType' => 'resumable',
'fields' => 'id'));
$fileids1 = $files->id;
$contractdrivelink = "https://drive.google.com/open?id=".$fileids1."";
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($fileids1, $newPermission);
@unlink(ABSPATH."wp-content/themes/enemat/pdfs/".basename($filename2));
}catch (Google_ServiceException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Google_IOException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
} catch (Google_Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}
}
$SQLUPDATE = "UPDATE wp_clientinfo SET docusignorgs='".$docusignorgs."',folderid='".$folderslink."',contractdrivelink='".$contractdrivelink."' WHERE id='".$id."'";
$wpdb->query($SQLUPDATE);
}catch (Google_ServiceException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Google_IOException $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
} catch (Google_Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}catch (Exception $e) {
$file_save = false;
file_put_contents(ABSPATH."wp-content/themes/enemat/ajax_scripts/error_logs.txt",$e->getMessage(),FILE_APPEND);
}
}
//======Send To Google Drive======//
来源:https://stackoverflow.com/questions/52817476/google-drive-api-folder-creation-and-upload-files-by-service-account-in-google-d