6

AWS 2.3.2 SDK for PHP を使用して、ストリーム ラッパーを使用して S3 から大きなファイル (~4g) をプルダウンしようとしています。メモリー。

参照は次のとおりです。

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]: プロセス実行 mem=515M(100.6%) 2013-08-22T19:57:59.537972+00:00 heroku[run.9336]: エラーR14 (メモリ割り当て超過)

これは、これがまだ何らかの形でメモリにバッファリングされていると私に信じさせます。https://github.com/arnaud-lb/php-memory-profilerを使用しようとしましたが、Seg Fault が発生しました。

また、CURLOPT_FILEオプションを指定してcURLを使用してファイルをダウンロードして、ディスクに直接書き込もうとしましたが、まだメモリが不足しています。奇妙なことは、top私のphpインスタンスによると、223mのメモリを使用しているため、許可されている512の半分でもない.

誰にもアイデアはありますか?これをphp 5.4.17 cliから実行してテストしています。

4

1 に答える 1

2

メモリが 1GB の 2x dyno を既に試しましたか?

また、PHP で curl コマンドを実行してファイルをダウンロードすることもできます。これは最もクリーンな方法ではありませんが、はるかに高速で信頼性が高く、メモリに優しい方法です。

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

この例は、パブリック URL 用です。S3 ファイルを公開したくない場合は、いつでも署名付き URL を作成し、それを curl コマンドと組み合わせて使用​​できます。

于 2013-08-22T23:59:50.880 に答える