PHPackages                             pimphand/gdrive - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. pimphand/gdrive

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

pimphand/gdrive
===============

Laravel Google Drive storage package with S3-style API, OAuth, streaming upload/download, and quota sync.

v1.2.2(2d ago)017↑2723.5%MITPHPPHP ^8.2

Since Jun 7Pushed 2d agoCompare

[ Source](https://github.com/pimphand/gdrive)[ Packagist](https://packagist.org/packages/pimphand/gdrive)[ Docs](https://github.com/pimphand/gdrive)[ RSS](/packages/pimphand-gdrive/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (7)Versions (8)Used By (0)

pimphand/gdrive
===============

[](#pimphandgdrive)

Laravel package untuk menggunakan **Google Drive sebagai cloud storage** dengan API mirip S3. Mendukung OAuth2, upload streaming langsung ke Drive (tanpa simpan ke disk lokal), download/preview dengan byte-range, sync quota, dan enkripsi token yang kompatibel dengan [pimpdrive](https://github.com/pimphand/gdrive) Node backend.

[![Latest Version on Packagist](https://camo.githubusercontent.com/1ee05e1dca7544b29acd8a1a9a9e1b30e138aa06a021872301766a0c262f87b8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70696d7068616e642f6764726976652e737667)](https://packagist.org/packages/pimphand/gdrive)[![Total Downloads](https://camo.githubusercontent.com/3c83afe61af39a995581dba2eb4331ed8581549090fdc15c4d58ab06103354a9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70696d7068616e642f6764726976652e737667)](https://packagist.org/packages/pimphand/gdrive)[![License](https://camo.githubusercontent.com/fb49d27d5e040d8dd96ec124d70a2479f43c61c185c208f650bc5fa0bf23ce29/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f70696d7068616e642f6764726976652e737667)](https://packagist.org/packages/pimphand/gdrive)

---

Fitur
-----

[](#fitur)

- **S3-style API** — `put`, `get`, `delete`, `list`, `exists`, `url`, `quota`
- **OAuth2 Google Drive** — auth URL, exchange code, auto token refresh
- **Streaming upload** — file langsung ke Google Drive tanpa write ke disk server
- **Streaming download** — proxy file dari Drive dengan dukungan `Range` header
- **Google Workspace export** — Docs/Sheets/Slides otomatis di-export saat download/preview
- **Folder app `pimpdrive`** — semua file fisik disimpan di root folder Drive (konfigurabel)
- **AES-256-GCM encryption** — kompatibel dengan `crypto.ts` di pimpdrive backend
- **Laravel auto-discovery** — service provider &amp; facade terdaftar otomatis
- **Artisan commands** — `gdrive:seed-config`, `gdrive:sync`

---

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11, 12, atau 13
- Google Cloud project dengan **Google Drive API** enabled
- Google OAuth Client ID &amp; Secret

---

Instalasi
---------

[](#instalasi)

### Via Packagist (production)

[](#via-packagist-production)

```
composer require pimphand/gdrive
```

### Via path repository (development lokal)

[](#via-path-repository-development-lokal)

Tambahkan di `composer.json` project Laravel Anda:

```
{
    "repositories": [
        {
            "type": "path",
            "url": "../gdrive",
            "options": {
                "symlink": true
            }
        }
    ],
    "require": {
        "pimphand/gdrive": "@dev"
    }
}
```

Kemudian jalankan:

```
composer require pimphand/gdrive:@dev
```

### Publish config (opsional)

[](#publish-config-opsional)

```
php artisan vendor:publish --tag=gdrive-config
```

File config akan tersedia di `config/gdrive.php`. Tanpa publish, config default package sudah otomatis di-merge.

### Publish contoh aplikasi lengkap (disarankan untuk pertama kali)

[](#publish-contoh-aplikasi-lengkap-disarankan-untuk-pertama-kali)

Satu perintah ini akan membuat **controller, routes, views, dan token store** — langsung bisa diakses di `/gdrive`:

```
php artisan vendor:publish --tag=gdrive-example
```

**Memperbarui stub setelah upgrade package** — `vendor:publish` tidak menimpa file yang sudah ada. Gunakan salah satu:

```
php artisan vendor:publish --tag=gdrive-example --force
# atau (selalu menimpa dengan versi terbaru dari package)
php artisan gdrive:publish-example
```

File yang dibuat otomatis:

FileFungsi`routes/gdrive.php`Route `/gdrive` (auto-loaded oleh package)`app/Http/Controllers/GDrive/GDriveController.php`Dashboard, upload, download, preview, delete`app/Http/Controllers/GDrive/GDriveAuthController.php`OAuth connect / callback / disconnect`app/Http/Controllers/GDrive/GDriveApiController.php`API upload chunk + sync`app/Services/GDrive/FileGoogleDriveTokenStore.php`Simpan token terenkripsi di `storage/app/gdrive/``resources/views/gdrive/*.blade.php`Halaman demo`public/js/gdrive-demo.js`Upload langsung ke Drive + sync`public/css/gdrive-drive.css`Style demo`config/gdrive.php`Konfigurasi package`gdrive.env.example`Template variabel `.env`**Tidak perlu edit `routes/web.php`** — package otomatis memuat `routes/gdrive.php` jika file tersebut ada.

#### Setup cepat (hanya .env)

[](#setup-cepat-hanya-env)

1. Publish example (lihat di atas)
2. Salin variabel dari `gdrive.env.example` ke `.env`:

```
GDRIVE_CLIENT_ID=your-client-id.apps.googleusercontent.com
GDRIVE_CLIENT_SECRET=your-client-secret
GDRIVE_REDIRECT_URI=http://localhost:8000/gdrive/callback
```

3. Di Google Cloud Console, tambahkan redirect URI: `http://localhost:8000/gdrive/callback`
4. Jalankan server:

```
php artisan serve
```

5. Buka **** → klik **Connect Google Drive**

#### Route contoh

[](#route-contoh)

MethodURLFungsiGET`/gdrive`Dashboard (quota, daftar file, upload)GET`/gdrive/connect`Mulai OAuth GoogleGET`/gdrive/callback`OAuth callbackPOST`/gdrive/disconnect`Putus koneksi DrivePOST`/gdrive/upload`Upload fileGET`/gdrive/files/{id}/download`Download fileGET`/gdrive/files/{id}/preview`Preview inlineDELETE`/gdrive/files/{id}`Hapus file---

Konfigurasi Google Cloud
------------------------

[](#konfigurasi-google-cloud)

1. Buka [Google Cloud Console](https://console.cloud.google.com/)
2. Buat project (atau gunakan yang sudah ada)
3. Enable **Google Drive API**
4. Buat **OAuth 2.0 Client ID** (tipe: Web application)
5. Tambahkan **Authorized redirect URI**, contoh:
    - `http://localhost:8000/auth/google/callback`
    - `https://yourdomain.com/auth/google/callback`

---

Environment Variables
---------------------

[](#environment-variables)

Tambahkan ke `.env` project Laravel:

```
# OAuth client (wajib)
GDRIVE_CLIENT_ID=your-client-id.apps.googleusercontent.com
GDRIVE_CLIENT_SECRET=your-client-secret
GDRIVE_REDIRECT_URI=http://localhost:8000/auth/google/callback

# Nama folder root di Google Drive (default: pimpdrive)
GDRIVE_APP_FOLDER=pimpdrive

# Kunci enkripsi token (gunakan string acak panjang)
GDRIVE_TOKEN_ENCRYPTION_KEY=your-random-secret-key

# OAuth tokens akun yang terhubung (setelah OAuth flow)
GDRIVE_ACCESS_TOKEN=ya29....
GDRIVE_REFRESH_TOKEN=1//0g....
GDRIVE_TOKEN_EXPIRES_AT=2026-06-07T12:00:00+00:00
```

### Alias kompatibel pimpdrive backend

[](#alias-kompatibel-pimpdrive-backend)

Package juga membaca variabel `GOOGLE_*` dan `TOKEN_ENCRYPTION_KEY` sebagai fallback:

Package (preferred)pimpdrive backend fallback`GDRIVE_CLIENT_ID``GOOGLE_CLIENT_ID``GDRIVE_CLIENT_SECRET``GOOGLE_CLIENT_SECRET``GDRIVE_REDIRECT_URI``GOOGLE_REDIRECT_URI``GDRIVE_APP_FOLDER``GOOGLE_DRIVE_APP_FOLDER``GDRIVE_TOKEN_ENCRYPTION_KEY``TOKEN_ENCRYPTION_KEY``GDRIVE_ACCESS_TOKEN``GOOGLE_ACCESS_TOKEN``GDRIVE_REFRESH_TOKEN``GOOGLE_REFRESH_TOKEN`Token bisa berupa **plain text** atau **terenkripsi** format `iv:tag:ciphertext` (AES-256-GCM).

---

Validasi config
---------------

[](#validasi-config)

```
php artisan gdrive:seed-config

# Tampilkan nilai terenkripsi (untuk seed database)
php artisan gdrive:seed-config --show-encrypted
```

---

Penggunaan Dasar
----------------

[](#penggunaan-dasar)

### Facade (paling sederhana)

[](#facade-paling-sederhana)

```
use Pimphand\GDrive\Facades\GDrive;

// Upload file
$file = GDrive::put(
    fileName: 'report.pdf',
    body: $request->getContent(),          // string, resource, atau StreamInterface
    mimeType: 'application/pdf',
    sizeBytes: (string) $request->header('Content-Length'), // opsional
);

// Response: ['id' => '...', 'name' => '...', 'mime_type' => '...', 'size' => '...']

// Download (return StreamedResponse)
return GDrive::get($file['id']);

// Preview inline (video/PDF di browser)
return GDrive::get($file['id'], disposition: 'inline');

// Byte-range (video seek)
return GDrive::get($file['id'], range: $request->header('Range'));

// Hapus file
GDrive::delete($file['id']);

// List semua file di folder app
$files = GDrive::list();

// Cek file ada
GDrive::exists($file['id']);

// URL view Google Drive
$viewUrl = GDrive::url($file['id']);

// Sync quota
$quota = GDrive::quota();
```

### Dependency Injection

[](#dependency-injection)

```
use Pimphand\GDrive\GoogleDriveStorage;

class FileController extends Controller
{
    public function __construct(
        private readonly GoogleDriveStorage $storage,
    ) {}

    public function upload(Request $request)
    {
        $file = $this->storage->put(
            $request->file('file')->getClientOriginalName(),
            fopen($request->file('file')->getRealPath(), 'r'),
            $request->file('file')->getMimeType(),
            (string) $request->file('file')->getSize(),
        );

        return response()->json($file);
    }

    public function download(string $fileId)
    {
        return $this->storage->get($fileId);
    }
}
```

### Static factory

[](#static-factory)

```
use Pimphand\GDrive\GoogleDriveStorage;

$storage = GoogleDriveStorage::fromConfig();
$file = $storage->put('photo.jpg', file_get_contents('/path/to/photo.jpg'), 'image/jpeg');
```

---

Upload Controller (contoh lengkap)
----------------------------------

[](#upload-controller-contoh-lengkap)

```
