1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
<?php
namespace LeanCloud\Uploader;
use LeanCloud\Client;
class S3Uploader extends SimpleUploader {
public function upload($content, $mimeType, $name=null) {
if (!$this->getUploadUrl()) {
throw new \RuntimeException("Please initialize with pre-signed url.");
}
$headers[] = "User-Agent: " . Client::getVersionString();
$headers[] = "Content-Type: $mimeType";
$url = $this->getUploadUrl();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$resp = curl_exec($ch);
$respCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$respType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
if ($errno > 0) {
throw new \RuntimeException("CURL ({$url}) error: " .
"{$errno} {$error}",
$errno);
}
if ($respCode >= "300") {
$S3Error = simplexml_load_string($resp);
throw new \RuntimeException("Upload to S3 ({$url}) failed: " .
"{$S3Error->Code} {$S3Error->Message}");
}
return true;
}
}