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
Beispiel #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;
 }
function file_get($filename)
{
    if (IS_BAE) {
        $arr = explode('/', ltrim($filename, './'));
        $bucket = C('BUCKET_PREFIX') . strtolower(array_shift($arr));
        $file = implode('/', $arr);
        try {
            $bcs = new BaiduBCS();
            $tmp_name = sys_get_temp_dir() . '/' . uniqid();
            $response = $bcs->get_object($bucket, '/' . $file, array('fileWriteTo' => $tmp_name));
            if ($response->isOK()) {
                $content = file_get_contents($tmp_name);
                unlink($tmp_name);
                return $content;
            }
            return false;
        } catch (Exception $e) {
            return false;
        }
    } else {
        return file_get_contents($filename);
    }
}
$baiduBCS = new BaiduBCS($ak, $sk, $host);
//step1. create a bucket
$response = $baiduBCS->create_bucket($bucket);
if ($response->isOK()) {
    echo "Create bucket[{$bucket}] success\n";
    //step2. create an object
    sleep(3);
    $response = $baiduBCS->create_object($bucket, $object, $fileUpload);
    if (!$response->isOK()) {
        die("Create object failed.");
    }
    echo "Create object[{$object}] in bucket[{$bucket}] success\n";
    //step3. download this object
    sleep(3);
    $opt = array("fileWriteTo" => $fileWriteTo);
    $response = $baiduBCS->get_object($bucket, $object, $opt);
    if (!$response->isOK()) {
        die("Download object failed.");
    }
    echo "Download object[{$object}] in bucket[{$bucket}] success. And write to [{$fileWriteTo}]\n";
    //step4. delete this object
    sleep(3);
    $response = $baiduBCS->delete_object($bucket, $object);
    if (!$response->isOK()) {
        die("Delete object failed.");
    }
    echo "Delete object[{$object}] in bucket[{$bucket}] success\n";
    //step5. delete this bucket
    sleep(3);
    $response = $baiduBCS->delete_bucket($bucket);
    if (!$response->isOK()) {
Beispiel #4
0
$host = 'bcs.duapp.com';
//online
$ak = 'QkAPgTkquNrTWqcbEMOOvrq7';
$sk = 'zjtQ4GALm3VtTsr4wm38yRpRcSajD0ZI';
$bucket = 'bigbang-product-pic-1';
$upload_dir = "../";
$object = '/a.txt';
$fileUpload = './a.txt';
$fileWriteTo = './a.' . time() . '.txt';
$baidu_bcs = new BaiduBCS($ak, $sk, $host);
/**
 * ************************single test******************************************* *
 * */
try {
    // $response = $baidu_bcs->create_object ( $bucket, $object, $fileUpload );
    var_dump($baidu_bcs->get_object($bucket, $object));
    // create_bucket ( $baidu_bcs );
    //list_bucket ( $baidu_bcs );
    //list_object ( $baidu_bcs );
    //set_bucket_acl_by_acl_type ( $baidu_bcs );
    //set_bucket_acl_by_json_array ( $baidu_bcs );
    //set_bucket_acl_by_json_string ( $baidu_bcs );
    //get_bucket_acl ( $baidu_bcs );
    //delete_bucket ( $baidu_bcs );
    // create_object ( $baidu_bcs );
    //create_object_superfile ( $baidu_bcs );
    //upload_directory ( $baidu_bcs );
    //copy_object ( $baidu_bcs );
    //set_object_meta ( $baidu_bcs );
    //get_object ( $baidu_bcs );
    //is_object_exist ( $baidu_bcs );