Spatie media library upload to Amazon s3 bucket subfolder - laravel

I am using spatie media library in Laravel with the below code to upload images to s3 bucket
$file = $this->fileUploadModel
->addMediaFromDisk($file->path(), 's3')
->toMediaCollection();
The image is saved to s3 bucket on the format:
my_bucket_name/1/image_name.png
my_bucket_name/2/image_name.png
etc
However I want a way to store the images inside an images folder ie.
my_bucket_name/images/1/image_name.png
By using only Laravel you can do that with a simple:
$file->store('images','s3');
How can I do that?

I implemented the following solution:
Un file config/filesystems.php I defined the following disk:
's3-media' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'root' => 'images/media',
],
In position root of the s3-media array I indicate the main path where the images will be stored.

Related

File upload to s3 not working after setting visibility to public

Trying to configure Laravel to upload to my aws s3 bucket. It is working fine until I change visibility to public. Then it seems to work or at least it does not show any error but nothing gets uploaded to aws.
Here is the part in my register controller where I am uploading a profile picture
if($request->hasFile('avatar')) {
$file = $request->file('avatar');
$filename = $file->getClientOriginalName();
$file->storeAs('avatars/' . $user->id, $filename, 's3');
$user->update([
'avatar' => $filename,
]);
}
And here is the configuration for s3 in filesystems.php
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
'visibility' => 'public',
],
Without the 'visibility' => 'public', it is working fine but as soon as I add it nothing gets uploaded anymore.
I had this before, you need to Edit Object Ownership to ACLs enabled. By default it is disabled which means that all objects. on the bucket is yours as a private, but ACLs let you choose if you won't object to being public or private.
if you will create a new s3 bucket, in Object Ownership enabled ACLs.
or if you have already created a bucket go to permissions and edit Object Ownership to enable ACLs.
What worked for me was enabling public access to my bucket and adding bucket policy that makes the files in question publicly readable. You can read the official docs here.
Also, make sure to remove the "visibility" => "public" line from Laravel as this prevents the upload from taking place.

Performance issue with streaming files from digial ocean storage in laravel 5.5

I am using digital ocean space for storing files. I am having performance issue while reading files from there, specially streaming files in a route. Here is my code structure.
Default Driver:
FILESYSTEM_DRIVER=digitalocean
Disk Config:
'digitalocean' => [
'driver' => 's3',
'key' => env('DIGITALOCEAN_SPACES_KEY'),
'secret' => env('DIGITALOCEAN_SPACES_SECRET'),
'endpoint' => env('DIGITALOCEAN_SPACES_ENDPOINT'),
'region' => env('DIGITALOCEAN_SPACES_REGION'),
'bucket' => env('DIGITALOCEAN_SPACES_BUCKET'),
],
Controller function:
public function view($path)
{
return Storage::response($filePath);
}
This is really slow since it fetches file from the cloud storage and then streams in a url. Is there any way to speedup the process ?

Download files stored in AWS S3 from Laravel Nova results in bug

