Aug 7, 2023
0
Level 1
Unable to change the default s3 driver bucket name with a new bucket in laravel
I am trying to update a profile and also trying to upload the image to a specific aws bucket. In my function, the s3 is considering the default bucket, I want to store it into a different bucket which I have created.
public function updateCustomerProfileDetails(Request $request)
{
$returnArray = array();
// Validate the request data
$request->validate([
'first_name' => 'alpha:ascii',
'last_name' => 'alpha:ascii',
'profile_image' => 'file|image|mimes:jpeg,png,jpg,gif',
'contact_number' => ['regex:/^(\+\d{1,2}\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/']
]);
try {
$logedUser = auth()->user();
$user = self::loadParticuler('id', $logedUser->id);
if ($user == null) {
return $this->returnErrorResponse(self::COMPONENT_CODE, 114, 'User does not exist', 422);
}
$userDetails = $user->details;
if ($userDetails == null) {
$userDetails = new UserDetails();
$userDetails->user_id = $user->id;
}
$root_user = RootUser::where('email', auth()->user()->email)->with('details')->first();
$isUpdated = false;
if (!empty($request->first_name)) {
$userDetails->first_name = $request->first_name;
$root_user->details->first_name = $request->first_name;
$isUpdated = true;
}
if (!empty($request->last_name)) {
$userDetails->last_name = $request->last_name;
$root_user->details->last_name = $request->last_name;
$isUpdated = true;
}
if (!empty($request->contact_number)) {
$userDetails->mobile = $request->contact_number;
$root_user->details->contact_number = $request->contact_number;
$isUpdated = true;
}
if (!empty($request->profile_image)) {
$env = config('app.env');
$disk = ($env == 'production') ? 's3' : 'local';
$url = Storage::disk($disk)->put('/'.auth()->user()->email.'/', $request->profile_image);
$userDetails->profile_image = $url;
$root_user->details->profile_image = $url;
$isUpdated = true;
}
if ($isUpdated) {
$userDetails->save();
$root_user->details->save();
}
$msg = $isUpdated ? "Data Updated Successfully" : "There are no changes to update";
$returnArray['status'] = "Success";
$returnArray['message'] = $msg;
return response()->json($returnArray, 200);
} catch (\Illuminate\Validation\ValidationException $e) {
return $this->returnErrorResponse(self::COMPONENT_CODE, 115, array_column($e->errors(), 0), 422);
} catch (\Exception $e) {
return $this->returnErrorResponse(self::COMPONENT_CODE, 116, $e->getMessage(), 422);
}
}
KIndly suggest how can I change the default bucket to my specified bucket and within t a folder will be created with the email id of the user logged in in which the profile image will be saved.
Please or to participate in this conversation.