duancan9815 2013-12-05 06:57
浏览 71

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

Trying to write my own class since I only need "putobject" functionality. I can't get this to work to save my life.

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."
".$this->__headers['Content-MD5']."
".$this->__headers['Content-Type']."
".$this->__headers['Date']."
".$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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试
    • ¥15 教务系统账号被盗号如何追溯设备
    • ¥20 delta降尺度方法,未来数据怎么降尺度
    • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
    • ¥15 再不同版本的系统上,TCP传输速度不一致
    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