简体   繁体   English

如何使用 PHP 和进度条将文件直接上传到 S3

[英]How to upload files directly to S3 using PHP and with progress bar

There are some similar questions but none have a good answers in how to upload files directly to S3 using PHP with progress bar.有一些类似的问题,但对于如何使用带有进度条的 PHP 将文件直接上传到 S3,没有一个很好的答案。 Is it even possible adding a progress bar without using Flash?甚至可以在不使用 Flash 的情况下添加进度条吗?

NOTE: I am referring to uploading from client browser directly to S3.注意:我指的是从客户端浏览器直接上传到 S3。

I've done this in our project.我已经在我们的项目中做到了这一点。 You can't upload directly to S3 using AJAX because of standard cross domain security policies;由于标准的跨域安全策略,您不能使用 AJAX 直接上传到 S3; instead, you need to use either a regular form POST or Flash.相反,您需要使用常规形式的 POST 或 Flash。 You'll need to send the security policy and signature in a relatively complex process, as explained in the S3 docs .您需要在一个相对复杂的过程中发送安全策略和签名,如S3 文档中所述。

YES , it is possible to do this in PHP SDK v3.的,可以在 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 .这在 AWS docs- S3 配置部分进行了解释。 It works by exposing GuzzleHttp's progress property-callable, as explained in this SO answer .它通过公开 GuzzleHttp 的progress属性可调用来工作,如this SO answer中所述。

Technically speaking, with PHP you cannot go from client --> S3.从技术上讲,使用 PHP 您不能从客户端获得 go --> S3。 Your solution, if you want to use PHP would either have to be designed as follows:你的解决方案,如果你想使用PHP要么必须设计如下:

  • Client -> Web Server (PHP) -> Amazon S3客户端 -> Web 服务器 (PHP) -> Amazon S3
  • Client with PHP server embedded -> Amazon S3嵌入了 PHP 服务器的客户端 -> 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 AWS PHP SDK: http://aws.amazon.com/sdkforphp/写得很好,包含一个来自客户端文件的具体示例 --> 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:快速搜索 stackoverflow.com 显示了一个与此问题相同的问题:

It is possible to directly upload, but progress bar is impossible: http://undesigned.org.za/2007/10/22/amazon-s3-php-class/可以直接上传,但是进度条不行: http://undesign.org.za/2007/10/22/amazon-s3-php-class/

see example_form in the downloads, direct upload from browser to S3参见下载中的 example_form,直接从浏览器上传到 S3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM