我想使用
PHP SDK将外部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函数中设置’length’属性,我会收到一条错误消息,指出’无法确定流上传的流大小’.任何想法如何解决这个问题?