Amazon S3(简单存储服务)PHP REST

时间:2013-12-05 06:58:00

标签: php rest curl amazon-web-services amazon-s3

试着写我自己的课,因为我只需要" putobject"功能。我无法通过这项工作来挽救我的生命。

http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html

class s3{

    var $__ak = 'accessKey';
    var $__sk = 'shareKey';
    var $__bucket = 'bucket';
    var $__headers = false;
    var $__content = false;
    var $__verb = false;
    var $__data = false;

    function __getSignature(){
        $query = $this->__verb."\n".$this->__headers['Content-MD5']."\n".$this->__headers['Content-Type']."\n".$this->__headers['Date']."\n".$this->__data;
        return 'AWS '.$this->__ak.':'.base64_encode(hash_hmac('sha1',$query,$this->__sk,true));
    }

    function __returnHeaders(){
        foreach($this->__headers as $k=>$v) $temp[] = $k.': '.$v;
        return $temp?$temp:false;
    }   

    function __putObject($input){

        $this->__verb = 'PUT';
        $this->__data = $input;

        $this->__headers['Host'] = $this->__bucket.'.s3.amazonaws.com';
        $this->__headers['Date'] = gmdate('D, d M Y H:i:s O');
        $this->__headers['Authorization'] = $this->__getSignature();

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_USERAGENT, 'S3/php');
        curl_setopt($ch, CURLOPT_URL, $this->__headers['Host']);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->__returnHeaders());
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_POSTFIELDS, '@'.$input);

        $result = curl_exec($ch);
        $info = curl_getinfo($ch);

        curl_close($ch);
    }
}

$s = new s3;

$s->__putObject('/image.jpg');

0 个答案:

没有答案