upload to s3 bucket folder with laravel - laravel

I am currently using this packages for amazon file uploads and it works, the only problem is i don't know how to specify a folder in my chosen bucket.
Package Used - "aws/aws-sdk-php": "~3.0",
This is how i currently upload to the bucket
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$image = $request->file('image');
$t = Storage::disk('s3')->put($imageName, file_get_contents($image), 'public');
$imageName = Storage::disk('s3')->url($imageName);

You need to create a path and use in put method instead of $imageName to store in that particular path of bucket. It'll create the folder itself according to path.
For e.g. if you set the path as $path = "folder_1/folder_2/file.pdf", s3 driver will store file.pdf in folder_2 which is inside folder_1.
$imageName = time().'.'.$request->image->getClientOriginalExtension();
$image = $request->file('image');
//image stored in folder name image_folder
$path = "image_folder/".$imageName;
$t = Storage::disk('s3')->put($path, file_get_contents($image));

Related

How to delete image from storage in restfull api

$employee = Employee::find($id);
Storage::delete($employee->photo_file_path . $employee->photo_file_type);
$file = $request->image;
$path = $file->storeAs('public/images', $id . '.' . $file->getClientOriginalExtension());
$employee->photo_file_name = $id;
$employee->photo_file_type = $file->getClientOriginalExtension();
$employee->photo_file_path = $path;
when uploading photos, I want the previous photo to be deleted first and then insert a new photo again, I use Storage::delete. I have checked the public folder but the previous photo was not deleted, instead the photo was added
If you are uploading images in public folder then you need to add public before your path
Storage::delete('public/'.$employee->photo_file_path . $employee->photo_file_type);
if(file_exists(public_path('upload/bio.png'))){unlink(public_path('upload/bio.png'));
}else{dd('File does not exists.');}

How to upload file from my local storage into S3?

Looking at some samples how Laravel work with AWS S3 survice I see examples of code like :
$path = $request->file('image')->store('images', 's3');
But when I try to upload file from my local storage :
$adImageDestDirectory = '/public/' . ‘subdirectory_path’;
$path = Storage::get($adImageDestDirectory . $adImageItem->image)->store('images', 's3');
I got error:
Call to a member function store() on string
Which is valid way ?
Thanks!
If you need to upload files from local storage to aws S3 bucket then you need to copy files from local: use function file_get_contents()
Storage::disk('s3')->put('filename.jpg', file_get_contents($adImageDestDirectory );
You can do this to store the file to s3
$file = $request->file('attachment');
$fileName = time() . $file->getClientOriginalName();
$filePath = 'uploads/' . $fileName;
Storage::disk('s3')->put($filePath, file_get_contents($file), 'public');
$urlPath = Storage::disk('s3')->url($filePath);

Storing image in local drive and saving path in db

I would like to save an image in my local drive, and insert its path to a DB, and then preview the image through the website. I have no idea how to do it and also I have not found anything similar anywhere.
Try this one
$image = $request->file('image');
$filename = time() . $image->getClientOriginalName();
$destination_path = public_path('/image/');
$image->save($destination_path . $filename);
$image->image_path= $destination_path;
Hope this helps :)
You can try to do this:
// To save image on local drive and insert path to db.
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$path = $request->image->store('public/images');
$path = basename($path);
$image = new Images();
$image->photo = $path;
$image->save();
}
I hope it would be helpful.

Incorrect URL from AWS S3 with Laravel File Upload

I'm uploading an image to S3 with Laravel as follows:
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url($imageFileName);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}
The image uploads successfully but I need to store the URL in my database. This is being called here:
$image = Storage::cloud()->url($imageFileName);
The issue is the URL being returned, it looks like this:
http://test-env.XXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
Hence:
http://mentr-test-env.2w8sh3esch.us-west-2.elasticbeanstalk.com/profile/
is somewhat-correct. But the next piece is missing the 'profile' sub-folder, and obviously starts at HTTPS again:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/6_52644340.jpg
It would appear I'm getting two halves of the link in a single string. I don't edit the $image variable anywhere else.
The correct link is:
https://elasticbeanstalk-us-west-2-123XXX456XXX.s3.us-west-2.amazonaws.com/profile/6_52644340.jpg
I have confirmed the files are uploading correctly and publicly available.
I have tried calling:
$image = Storage::cloud()->url($filePath);
And this returns:
http://test-env.XXXX.us-west-2.elasticbeanstalk.com/profile/https://elasticbeanstalk-us-west-2-XXX123XXX.s3.us-west-2.amazonaws.com//profile/6_31595766.jpg
Update
I just noticed the first part of the returned URL is the BeanStalk instance URL with the /profile/ added. This is even stranger as I don't wish to use Beanstalk, I only want to use S3.
If you want to store the entire url you can get it from the return variable passed back from the put() function.
$s3_url = $s3->put($filePath, file_get_contents($image), 'public');
Sometimes I like to just store the path though and save just that piece to the database then I can pass the path to Storage::url($filePath); and it still works.
$image = $request->image;
if (!empty($image)) {
$imageFileName = $user_id.'_'.rand(11111111, 99999999) . '.' . $image->getClientOriginalExtension(); // Rename Image
try
{
//Send to S3
$s3 = Storage::disk('s3');
$filePath = '/profile/' . $imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$image = Storage::cloud()->url('s3-url/s3-bucket'.'/'.$filepath);
}
catch(\Exception $exx)
{
//Send to Logs Etc
}

how to upload image from localhost to server path in laravel?

I am getting error 'Can't write image data to path', and i have folder permission too.
$image = $request->file('image');
$name = time()."_". $image->getClientOriginalName();
$img = Image::make($image->getRealPath());
$img->save('http://test.server.com/upload/' . $name);
first Install intervention image to your project
composer require intervention/image
after that
then inside your controller
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
then inside your method to store file
$file = Input::file('image');
$fileName = $file->hashName();
Image::make( $file->getRealPath() )->fit(70, 70)->save(public_path('path/to/dir/').$fileName)->destroy();
$object->imageName = $fileName;
this link was taken as reference

Resources