1

There are some similar questions but none have a good answers in how to upload files directly to S3 using PHP with progress bar. Is it even possible adding a progress bar without using Flash?

NOTE: I am referring to uploading from client browser directly to S3.

Amy
  • 11
  • 1
  • 1
  • 2
  • willing to use jQuery and ajax? http://stackoverflow.com/questions/2714507/jquery-ajax-upload-with-progress-bar-no-flash – tradyblix Jul 01 '11 at 03:41
  • yes, I can use jQuery and Ajax – Amy Jul 01 '11 at 03:42
  • 1
    Correct me if I'm wrong, but in order to upload files directly from the client, you need to give the client upload access, even just temporarily. Is that an acceptable security tradeoff for you? – Nick ODell Jul 01 '11 at 03:54

4 Answers4

2

YES, it is possible to do this in 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',
    '@http' => [
        'progress' => function ($downloadTotalSize, $downloadSizeSoFar, $uploadTotalSize, $uploadSizeSoFar) {
            // handle your progress bar percentage
            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.

Community
  • 1
  • 1
The Onin
  • 5,068
  • 2
  • 38
  • 55
2

I've done this in our project. You can't upload directly to S3 using AJAX because of standard cross domain security policies; instead, you need to use either a regular form POST or Flash. You'll need to send the security policy and signature in a relatively complex process, as explained in the S3 docs.

nitwit
  • 1,745
  • 2
  • 17
  • 20
Khaled
  • 690
  • 2
  • 10
  • 19
0

Technically speaking, with PHP you cannot go from client --> S3. Your solution, if you want to use PHP would either have to be designed as follows:

  • Client -> Web Server (PHP) -> Amazon S3
  • Client with PHP server embedded -> Amazon S3

The AWS PHP SDK: http://aws.amazon.com/sdkforphp/ is very well written and contains a specific example on how to send a file from a Client --> Server --> S3

With respects to the progress bar, there are many options available. A quick search of stackoverflow.com shows a question answered identical to this one:

Community
  • 1
  • 1
sdolgy
  • 6,963
  • 3
  • 41
  • 61
  • 4
    To post directly to s3, you need to sign the form. Signing the form should be done server side so you don't reveal your credentials. So the beginning of this answer is incorrect. You can, and should use PHP to post from client to S3. – trex005 Aug 20 '12 at 09:26
-1

It is possible to directly upload, but progress bar is impossible: http://undesigned.org.za/2007/10/22/amazon-s3-php-class/

see example_form in the downloads, direct upload from browser to S3

  • 4
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Mihai Iorga Sep 28 '12 at 08:59
  • You can actually use HTML 5 for the upload progress bar section so ... Impossible should never be used (although it was 2012 when you wrote that) :-) – Chris Pierce Apr 27 '17 at 19:51