laravelamazon-web-servicesamazon-s3

Laravel file upload s3 Multipart


how do I integrate the multipart upload of s3? I am uploading to s3 and everything works. just I want to refactor the code to S3 multipart upload because the files are too large on the server

//    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);
}

The Amazon SK Example is below:

 require 'vendor/autoload.php';

use Aws\Common\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\S3Client;

$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';
                        
$s3 = new S3Client([
    'version' => 'latest',
    'region'  => 'us-east-1'
]);
 
// Prepare the upload parameters.
$uploader = new MultipartUploader($s3, '/path/to/large/file.zip', [
    'bucket' => $bucket,
    'key'    => $keyname
]);

// Perform the upload.
try {
    $result = $uploader->upload();
    echo "Upload complete: {$result['ObjectURL']}" . PHP_EOL;
} catch (MultipartUploadException $e) {
    echo $e->getMessage() . PHP_EOL;
}

Solution

  • I fixed it.

    foreach ($request->file('fileslab') 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();
      }
    }