Upload Files
Once you have a cloud storage authorized and connected you can begin uploading files.
$model->dropbox->upload('/path/to/source.pdf', 'My File.pdf');
Queued uploads
By default a queue job is created to handle the upload. This avoids locking up your app during a large upload and ensures any upload failures are retried.
If you really don't want the upload to be queued you can pass false in as a third argument.
$model->dropbox->upload('/path/to/source.pdf', 'My File.pdf', false);
Uploading from S3
You can upload files from an S3 bucket by using the s3://
protocol. This assumes, of course, you have your AWS credentials setup in the .env file and read access to the bucket where the files are stored.
$model->dropbox->upload('s3://bucket-name/source.pdf', 'My File.pdf');
Uploading from URL
You can also upload from a URL.
$model->dropbox->upload('https://www.somewebsite.com/source.pdf', 'My File.pdf');
If you are using Dropbox, this will use the save_url
method. Dropbox will pull the file directly from the URL.
If you are using Google, this package will download the file from the URL first, and then do a normal upload.
Uploading an Eloquent model
In all the above examples we are explicitly providing the source and destination paths as strings. There is also an option to just point to an Eloquent model for the upload.
Perhaps you have a files
table in your database. Edit the Files
model, implement the UploadTarget
contract, and add two accessors like this:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use STS\StorageConnect\Contracts\UploadTarget;
class File extends Model implements UploadTarget {
public function getUploadSourcePathAttribute()
{
// Return the source path for this file
}
public function getUploadDestinationPathAttribute()
{
// Return the destination path for the upload
}
...
Now you can upload an instance of this model directly:
$model->dropbox->upload($file);
One big advantage of this approach is that all the upload related events will now include this target model, for your reference.
Event::listen(UploadSucceeded::class, function($event) {
// $event->target is a reference to the $file model you asked to upload
});