我有一个看起来像这样的函数:
public function downloadProjectFolder($projectId, $taskToken){
// Download the project directory if it isn't on the server
if(is_dir('/path/to/folder/') === false){
$manager = $this->instantiateS3TransferObject($projectId, $taskToken);
$promise = $manager->promise();
$promise->wait();
}
else{
return 'Project Folder Already Exists';
}
}
如果本地计算机上尚不存在文件夹,则上述方法会从 AWS S3 将文件夹下载到我的服务器上。实际的 S3 Transfer 对象(来自 AWS PHP SDK V3 库 - 它本身主要从 Guzzle PHP 抽象而来)由以下函数实例化:
private function instantiateS3TransferObject($projectId, $taskToken){
$lastDatetime = time();
return new \Aws\S3\Transfer($this->myS3ClientObject, 's3://mys3bucket/destination/url',
'/path/to/local/directory/', array(
'base_dir' => 'destination/url',
'before' => function()use($projectId, $taskToken, &$lastDatetime){
$currentDatetime = time();
if(($currentDatetime - $lastDatetime) >= 30){
$postParams = array(
'project_id' => $projectId,
'task_token' => $taskToken
);
$this->curl->post($postParams, 'http://url/endpoint');
$lastDatetime = $currentDatetime;
}
}
)
);
}
上面基本上开始我的文件夹下载并每 30 秒异步点击一个自定义端点。
在这种情况下,我将如何模拟\Aws\S3\Transfer
对象,以便它包含返回的promise()
方法,而该方法又返回该wait()
方法?