Level 5
bump it, any one?
Can anyone help me to refactor my code to match the second code? The problem I have is I changed the method to use the AWS S3multipart upload and am not able to put the file in the subfolder of the bucket. All files are uploaded at the root level and I can't create subfolders because of how the AWS multipart code is structured.
What I tried is appending "\subfolder" to the 'bucket' => $_ENV['AWS_BUCKET'], but definitely, it did not work.
if ($request->hasFile('files_doctor')) {
$patientFirstName = $request->patient_firstname;
$patientLastName = $request->patient_lastname;
// Users name adding
$caseDir = auth()->user()->drname;
$caseDir = str_replace(' ', '', $caseDir);
$path = public_path('storage/uploads/chromedoctorefiles/'. $caseDir);
foreach ($request->file('files_doctor') as $s3file) {
$directory = 'Case/'. $caseDir;
$contents = fopen($s3file, 'rb');
$s3patientFirstName = $request->patient_firstname;
$s3patientLastName = $request->patient_lastname;
$s3SavedOrigName = $s3file->getClientOriginalName();
$SendFileToS3 = $s3patientFirstName . '_' . $s3patientLastName . '_' . time() . $s3SavedOrigName;
// $disk = Storage::disk('s3');
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-west-1'
]);
$uploader = new MultipartUploader($s3, $contents, [
'bucket' => $_ENV['AWS_BUCKET'],
'key' => $SendFileToS3,
]);
try {
$result = $uploader->upload();
} catch (MultipartUploadException $e) {
return $e->getMessage();
}
}
```
Second Code
if ($request->hasFile('files_doctor')) {
$patientFirstName = $request->patient_firstname;
$patientLastName = $request->patient_lastname;
// Users name adding
$caseDir = auth()->user()->drname;
$caseDir = str_replace(' ', '', $caseDir);
$path = public_path('storage/uploads/chromedoctorefiles/'. $caseDir);
// Amazon checking folder
$directory = 'Case/'. $caseDir;
foreach ($request->file('fileslab') as $s3file) {
// Getting request names & extension
$s3patientFirstName = $request->patient_firstname;
$s3patientLastName = $request->patient_lastname;
$s3SavedOrigName = $s3file->getClientOriginalName();
$SendFileToS3 = $s3patientFirstName . '_' . $s3patientLastName . '_' . time() . $s3SavedOrigName;
$contents = file_get_contents($dbfile->getRealPath());
$path = Storage::disk('s3')->put($directory. '/' .$SendFileToS3, $contents);
if (!Storage::disk('s3')->exists($directory)){
Storage::disk('s3')->makeDirectory($directory);
$path = Storage::disk('s3')->put( $directory. '/' . $SendFileToS3, $contents );
}else{
$path = Storage::disk('s3')->put( $directory. '/' .$SendFileToS3, $contents);
}
}
Please or to participate in this conversation.