由于无法将PHP更新到5.5版本,我被迫使用AWS S3的第二个版本,而不是第三个版本。
我编写了这个PHP脚本来从AWS下载文件,它运行良好:
//http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_createPresignedUrl
// Get a command object from the client and pass in any options
// available in the GetObject command (e.g. ResponseContentDisposition)
$command = $s3Client->getCommand('GetObject', array(
'Bucket' => $bucket,
'Key' => $objectKey,
'ResponseContentDisposition' => 'attachment; filename="' . $originFilename . '"'
));
// Create a signed URL from the command object that will last for
// 10 minutes from the current time
$signedUrl = $command->createPresignedUrl('+1000 minutes');
$file = file_get_contents($signedUrl);
问题是我想确保
file_get_contents()
下载整个文件,并检测和修复任何错误(例如在下载期间服务器离线等),因此我考虑以下流程:
- 向AWS请求文件大小
- 下载文件
- 检查文件大小。如果不相等,则重新下载文件