BaiduBCS::get_object PHP Method

get_object() public method

下载object
public get_object ( string $bucket, string $object, array $opt = [] ) : BCS_ResponseCore
$bucket string (Required)
$object string (Required)
$opt array (Optional) fileWriteTo (Optional)直接将请求结果写入该文件,如果fileWriteTo文件存在,sdk进行重命名再存储
return BCS_ResponseCore
    public function get_object($bucket, $object, $opt = array())
    {
        $this->assertParameterArray($opt);
        //若fileWriteTo待写入的文件已经存在,需要进行重命名
        if (isset($opt["fileWriteTo"]) && file_exists($opt["fileWriteTo"])) {
            $original_file_write_to = $opt["fileWriteTo"];
            $arr = explode(DIRECTORY_SEPARATOR, $opt["fileWriteTo"]);
            $file_name = $arr[count($arr) - 1];
            $num = 1;
            while (file_exists($opt["fileWriteTo"])) {
                $new_name_arr = explode(".", $file_name);
                if (count($new_name_arr) > 1) {
                    $new_name_arr[count($new_name_arr) - 2] .= " ({$num})";
                } else {
                    $new_name_arr[0] .= " ({$num})";
                }
                $arr[count($arr) - 1] = implode(".", $new_name_arr);
                $opt["fileWriteTo"] = implode(DIRECTORY_SEPARATOR, $arr);
                $num++;
            }
            $this->log("[{$original_file_write_to}] already exist, rename it to [" . $opt["fileWriteTo"] . "]", $opt);
        }
        $opt[self::BUCKET] = $bucket;
        $opt[self::METHOD] = 'GET';
        $opt[self::OBJECT] = $object;
        $response = $this->authenticate($opt);
        $this->log($response->isOK() ? "Get object success!" : "Get object failed! Response: [" . $response->body . "]", $opt);
        if (!$response->isOK() && isset($opt["fileWriteTo"])) {
            unlink($opt["fileWriteTo"]);
        }
        return $response;
    }

Usage Example

Esempio n. 1
0
 public function getAction($objName)
 {
     $conf = $this->getDI()->get('config');
     $bcs = new \BaiduBCS($conf->bcs->ak, $conf->bcs->sk, $conf->bcs->host);
     $objName = "/3EBF946E-A756-099E-4B2E-89A0153D19AF_300.jpg";
     $response = $bcs->get_object($conf->bcs->bucket, $objName);
     if (!$response->isOK()) {
         $this->flashJson("抱兼,文件获取失败,请重试!");
     } else {
         header("Content-type: image/jpeg");
         echo $response->body;
     }
     exit;
 }
All Usage Examples Of BaiduBCS::get_object