Laravel Storage Dropbox Integration Example
Hi Friends,
This tutorial shows you laravel dropbox api example. you can understand a concept of laravel dropbox integration example. you'll learn laravel dropbox storage. This article goes in detailed on how to use dropbox in laravel. Alright, let’s dive into the steps.
Dropbox is a cloud-based file storage and sharing service that allows users to access their files from anywhere and share them with others. The company was founded in 2007 by Drew Houston and Arash Ferdowsi and is headquartered in San Francisco, California. With Dropbox, users can upload and store files such as documents, photos, and videos, which are then synced to all their devices. This means that users can access their files from their desktop computer, laptop, smartphone, or tablet, as long as they have an internet connection. Dropbox also offers offline access, which allows users to work on files even when they're not connected to the internet.
In this example, we will use Dropbox and Spatie Package to store images, files etc. upload on your dropbox account.
So, let's follow the below step to make it done this example.
Step 1: Install Laravel
first of all, we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:
composer create-project laravel/laravel example-app
Step 2: Configure Dropbox as Driver
In this step, we will install spatie/flysystem-dropbox package for access dropbox api. Then we will add dropbox storage configuration on AppServiceProvider. Then also we will configure as driver on config/filesystems.php file. so, let's run following command to install package.
composer require spatie/flysystem-dropbox
Next, update AppServiceProvider file.
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Filesystem;
use Spatie\Dropbox\Client as DropboxClient;
use Spatie\FlysystemDropbox\DropboxAdapter;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Storage::extend('dropbox', function (Application $app, array $config) {
$adapter = new DropboxAdapter(new DropboxClient(
$config['authorization_token']
));
return new FilesystemAdapter(
new Filesystem($adapter, $config),
$adapter,
$config
);
});
}
}
Now, let's add new driver on config/filesystems.php file.
config/filesystems.php
<?php
return [
...
...
'disks' => [
'dropbox' => [
'driver' => 'dropbox',
'key' => env('DROPBOX_APP_KEY'),
'secret' => env('DROPBOX_APP_SECRET'),
'authorization_token' => env('DROPBOX_AUTH_TOKEN'),
],
],
]
Step 3: Get Dropbox API Key & Secret & Access Token
In this step, we will need to dropbox api key, secret and access token to store files on Dropbox Account.
1. You need to go here Dropbox App Console and you need to create new app as like the following screenshot:
2. After click on "Create App" button, you need to add name of App.
3. Add "files.content.write" permission to your app.
4. Here you need to get API Key, API Secret and Access token from here and need to set on .env file.
You need to set all configuration on your .env file.
.env
DROPBOX_APP_KEY=wx0qfff2ly...
DROPBOX_APP_SECRET=gqdi00g...
DROPBOX_AUTH_TOKEN=sl.Bc5_aUxtGFDZP6...
Step 4: Upload File using Dropbox
Now, you can uplad files in your dropbox account using Storage facade. You can see the below example:
Example Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
/* Simple Put File */
Storage::disk('dropbox')->put('demo.txt', "Hello");
/* Simple Get File Content */
Storage::disk('dropbox')->get('demo.txt');
/* Simple Store File */
$path = Storage::disk('dropbox')->putFileAs(
'avatars', $request->file('avatar'), $request->user()->id
);
dd('DONE');
}
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Download File from URL to Storage Example
- How to Truncate String in Laravel?
- Laravel Amazon S3 File Upload Tutorial
- How to Create Custom Blade Directive in Laravel?
- Laravel Sanctum SPA API Authentication Example
- Laravel Carbon Get Year from Date Example
- Laravel Carbon addMonths() | Laravel Carbon Add Months Example
- How to Delete File from Public Folder / Storage Folder in Laravel?
- Laravel Collection Map Method Example
- How to Create Table using Migration in Laravel?
- How to Display Image from Storage Folder in Laravel?
- How to Store Data in Cache in Laravel?