I'm trying to upload files into a google drive in a specific folder using a service account. It all appears to work - but the files always show up with 0 size. I've found a couple of very similar questions on here but none of the answers have worked.
The code I'm using is:
putenv('GOOGLE_APPLICATION_CREDENTIALS = ' . $home_path . '../google-oauth-credentials.json');
try {
$gclient = new Google\Client();
$gclient->useApplicationDefaultCredentials();
$gclient->addScope('https://www.googleapis.com/auth/drive');
$driveService = new Google\Service\Drive($gclient);
$content = file_get_contents('testfile.txt');
$fileMetadata = new Google\Service\Drive\DriveFile(array(
'name' => 'test.txt',
'parents' => array('{PARENT_ID}'),
'description' => 'Test Description'
));
$file = $driveService->files->create($fileMetadata, array([
'data' => $content,
'mimeType' => 'text/plain',
'uploadType' => 'multipart',
'fields' => 'id'
]));
printf("File ID: %s\n", $file->id);
echo '<br>';
echo '<pre>' . print_r($file,1) . '</pre>';
echo '<h1>Content:</h1>';
echo ($content);
} catch(Exception $e) {
echo "Error Message: ".$e;
};
With {PARENT_ID} being the ID of the folder the files should be saved in.
This is meant to be uploading pdf files but I've switched to a simple text file to rule out a mime issue. With the PDF's I was using "application/pdf" as the mimeType, I've also tried "application/octet-stream" for both pdf and text and I've tried putting the mimeType in the $fileMetaData as well as in the request body as it currently is.
I've tried using 'uploadType' of 'media' as well as some other answers had suggested.
I've also tried with and without the 'fields' => 'id' whose purpose I can't seem to find in the API docs - but which was shown in various examples and other answers. I get the same results with or without it.
No luck. With any of that I get files with correct meta data....but they never have any content and are just 0 bytes in size.
I added the lines echoing out $content to confirm that file_get_contents is loading the correct file and its content to rule out a problem with what I'm passing to the request.
I'm loading the google api client through composer with
composer require google/apiclient:^2.12.1
And it seems to be working correctly as I can create google sheets, and edit their contents as well as create files...just the files always wind up having 0 size.
CodePudding user response:
Try dropping the 'uploadType' => 'multipart',
// Upload a file to the users Google Drive account
try{
$filePath = "image.png";
$fileMetadata = new Drive\DriveFile();
$fileMetadata->setName('testfile.txt');
$fileMetadata->setMimeType('text/plain');
$content = file_get_contents($filePath);
$mimeType=mime_content_type($filePath);
$request = $service->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => $mimeType,
'fields' => 'id'));
printf("File ID: %s\n", $request->id);
}
catch(Exception $e) {
// TODO(developer) - handle error appropriately
echo 'Message: ' .$e->getMessage();
}
CodePudding user response:
I found the problem.
The error was in:
$file = $driveService->files->create($fileMetadata, array([
'data' => $content,
'mimeType' => 'text/plain',
'uploadType' => 'multipart',
'fields' => 'id'
]));
Which came more or less verbatim from: https://developers.google.com/drive/api/guides/manage-uploads
I'm also not sure why the 'fields' parameter is included since v3 create doesn't appear to support that.
Note the "array([])" It's defining an array inside the array.
Removing the [] or the array() solves the problem by correctly passing the intended array instead of passing an array containing the intended array.