Recent Post

Cách lưu trữ dữ liệu trong Laravel với Google Drive

I. Cài đặt
Google cũng đã cung cấp API cho chúng ta tương tác, nhưng để sử dụng thuận tiện hơn, nhanh gọn hơn thì mình sử dụng package Flysystem Adapter for Google Drive
Cài đặt package cho Google Drive API V3
composer require nao-pon/flysystem-google-drive:~1.1

Cài đặt package cho Google Drive API V2
composer require nao-pon/flysystem-google-drive:~1.0.0

II. Khởi tạo:
B1: Tạo Provider GoogleDriveServiceProvider và GoogleDriveAdapter.
B2: Thêm code trong Provider
- Trong app/Providers/GoogleDriveServiceProvider.php ở public function boot() ta thêm code sau bên trong:
        \Storage::extend('google', function($app, $config) {
            $client = new \Google_Client();
            $client->setClientId($config['clientId']);
            $client->setClientSecret($config['clientSecret']);
            $client->refreshToken($config['refreshToken']);
            $service = new \Google_Service_Drive($client);

            $options = [];
            if(isset($config['teamDriveId'])) {
                $options['teamDriveId'] = $config['teamDriveId'];
            }

            $adapter = new GoogleDriveAdapter($service, $config['folderId'], $options);

            return new \League\Flysystem\Filesystem($adapter);
        });
- Trong app/Providers/GoogleDriveAdapter.php
Sửa class GoogleDriveAdapter extends ... thành
class GoogleDriveAdapter extends \Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter
Thêm function sau

    public function getService()
    {
            return $this->service;
    }
B3: Trong config/app.php thêm
GoogleDriveServiceProvider::class,
Vào trong providers

'providers' => [
    // ...
    App\Providers\GoogleDriveServiceProvider::class,
    // ...
],
B4: Trong config/filesystems.php disks thêm
'google' => [
        'driver' => 'google',
        'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
        'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
        'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
        'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
        'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
    ],
Ví dụ
'disks' => [
    // ...
    'google' => [
        'driver' => 'google',
        'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
        'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
        'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
        'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
        'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
    ],
    // ...
],
B5: Tạo Google Drive API keys để lấy Client IDClient secret
- Vào https://console.developers.google.com/ bấm vào Select project > New project
- Ở trang tiếp theo điền tên project ở (1) và Project ID (2) nếu muốn sau đó bấm Save


Sau đó ta có màn hình như trên bấm vào Library
- Ở trang tiếp theo tìm Google Drive API và nhấn vào đó chọn Enable

- Ở trang tiếp theo chọn Create credentials
- Ở trang này điền
+ Which API are you using? : Google Drive API
+ Where will you be calling the API from? : Web browser (Javascript)
+ What data will you be accessing? : User data
+ Sau đó nhấn vào What...?
- Sau khi nhấn vào đó sẽ hiện ra 1 box chọn Setup ... thì mở ở tab mới
+ Sau khi ở trang đó chọn Extenal bấm vào Create

+ Ở trang mới chỉ cần điền mục Application name sau đó lướt xuống dưới chọn Save

- Sau khi đã lưu thì trở lại trang trước đó chọn Not now kéo xuống dưới chọn Refresh sau đó sẽ hiện ra nút Create OAuth ... thì nhấn vào sau đó nhấn Done


- Ở trang tiếp theo chọn tên OAuth Client vừa tạo
- Copy lại Client IDClient secret ở trang tiếp theo
- Ở mục Authorized redirect URIs bấm Add URL điền
https://developers.google.com/oauthplayground
Sau đó nhấn Save

B6: Tạo Refresh Token
+ Bấm nút hình con ốc bên phải
+ Tích chọn Use your own OAuth credentials và điền Client IDClient secret ban nãy vào nhấn Close
+ Ở cột bên trái tìm Drive API V3 > https://www.googleapis.com/auth/drive nhấn vào và chọn Authorize APIs

- Ở trang tiếp theo chọn tài khoản gmail muốn lấy token
+ Nếu hiện trang This app isn't verified thì chọn Advanced > Goto ...
+ Ở trang tiếp theo bấm Allow liên tục

- Nếu thành công thì sẽ được trả về trang https://developers.google.com/oauthplayground/?code=...
+ Nhấn vào Exchange ...
+ Nếu tự động chuyển sang Step 3 bấm lại Step 2
+ Tích vào Auto-refresh the token before it expires.
+ Copy mã Refresh token
B7: Lấy ID folder (hoặc ID Team drive)
- Vào drive của bạn vào thư mục bất kì thì có link dạng
https://drive.google.com/drive/u/0/folders/0AKYy7sVtdEiFUk9PVA
+ Thì 0AKYy7sVtdEiFUk9PVA chính là ID cần tìm.
+ Nếu muốn tệp tin tải lên ở thư mục gốc thì giá trị ID là null
- Đặc biệt nếu muốn dùng Team Drive thì cũng vào Team Drive lấy ID tương tự
+ Nếu muốn up lên thư mục gốc của Team Drive thì ID foldernull
+ Nếu muốn up lên thư mục bất kì thì vào thư mục cần lấy trong Team Drive lấy ID folder
- Nếu không muốn dùng Team Drive thì ID Team Drivenull
B8: Thêm vào .env

FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=xxx.apps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxx
GOOGLE_DRIVE_FOLDER_ID=null
GOOGLE_DRIVE_TEAM_DRIVE_ID=null
Sửa lại giá trị Client ID, Client Secret, Refresh Token, Folder ID, Team Drive ID như những hướng dẫn ở trên là xog bước thiết lập.

B9: Sử dụng
Vậy là có thể sử dụng.

0 Nhận xét