AWS S3 PHP Progress Bar (Cloud Computing Server)

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

Uploading files is pretty simple:

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

      

I can even go with multiple pieces

    $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 until I realize that my client needs a crazy progress bar. I've searched a lot now and all I see are links to downloaders like http://fineuploader.com/ assuming the download will happen directly from the browser (i.e. not from my server). So PHP-progress bar-S3, anyone?

+3


source to share


2 answers


I got this to work by enabling concurrent XHRs on the server to poll the boot process and store it in a session variable. See: Why are my XHR calls waiting for each other to return a response where I asked another question related to XHR polling and session locking to accomplish this.



In the end though, I decided to give it all up completely. My production server was an EC2 instance, so any upload to the S3 server took only negligible network overhead (I should have done this before). I could transfer a few MB of images (whatever I would ever need) in less than 3 seconds, so I decided to just not display the progress bar as that doesn't justify the cost of adding nasty session calls in different parts of my code.

0


source


If you're still curious, I found a way to track progress in PHP using the 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 to a callable progress

property as described in this SO answer .

+2


source







All Articles