Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 PHP Progress Bar (Server to Cloud)

I have an image upload system in my application written in PHP. The file browser opens, user picks an image, I upload it to my server, I crop, I resize, I apply a watermark to it. Bottom line is the images are in my server. At some point, the user clicks a button and then I move those files to my S3 bucket. Naturally, I need a progress bar because, ze client wants a progress bar.

Now uploading the files is quite easy:

    $result = $this->awsS3Client->putObject(array(
        'Bucket' => 'bad-dum-tss-bucket',
        'Key'    => $destinationFilePath,
        'SourceFile'  => $sourceFilePath,
        'ContentType' => $mimeType,
        'ACL'    => 'public-read',
    ));

I can even go multi-part

    $uploader = UploadBuilder::newInstance()
        ->setClient($this->awsS3Client)
        ->setSource($sourceFilePath)
        ->setBucket( 'bad-dum-tss-bucket')
        ->setKey($destinationFilePath)
        ->build();

    try {
        $uploader->upload();
    } catch (MultipartUploadException $e) {
        $uploader->abort();
    }

No problem there until I realize my client needs a freaking progress bar. Now I've searched a lot and all I can see are links to uploaders such as http://fineuploader.com/ that assumes that the upload will happen directly from the browser (i.e. not from my server). So PHP-progress bar-S3, anybody?

like image 515
turntwo Avatar asked Nov 01 '25 15:11

turntwo


1 Answers

If you're still interested, I found a way to track progress in PHP with AWS SDK v3.

$client = new S3Client(/* config */);

$result = $client->putObject([
    'Bucket'     => 'bucket-name',
    'Key'        => 'bucket-name/file.ext',
    'SourceFile' => 'local-file.ext',
    'ContentType' => 'application/pdf',
    '@http' => [
        'progress' => function ($downloadTotalSize, $downloadSizeSoFar, $uploadTotalSize, $uploadSizeSoFar) {
            printf(
                "%s of %s downloaded, %s of %s uploaded.\n",
                $downloadSizeSoFar,
                $downloadTotalSize,
                $uploadSizeSoFar,
                $uploadTotalSize
            );
        }
    ]
]);

This is explained in the AWS docs - S3 Config section. It works by exposing GuzzleHttp's progress property-callable, as explained in this SO answer.

like image 86
The Onin Avatar answered Nov 04 '25 07:11

The Onin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!