create_bucket就不说了,应用场景不多,而且挺简单。说说create_object吧,这是一个上传文件的函数。我个人理解object指的就是云文件,bae的文档详细但是很生硬:
$file其实就是你的代码包里的某个文件,我尝试给它一个远程地址,但是整个代码在这个函数处终止了。文档下面给出了一段示范代码:
<?php require_once './bcs.class.php'; $ak = "accesskey"; // ak&sk 请在管理网站上获取 $sk = "secretkey"; $host = 'bcs.duapp.com'; $baidu_bcs = new BaiduBCS ( $ak, $sk, $host ); $bucket = "sample"; $object = "/create_object.php"; $file = "./create_object.php"; $response = $baidu_bcs->create_object ( $bucket, $object, $file ); echo $response->isOK () ? "OK\n" : "NOT OK\n"; echo 'Status:' . $response->status . "\n"; echo 'Body:' . $response->body . "\n"; foreach ( $response->header as $key => $value ) { echo "Http Header -> $key:$value\n"; } ?>
但是文档忽略了很重要的一点,也是我们这些初学者很容易忽略的一点。通过网页手工创建的bucket默认是私有的,所谓私有,就是没有AK和SK的任何应用都无法操作它,除非你用网页版的bucket上传下载。只有将其改为
才能读写操作,够坑爹的吧。
实例代码有些烦,我来给一段代码吧:
<?php echo "Welcome to Baidu Cloud<br>"; require_once './bcs.class.php'; $host = 'bcs.duapp.com'; //online $ak = 'HTTP_BAE_ENV_AK'; $sk = 'HTTP_BAE_ENV_SK'; $baidu_bcs = new BaiduBCS ( $ak, $sk, $host ); echo "BCS created<br>"; $bucket = '改成自己的!'; $object = "/index.php"; $file = "index.php"; $response = $baidu_bcs->create_object($bucket, $object, $file); echo $response->isOK () ? "OK<br>" : "NOT OK<br>"; echo 'Status:' . $response->status . "<br>"; echo 'Body:' . $response->body . "<br>"; foreach ( $response->header as $key => $value ) { echo "Http Header -> $key:$value<br>"; } ?>
bcs.class.php是SDK,路径根据实际情况定。运行后输出:
Welcome to Baidu Cloud BCS created OK Status:200 Body: Http Header -> Server:nginx/1.2.0 Http Header -> Date:Sat, 13 Jul 2013 14:59:24 GMT Http Header -> Content-Length:0 Http Header -> Connection:keep-alive Http Header -> Access-Control-Allow-Origin:* Http Header -> Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS,HEAD Http Header -> x-bs-version:6A36A88F14945B936A102DF613D480B6 Http Header -> ETag:d30db479501561ed84add593086ceacf Http Header -> Content-MD5:d30db479501561ed84add593086ceacf Http Header -> x-bs-request-id:MTAuMzYuMTAxLjE2OjgwODA6MTEyOTk5Mjg3MDoxMy9KdWwvMjAxMyAyMjo1OToyNCA= Http Header -> x-bs-file-size:620 Http Header -> x-bs-client-ip:MjIwLjE4MS41MS4yMDA= Http Header -> _info:Array
在网页版的管理界面出现了
这段代码的作用就是把自己传到云储存里面去。顺便上面代码有一处有问题你看出来了吗?