miqoo1996/google-drive

Laravel/PHP 用于 Google Drive API 的包

dev-master 2021-01-21 03:12 UTC

This package is auto-updated.

Last update: 2024-09-21 11:17:52 UTC


README

  1. 认证 GoogleDrive
  2. 添加文件/目录
  3. 编辑文件/目录
  4. 下载文件/目录
  5. 删除文件/目录

演示 - 认证和获取文件

简单演示

安装

composer require miqoo1996/google-drive
  • 在您的 .env 文件中添加这些参数
GDrive_client_id=
GDrive_client_secret=
GDrive_client_redirect_url=

说明

// Get the API client and construct the service object.
$api = new \Miqoo1996\GDrive\Repositories\GoogleDriveRepository();
  • 要获取登录或更新账户(access_token)的 URL,请参见以下示例
  <a href="<?= $api->getClient()->createAuthUrl(); ?>">Click To Login or change current account.</a> 
$api = new \Miqoo1996\GDrive\Repositories\GoogleDriveRepository();

// or you can check this in session.
if ($api->getClient()->getAccessToken()) {
    
    /-------------------------- Example --------------------------/
    
    // get all files populated on the root path
    $api->getRootFiles(function (Google_Service_Drive_DriveFile $file, int $line) {
        if ($line === 1) {
            echo '<h1>All files/folders in root DIR.</h1>';
        }

        printf("<p>ID = %s || File = %s || mimeType = %s</p><hr>", $file->getId(), $file->getName(), $file->mimeType);
    });
    
    /-------------------------- Example --------------------------/
    
    
    /-------------------------- Example --------------------------/
    
    // get files with the given folder id.
    // as well as you can use with callback like the above example.
    $files = $api->getFilesByFolder('-- folder id --');
    
    /-------------------------- Example --------------------------/
    
    
    /-------------------------- Example --------------------------/

    // create new folder
    $file = $api->createFolder('folder_name', 'aaaa');

    // update Folder by ID
    $api->updateFolder('new-aaaa', $file->getId());
    
    /-------------------------- Example --------------------------/
    
    
    /-------------------------- Example --------------------------/

    // delete file/folder with the given id
    $api->deleteFile('1E8930HbZLjnl_shQWoIuudK0RZQWxpgb');
    
    /-------------------------- Example --------------------------/
    
    
    /-------------------------- Example --------------------------/

    // add file on drive with the given path
    // To create a file we can use this function as well
    $api->uploadFile('root', __DIR__, '00.html');
    
    /-------------------------- Example --------------------------/
    
    

    /*------- File Creation and Modifications  -------*/

    $file = $api->createFile([
        'name' => 'test.csv',
        'parents' => ['root'],
        'data' => '77777',
        'mimeType' => 'text/csv',
        'uploadType' => 'multipart'
    ]);

    $api->updateFile($file->getId(), [
        'name' => 'test_new.csv',
        'parents' => ['root'],
        'data' => 'new 77777',
        'mimeType' => 'text/csv',
        'uploadType' => 'multipart'
    ]);

    // file will be stored/downloaded with this name: $id.mimetype
    // As its written on there documentation, Google Drive allows to download binary files only.
    $api->downloadFile('125a2fLbBZwKTjj-D0IuoM3EQwtTCDQv2m6E9OW_iKOY', __DIR__);
}