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);
Related
My all files path is /storage/app/public/applicants/pdf/[all-files-here]. So, how can I download it from the folder.
I have done this:
$file = Storage::disk('public')->get('/applicants/' . 'selection_test_result_1669368177.pdf');
return response()->download($file);
return Storage::download('file.pdf', $name, $headers);
I want to store an uploaded file with its original client name in the storage folder. What do I need to add or change in my code?Any help or recommendation will be greatly appreciated
Here my Controller
public function store(Request $request) {
$path = "dev/table/".$input['id']."";
$originalName = $request->file->getClientOriginalName();
$file = $request->file;
Storage::disk('local')->put($path . '/' . $originalName, $request->file);
}
Edit: I know how to get the originalClientName. the problem is storing the file in the folder using the original name, not the hash name. It doesn't store in the file in the original it makes a new folder instead here is the output "dev/table/101/Capture1.PNG/xtZ9iFoJMoLrLaPDDPvc4DMJEXkRL3R4qWOionMC.png" what I trying to get is "dev/table/101/Capture1.PNG"
I have tried to use StoreAs Or putFileAs but the method is undefined
I managed to figure out how to store it with a custom name, for those who want to know how to do it here is the code
$id = $input['id'];
$originalName = $request->file->getClientOriginalName();
$path = "dev/table/$id/".$originalName;
Storage::disk('local')->put($path, file_get_contents($request->file));
public function store(Request $request) {
$originalName = $request->file->getClientOriginalName();
$extension = $request->file->getClientOriginalExtension();
$path = "dev/table/" . $input['id'] . "/" . $originalName . "." . $extension;
$file = $request->file;
Storage::disk('local')->put($path, $file);
}
To get the original file name you can use this in your ControllerClass:
$file = $request->file->getClientOriginalName();
To get additional the extension you can use this Laravel Request Method:
$ext = $request->file->getClientOriginalExtension();
Then you can save with:
$fileName = $file.'.'.$ext;
$request->file->storeAs($path, $fileName);
// or
Storage::disk('local')->put($path . '/' . $fileName , $request->file);
You can save files using storage with the default name using putFileAs function instead of put which allow take third param as a file name
$path = "dev/table/101/";
$originalName = request()->file->getClientOriginalName();
$image = request()->file;
Storage::disk('local')->putFileAs($path, $image, $originalName);
Update
You can do something like this with put,
Storage::disk('local')->put($path.$originalName, file_get_contents($image));
I tried to manage like this;
$insurance = $request->file('insurance_papers');
$insuranceExtention = $insurance->getClientOriginalExtension();
$path = "public/files/" . $carrier->id . "/insurance_papers." . $insuranceExtention;
Storage::disk('local')->put($path, file_get_contents($insurance));
You can try this, this is work for me
if ($request->hasFile('attachment')) {
$image = $request->file('attachment');
$imageName = time() . '.' . $image->getClientOriginalExtension();
$path = "foldername/".$imageName;
Storage::disk('public')->put($path, file_get_contents($image));
}
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
}
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
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));