Gcs.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. if ( ! defined('BASE_PATH')) exit('No direct script access allowed');
  3. class StorageHandle{
  4. public $instance;
  5. public $headurl;
  6. public function __construct(){
  7. //$this->domain = DOMAIN;
  8. require dirname(__FILE__).'/gcs/GrandCloudStorage.php';
  9. $this->instance = new GrandCloudStorage('http://storage.grandcloud.cn');
  10. $this->instance->set_key_secret(CS_AK, CS_SK);
  11. $this->instance->set_bucket(DOMAIN);
  12. $this->headurl = 'http://storage-'.
  13. $this->instance->head_bucket(DOMAIN).
  14. '.sdcloud.cn';
  15. $this->instance->set_host($this->headurl);
  16. }
  17. public function exists($filename){
  18. //GCS木有正常的检测文件是否存在的API囧,那就获取信息吧,失败就不存在
  19. try{
  20. $this->instance->head_object($this->get_file($filename));
  21. return true;
  22. }catch(Exception $e){
  23. return false;
  24. }
  25. }
  26. public function read($filename){
  27. //GCS你是要闹哪样啊摔
  28. $temp = tmpfile();
  29. $this->instance->get_object($this->get_file($filename), $temp);
  30. fseek($temp,0);
  31. $contents = "";
  32. while (!feof($temp)){
  33. $contents .= fread($temp,8192);
  34. }
  35. return $contents;
  36. }
  37. public function write($name,$content){
  38. //同上啊摔
  39. $temp = tmpfile();
  40. fwrite($temp,$content);
  41. fseek($temp,0);
  42. //$temp = tempnam(sys_get_temp_dir());
  43. //file_put_contents($temp,$content);
  44. $this->instance->put_object($this->get_file($name), $temp);
  45. //unlink($temp);
  46. }
  47. public function url($name){
  48. return $this->headurl.$this->instance->get_object_resource($this->get_file($name),30*24*60*60);
  49. }
  50. public function error(){
  51. return false;
  52. }
  53. public function delete($name){
  54. return $this->instance->delete_object($this->get_file($name));
  55. }
  56. private function get_file($name){
  57. return ltrim($name,'/');
  58. }
  59. }