php - PHP 的 Heroku 内存错误和从 S3 读取大文件

标签 php memory heroku amazon-web-services amazon-s3

我正在使用适用于 PHP 的 AWS 2.3.2 SDK 尝试使用他们的流包装器从 S3 中提取一个大文件 (~4g),这应该允许我使用 fopen/fwrite 将文件写入磁盘并不缓冲到内存中。

引用如下:

http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/service-s3.html#downloading-data

这是我的代码:

public function download()
    {

        $client = S3Client::factory(array(
                    'key'    => getenv('S3_KEY'),
                    'secret' => getenv('S3_SECRET')
                    ));

        $bucket = getenv('S3_BUCKET');
        $client->registerStreamWrapper();

        try {
            error_log("calling download");
            // Open a stream in read-only mode
            if ($stream = fopen('s3://'.$bucket.'/tmp/'.$this->getOwner()->filename, 'r')) {
                // While the stream is still open
                if (($fp = @fopen($this->getOwner()->path . '/' . $this->getOwner()->filename, 'w')) !== false){

                    while (!feof($stream)) {
                        // Read 1024 bytes from the stream
                        fwrite($fp, fread($stream, 1024));
                    }
                    fclose($fp);
                }
            // Be sure to close the stream resource when you're done with it
            fclose($stream);
        }

文件下载但我不断收到来自 Heroku 的错误消息:

2013-08-22T19:57:59.537740+00:00 heroku[run.9336]: Process running mem=515M(100.6%) 2013-08-22T19:57:59.537972+00:00 heroku[run.9336]: Error R14 (Memory quota exceeded)

这让我相信这仍然以某种方式缓冲到内存中。我试过使用 https://github.com/arnaud-lb/php-memory-profiler ,但出现段错误。

我还尝试使用带有 CURLOPT_FILE 选项的 cURL 下载文件以直接写入磁盘,但内存仍然不足。奇怪的是,根据 top,我的 php 实例使用了 223m 的内存,所以连允许的 512 的一半都不到。

有人有什么想法吗?我正在从 php 5.4.17 cli 运行它进行测试。

最佳答案

您是否已经尝试过 2x dyno,它们有 1GB 内存?

您还可以尝试通过在 PHP 中执行 curl 命令来下载文件。这不是最干净的方法,但它会更快/更可靠并且内存友好。

exec("curl -O http://test.s3.amazonaws.com/file.zip", $output);

此示例适用于公共(public) URL。如果您不想公开您的 S3 文件,您始终可以创建一个签名 URL 并将其与 curl 命令结合使用。

关于php - PHP 的 Heroku 内存错误和从 S3 读取大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18390208/

相关文章:

php - AngularJS ng-repeat 不显示表中的数据

java - 如何在 Java 中解析 PHP 序列化数据结构?

c - 调整数组大小时,Realloc 给出无效指针错误

ruby-on-rails - Heroku & Delayed Job & Workless - RestClient::ResourceNotFound

Heroku 删除 Gemfile.lock,因为它是在 Windows 上生成的

php - 如何查询特定的名称服务器( native php)

php - 添加到收藏夹的代码无提示地失败

python - 在 Ubuntu12.04 中使用 Python3 的 lxml 内存泄漏

ios - 在 UIImageView 中加载 gif 图像时减少内存消耗

ruby-on-rails - 名称错误 : uninitialized constant Dotenv while pushing rails app to heroku