Ap3twe's avatar

Refactor s3 file upload to multipart upload

Here is my code, 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 example on Amazon sdk is

 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;
}
0 likes
9 replies
Ap3twe's avatar

I have read it just can't seem how to implement it. I have completed the lifecycle. if someone can provide sample code for me, I will appreciate

Ap3twe's avatar

This is what I tried, I get error unable to open PK with a bunch of binary codes and the page stuck in loop

 foreach ($request->file('fileslab') as $dbfile) {
                $contents = file_get_contents($dbfile->getRealPath());
                $disk = Storage::disk('s3');
                $uploader = new MultipartUploader($disk->getDriver()->getAdapter()->getClient(), $contents, [
                    'bucket' => $_ENV['AWS_BUCKET'],
                    'key'    => $_ENV['AWS_ACCESS_KEY_ID'],
                ]);
    
                try {
                    $result = $uploader->upload();
                    echo "Upload complete";
                } catch (MultipartUploadException $e) {
                    echo $e->getMessage();
                }
                
            }
            
Ap3twe's avatar

I tweak a lil bit and get error getDriver() not found

 foreach ($request->file('fileslab') as $dbfile) {
                $contents = file_get_contents($dbfile->getRealPath());
                $disk = Storage::disk('s3');
                $s3 = new S3Client([
                    'version' => 'latest',
                    'region'  => 'us-west-1'
                ]);
                $uploader = new MultipartUploader($s3->getDriver()->getAdapter()->getClient(), $contents, [
                    'bucket' => $_ENV['AWS_BUCKET'],
                    'key'    => $_ENV['AWS_ACCESS_KEY_ID'],
                ]);
    
                try {
                    $result = $uploader->upload();
                    echo "Upload complete";
                } catch (MultipartUploadException $e) {
                    echo $e->getMessage();
                }
                
            }
jlrdw's avatar

You might try working some of there examples, looks like there's a lot to the SDK.

Take note of the pricing and making sure you tell it upload is done.

Ap3twe's avatar
Ap3twe
OP
Best Answer
Level 5

Got it working.

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

Please or to participate in this conversation.