我想使用 PHP 开发工具包将文件从外部 URL 直接上传到 Amazon S3 存储桶。我设法用以下代码做到了这一点:
$s3 = new AmazonS3();
$response = $s3->create_object($bucket, $destination, array(
'fileUpload' => $source,
'length' => remote_filesize($source),
'contentType' => 'image/jpeg'
));
其中函数 remote_filesize 如下:
function remote_filesize($url) {
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$ok = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
return isset($matches[1]) ? $matches[1] : "unknown";
}
但是,如果我可以在上传到亚马逊时跳过设置文件大小,那就太好了,因为这样可以节省我去我自己的服务器的时间。但是,如果我删除 $s3->create_object 函数中的“长度”属性设置,我会收到一条错误消息,指出“无法确定流式上传的流大小”。任何想法如何解决这个问题?