简体   繁体   中英

Google Drive API upload 1 fiile from php

on output when open page - File ID: 1qG8tteyVhAbB_rbu_VUvaE9ReqnSjEAh... But on google drive no new files... i want upload file to cron but now i want only download ASB-Background-3.png and end...

require_once './google-api-php-client/vendor/autoload.php';
use Google\Client;
use Google\Service\Drive;

function uploadBasic()
{
    try {
        $client = new Client();
        //$client->useApplicationDefaultCredentials();
        $client->setAuthConfig('./google-api-php-client/1710-6c50418be6b2.json');
        $client->addScope(Drive::DRIVE);
        $driveService = new Drive($client);
        
        $fileMetadata = new Drive\DriveFile(array(
                    'name' => 'ASB-Background-3.png'));
        
        $mimeType=mime_content_type($fileMetadata);
        $content = file_get_contents('./google-api-php-client/ASB-Background-3.png');
        $file = $driveService->files->create($fileMetadata, array([
                    'data' => $content,
                    'mimeType' => $mimeType,
                    'uploadType' => 'multipart',
                    'fields' => 'id']));
        printf("File ID: %s\n", $file->id);
        return $file->id;
    } catch(Exception $e) {
            echo "Error Message: ".$e;
    }   
}

uploadBasic();

how to debug issue

The fastest way to debug this is to do aFile.list . This will tell you if in fact the file was uploaded.

You are not setting parents yin your meta data, so the file will have been uploaded to the root directory.

service account

Remember if you are using a service account that the files are uploaded into the service accounts google drive account, not your personal drive account.

To upload to your personal drive account you would need to create a directory on your drive account, share that directory with your service account using the service account email address. The service account email address can be found in the json key file its the only one with an @.

Then set parents in the meta data to the folder on your drive account

$fileMetadata = new Drive\DriveFile(array(
                'parents' => { 'FolderId' }
                'name' => 'ASB-Background-3.png'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM