Created
July 20, 2015 12:05
-
-
Save frankdejonge/54bd11e0e428f73a2f1c to your computer and use it in GitHub Desktop.
Concurrent upload to S3 buckets with Flysystem and pthreads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Aws\S3\S3Client; | |
use League\Flysystem\AwsS3v3\AwsS3Adapter; | |
use League\Flysystem\Filesystem; | |
function get_filesystem() { | |
$client = new S3Client([ | |
'credentials' => [ | |
'key' => getenv('AWS_KET'), | |
'secret' => getenv('AWS_SECRET'), | |
], | |
'region' => getenv('AWS_REGION'), | |
'version' => getenv('AWS_VERSION'), | |
]); | |
$adapter = new AwsS3Adapter($client, getenv('AWS_BUCKET_NAME')); | |
return new Filesystem($adapter); | |
} | |
class Upload extends Threaded | |
{ | |
private $from; | |
private $to; | |
public $success; | |
public $start; | |
public $end; | |
public function __construct($from, $to) | |
{ | |
$this->from = $from; | |
$this->to = $to; | |
} | |
public function run() | |
{ | |
// A thread needs the autoloader. | |
require_once __DIR__ . '/vendor/autoload.php'; | |
// Upload + profile | |
$this->start = new DateTime(); | |
$handle = fopen($this->from, 'r+'); | |
$filesystem = get_filesystem(); | |
$this->success = $filesystem->putStream($this->to, $handle); | |
$this->end = new DateTime(); | |
fclose($handle); | |
} | |
} | |
$pool = new Pool(8); | |
$uploadJobs = []; | |
foreach (range(1,5) as $num) { | |
$uploadJobs[$num] = new Upload(__DIR__.'/cat.gif', '/cats/'.$num.'.gif'); | |
$pool->submit($uploadJobs[$num]); | |
} | |
$pool->shutdown(); | |
foreach ($uploadJobs as $upload) { | |
var_dump($upload->success); | |
var_dump($upload->start); | |
var_dump($upload->end); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment