benjamincrozat/laravel-dropbox-driver
Laravel Flysystem driver that lets you use Dropbox as a storage disk in Laravel. Configure via filesystem settings and use the standard Storage API to upload, download, list, and manage files on Dropbox like any other disk.
Install the Package:
composer require benjamincrozat/laravel-dropbox-driver
Publish the configuration file:
php artisan vendor:publish --provider="BenjaminCrozat\DropboxDriver\DropboxDriverServiceProvider" --tag="config"
Configure Dropbox Disk:
Add the Dropbox disk to config/filesystems.php:
'disks' => [
'dropbox' => [
'driver' => 'dropbox',
'access_token' => env('DROPBOX_ACCESS_TOKEN'),
'root' => env('DROPBOX_ROOT', ''),
'throw' => env('DROPBOX_THROW', false),
],
],
Set your access token in .env:
DROPBOX_ACCESS_TOKEN=your_access_token_here
First Use Case:
Upload and retrieve a file using Laravel’s Storage facade:
// Upload a file
Storage::disk('dropbox')->put('folder/file.txt', 'Hello Dropbox!');
// Retrieve a file
$contents = Storage::disk('dropbox')->get('folder/file.txt');
// Generate a public URL
$url = Storage::disk('dropbox')->url('folder/file.txt');
Verify Connection: Check if the file exists in Dropbox:
if (Storage::disk('dropbox')->exists('folder/file.txt')) {
echo "File exists in Dropbox!";
}
config/filesystems.php (Dropbox disk settings).Illuminate\Support\Facades\Storage (Laravel’s storage methods).README.md for advanced features like shared links or metadata handling.File Operations:
Use Laravel’s Storage facade methods seamlessly:
// Upload
Storage::disk('dropbox')->put('path/to/file', $fileContents);
// Download
$fileContents = Storage::disk('dropbox')->get('path/to/file');
// Delete
Storage::disk('dropbox')->delete('path/to/file');
// Check existence
if (Storage::disk('dropbox')->exists('path/to/file')) { ... }
Shared Links: Generate public URLs with optional settings:
$url = Storage::disk('dropbox')->url('path/to/file', [
'shared' => true,
'expires' => now()->addHours(1),
]);
Directory Handling: Create directories and list files:
// Create a directory
Storage::disk('dropbox')->makeDirectory('path/to/new-folder');
// List files in a directory
$files = Storage::disk('dropbox')->files('path/to/folder');
Hybrid Storage: Combine Dropbox with other disks (e.g., S3, local) for redundancy:
$disk = Storage::disk(env('APP_ENV') === 'production' ? 'dropbox' : 'local');
File Uploads from Requests: Handle file uploads directly from HTTP requests:
$file = request()->file('file');
Storage::disk('dropbox')->putFile('uploads/', $file);
Event Handling:
Listen to filesystem events (e.g., created, deleted) for Dropbox:
Storage::disk('dropbox')->addListener(function ($event) {
// Handle Dropbox file events
});
Metadata Management: Use Dropbox’s metadata API via the package’s extension points:
$metadata = Storage::disk('dropbox')->metadata('path/to/file');
Large File Handling: For files >150MB, use Dropbox’s chunked uploads (requires manual implementation or SDK bypass):
// Example: Bypass package for large files
$dropbox = new \Dropbox\Client(env('DROPBOX_ACCESS_TOKEN'));
$dropbox->uploadFile('large-file.txt', fopen('path/to/local/file', 'r'));
Dynamic Disk Configuration: Override disk settings at runtime:
Storage::extend('dropbox', function ($app) {
return new \BenjaminCrozat\DropboxDriver\DropboxDriver(
$app['config']['filesystems.disks.dropbox']
);
});
Custom File URLs:
Extend the url method for Dropbox-specific logic:
Storage::disk('dropbox')->extend('url', function ($path, $options = []) {
$url = parent::url($path, $options);
return str_replace('dl=0', 'dl=1', $url); // Force download
});
Flysystem Events: Subscribe to Flysystem events for Dropbox:
Storage::disk('dropbox')->addListener('*', function ($event) {
// Log or process all Dropbox events
});
Token Management:
env() or a dedicated secrets manager (e.g., Vault). Rotate tokens periodically via a cron job or Laravel scheduler.Rate Limiting:
Large File Limits:
Permission Denied:
files.metadata.read scope).Path Normalization:
$path = strtolower(trim($path, '/'));
Shared Link Expiry:
$url = Storage::disk('dropbox')->url('file.txt', [
'shared' => true,
'expires' => now()->addDays(7),
]);
Enable Debug Logging:
Add to config/logging.php:
'channels' => [
'dropbox' => [
'driver' => 'single',
'path' => storage_path('logs/dropbox.log'),
'level' => 'debug',
],
],
Then log Dropbox-specific events:
\Log::channel('dropbox')->debug('Custom Dropbox log message');
Check SDK Errors: Dropbox SDK errors may not bubble up cleanly. Catch them explicitly:
try {
Storage::disk('dropbox')->put('file.txt', 'content');
} catch (\Dropbox\Exception\ApiException $e) {
\Log::error('Dropbox API Error: ' . $e->getMessage());
}
Verify API Response:
Use Storage::disk('dropbox')->metadata() to inspect file properties:
$metadata = Storage::disk('dropbox')->metadata('file.txt');
\Log::info('File metadata:', $metadata);
Test with a Sandbox Account: Use Dropbox’s developer sandbox to avoid hitting real account limits during testing.
Root Path:
root config key in filesystems.php maps to a Dropbox folder. Ensure it exists or the package will fail silently.root => 'my-app-files' creates a folder my-app-files in your Dropbox root.Throw Exceptions:
throw => true in the disk config to rethrow Dropbox API exceptions instead of returning false:
'dropbox' => [
'driver' => 'dropbox',
'throw' => true, // Enable exception throwing
],
Environment-Specific Configs: Override disk settings per environment:
'disks' => [
'
How can I help you explore Laravel packages today?