函数是否会暂停 php 脚本,直到它在 s3 服务器上找到对象?
我把它放在一个 foreach 循环中,一张一张地上传图片。找到对象后,我调用一种方法在本地删除图像,然后删除本地文件夹(如果为空)。这是解决问题的正确方法吗?谢谢
foreach ($fileNames as $fileName)
{
$imgSize = getimagesize($folderPath . $fileName);
$width = (string)$imgSize[0];
$height = (string)$imgSize[1];
//upload the images
$result = $S3->putObject(array(
'ACL' => 'public-read',
'Bucket' => $bucket,
'Key' => $keyPrefix . $fileName,
'SourceFile' => $folderPath . $fileName,
'Metadata' => array(
'w' => $width,
'h' => $height
)
));
$S3->waitUntilObjectExists(array(
'Bucket' => $bucket,
'Key' => $keyPrefix . $fileName));
$this->deleteStoreDirectory($folderPath, $fileName);
}
最佳答案
waitUntilObjectExists
基本上是一个服务员,它会在特定时间间隔定期检查(轮询)S3 以查看资源是否可用。脚本的执行被阻止,直到找到资源或达到最大重试次数。
正如 AWS 文档对它们的定义:
Waiters help make it easier to work with eventually consistent systems by providing an easy way to wait until a resource enters into a particular state by polling the resource.
默认情况下,waitUntilObjectExists
服务员被配置为尝试定位资源 20 次,每次尝试之间有 5 秒的延迟。您可以通过将附加参数传递给 waitUntilObjectExists
方法,用您想要的值覆盖这些默认值。
如果服务端在最大尝试次数后仍无法定位资源,则会抛出异常。
您可以在以下位置了解有关服务员的更多信息:
http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/feature-waiters.html
对于您的用例,我认为在上传对象后调用 waitUntilObjectExists
没有意义,除非相同的 PHP 脚本稍后在代码中尝试从 S3 检索相同的对象。
如果 putObject
API 调用返回了成功的响应,那么该对象最终将出现在 S3 中,您不必等到这发生再删除本地文件。
关于php - waitUntilObjectExists() Amazon S3 PHP SDK 方法,它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18049717/