We are using Nova package for our admin backend inside our Laravel App. All files and images are stored in the AWS S3 bucket.
After trying to download a file from Nova, the download begins with the name download.json and Server Error message.
Files are stored correctly in S3, I can check it manually, also the path to them inside S3 is correctly stored in the database.
Here is the code we use to create a download field in Nova
->download(function(){
return Storage::disk('s3')->download($this->name);
})
->onlyOnDetail()
$this->name holds the path inside the s3 bucket.
config/filesystems.php is also defined:
'disks' => [
...
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
Nova documentation did not helped me on this problem. Any input would be really helpful.
UPDATE:
The problem was not in code but in Configuration.
Without changing the configuration the following code did help:
Text::make('File/Document', function() {
$linkToFile = Storage::disk('s3')->temporaryUrl($this->name, now()->addMinutes(1));
return 'Download file';
})
->asHtml(),
Hard to see any issue without seeing the complete function, but make sure your name property $this->name has the same value as your remote file 'key' as shown in your Amazon s3 bucket.
Also, make sure the your .env file is correct and contains the following value:
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_access_key
AWS_DEFAULT_REGION=your_default_region
AWS_BUCKET=your_bucket_name
AWS_URL=your_url #if applicable
Hope this makes sense.
Edit: One more thing, in filesystem.php, this line:
'url' => env('AWS_URL'),
was changed in Laravel 6.x according to this bug and became:
'endpoint' => env('AWS_URL'),

Choose Amazon S3 storage class using Laravel Filesystem

Amazon S3 has different storage classes, with different price brackets.
I was wondering if there's a way I can choose a storage class in Laravel's Filesystem / Cloud Storage solution?
It would be good to choose a class on a per upload basis so I can choose throughout my application, not just once in a configuration file.
To pass additional options to flysystem you have to use getDriver()
Storage::disk('s3')->getDriver()->put(
'sample.txt',
'This is a demo',
[
'StorageClass' => 'REDUCED_REDUNDANCY'
]
);
This can be used in Laravel 7
Storage::disk('s3')->put(
'file path',
$request->file('file'),
[
'StorageClass' => 'STANDARD|REDUCED_REDUNDANCY|STANDARD_IA|ONEZONE_IA|INTELLIGENT_TIERING|GLACIER|DEEP_ARCHIVE',
]
);
You can use putFileAs() Method As Well Like Below
Storage::disk('s3')->putFileAs(
'file path',
$request->file('file'),
'file name',
[
'StorageClass' => 'STANDARD|REDUCED_REDUNDANCY|STANDARD_IA|ONEZONE_IA|INTELLIGENT_TIERING|GLACIER|DEEP_ARCHIVE',
]
);
I can't really find this answer on the internet. Hope it helps someone else.
If you want to set StorageClass on the disk level (once for every upload).
You can change it on the config\filesystems.php
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
'options' => [
'StorageClass' => 'INTELLIGENT_TIERING'
]
],
Other possible options...
'ACL',
'CacheControl',
'ContentDisposition',
'ContentEncoding',
'ContentLength',
'ContentType',
'Expires',
'GrantFullControl',
'GrantRead',
'GrantReadACP',
'GrantWriteACP',
'Metadata',
'RequestPayer',
'SSECustomerAlgorithm',
'SSECustomerKey',
'SSECustomerKeyMD5',
'SSEKMSKeyId',
'ServerSideEncryption',
'StorageClass',
'Tagging',
'WebsiteRedirectLocation',
Ref: thephpleague/flysystem-aws-s3-v3

Storing files outside the Laravel 5 Root Folder

I am developing a laravel 5 project and storing image files using imagine. I would want to store my image files in a folder outside the project's root folder. I am stuck at the moment The external folder where image files are supposed to be stored, I want to make it accessible via a sub-domain something like http://cdn.example.com Looking towards your solutions.
The laravel documentation could give you a helping hand.
Otherwise you could go to config/filesystems.php and add your own custom storage path for both local and production:
return [
'default' => 'custom',
'cloud' => 's3',
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path().'/app',
],
'custom' => [
'driver' => 'custom',
'root' => '../path/to/your/new/storage/folder',
],
's3' => [
'driver' => 's3',
'key' => 'your-key',
'secret' => 'your-secret',
'region' => 'your-region',
'bucket' => 'your-bucket',
],
'rackspace' => [
'driver' => 'rackspace',
'username' => 'your-username',
'key' => 'your-key',
'container' => 'your-container',
'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',
'region' => 'IAD',
],
],
];
get ur path name from base_path(); function, then from the string add your desired folder location.
suppose ur
base_path() = '/home/user/user-folder/your-laravel-project-folder/'
So ur desired path should be like this
$path = '/home/user/user-folder/your-target-folder/'.$imageName;
make sure u have the writing and reading permission
You can move all or a part of storage folder in any folder of yours in your server. You must put a link from old to new folder.
ln -s new_fodler_path older_folder_path
You can make a new virtual host to serve the new folder path.

Resources