PHP S3 upload progress

Issue

This has gone around a number of times but I’m still a bit bewildered. A lot of answers only copncern themselves with talking about upload progress bars and not getting the actual upload progress from an S3 upload.

I have read a number of questions and found a number of software pieces but I am still no closer to understanding the fundamental question of S3 uploads.

Is there a way of uploading to S3 while understanding the progress of that upload without having to use my own app servers resources to store the file in the temp directory first?

Do I have to store the file on my own server first and then push to S3? I have heard people talk about streaming bit at a time to S3 (one chunk at a time) but I am unsure what this involves. Imagine I was uploading from client side in a HTML page how would I stream the file chunk by chunk from a multi-part from as it comes in? (I don’t see any example of that only an example of when the file is already on your system and you know the chunks which is kinda useless tbh).

There is of course an example of upload progress in the API docs but again that is assuming the file is on your server first and is not coming from another computer supplied by the user.

EDIT: My original thought was to make a PHP script that could ping AWS once every so oftern getting upload progress. I am looking through the API to see if they do support something like that but atm no luck. Let me know if there is something…

Further EDIT: Is flash the only way to go with this?

Thanks,

Solution

YES, it is possible in AWS PHP SDK v3.

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

$result = $client->putObject([
    'Bucket'     => 'bucket-name',
    'Key'        => 'bucket-name/file.ext',
    'SourceFile' => 'local-file.ext',
    'ContentType' => 'application/pdf',
    'params' => [
            '@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.

Answered By – The Onin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published