create_object() public method

上传文件
public create_object ( string $bucket, string $object, string $file, array $opt = [] ) : BCS_ResponseCore
$bucket string (Required)
$object string (Required)
$file string (Required); 需要上传的文件的文件路径
$opt array (Optional) filename - Optional; 指定文件名 acl - Optional ; 上传文件的acl,只能使用acl_type seekTo - Optional; 上传文件的偏移位置 length - Optional; 待上传长度
return BCS_ResponseCore
Esempio n. 1
0
/**
 * 兼容 file_upload 函数
 */
function file_uploadBAE($file, $type)
{
    global $_W;
    $settings = $_W['uploadsetting'];
    $result = array('error' => 1, 'message' => '');
    if (empty($_W['config']['bae']['ak']) || empty($_W['config']['bae']['sk'])) {
        return error(-1, '请设置BAE的存储AK与SK');
    }
    $extention = pathinfo($file['name'], PATHINFO_EXTENSION);
    $result = array();
    $result['path'] = "/{$settings[$type]['folder']}/" . date('Y/m/');
    do {
        $filename = random(30) . ".{$extention}";
    } while (file_exists(IA_ROOT . $path . $filename));
    $result['path'] .= $filename;
    $result['url'] = 'http://bcs.duapp.com/' . $_W['config']['bae']['bucket'] . $result['path'];
    $baiduBCS = new BaiduBCS($_W['config']['bae']['ak'], $_W['config']['bae']['sk']);
    try {
        $response = $baiduBCS->create_object($_W['config']['bae']['bucket'], $result['path'], $file['tmp_name'], array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ));
    } catch (Exception $e) {
        return error(-1, $e->getMessage());
    }
    if ($response->isOK()) {
        $baiduBCS->set_object_meta($_W['config']['bae']['bucket'], $result['path'], array("Content-Type" => BCS_MimeTypes::get_mimetype($extention)));
        $result['success'] = true;
    }
    return $result;
}
Esempio n. 2
0
 public function upload($imageUrls)
 {
     $conf = $this->getDI()->get('config');
     $bcs = new \BaiduBCS($conf->bcs->ak, $conf->bcs->sk, $conf->bcs->host);
     foreach ($imageUrls as $objName => $fileUpload) {
         $response = $bcs->create_object($conf->bcs->bucket, $objName, $fileUpload, array('acl' => \BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ));
         if (!$response->isOK()) {
             foreach ($imageUrls as $objName => $fileUpload) {
                 $bcs->delete_object($conf->bcs->bucket, $objName);
             }
             return false;
         }
     }
     return true;
 }
Esempio n. 3
0
function mv_attachments_to_bcs($data)
{
    require_once 'bcs.class.php';
    $bcs_options = get_option('bcs_options', TRUE);
    $bcs_bucket = attribute_escape($bcs_options['bucket']);
    if (false === getenv('HTTP_BAE_ENV_AK')) {
        $bcs_ak = attribute_escape($bcs_options['ak']);
    }
    if (false === getenv('HTTP_BAE_ENV_SK')) {
        $bcs_sk = attribute_escape($bcs_options['sk']);
    }
    $is_Enabled_asl = attribute_escape($bcs_options['is_Enabled_asl']);
    $hiPath = attribute_escape($bcs_options['hiPath']);
    if (empty($hiPath)) {
        $hiPath = "/blog/{year}{month}/";
    }
    $baidu_bcs = new BaiduBCS($bcs_ak, $bcs_sk);
    $bucket = $bcs_bucket;
    $year = date("Y");
    $month = date("m");
    $day = date("d");
    $hiPath = str_replace('{day}', $day, $hiPath);
    $hiPath = str_replace('{month}', $month, $hiPath);
    $hiPath = str_replace('{year}', $year, $hiPath);
    $object = "/" . $hiPath . "/" . basename($data['file']);
    $file = $data['file'];
    $opt = array("acl" => "public-read");
    $baidu_bcs->create_object($bucket, $object, $file, $opt);
    if ($is_Enabled_asl) {
        $referer = attribute_escape($bcs_options['referer']);
        $referer2 = attribute_escape($bcs_options['referer2']);
        if (!empty($referer)) {
            if (!empty($referer2)) {
                $referer = array($referer, $referer2);
            } else {
                $referer = array($referer);
            }
        } else {
            $referer = preg_replace('#^www\\.#', '', strtolower($_SERVER['SERVER_NAME']));
            $referer = '*.' . preg_replace('#^blog\\.#', '', $referer) . '/*';
            $referer = array($referer);
        }
        $acl = array('statements' => array('0' => array('user' => array("*"), 'resource' => array($bucket . $object), 'action' => array(BaiduBCS::BCS_SDK_ACL_ACTION_GET_OBJECT), 'effect' => BaiduBCS::BCS_SDK_ACL_EFFECT_ALLOW, 'referer' => $referer)));
        $baidu_bcs->set_object_acl($bucket, $object, $acl);
    }
    $url = "http://bcs.duapp.com/{$bucket}{$object}";
    return array('file' => $url, 'url' => $url, 'type' => $data['type']);
}
Esempio n. 4
0
 public function baidutest()
 {
     $ak = $this->input->get('ak');
     $sk = $this->input->get('sk');
     $host = $this->input->get('host');
     $rurl = $this->input->get('rurl');
     $bucket = $this->input->get('bucket');
     if (!$ak || !$host || !$sk || !$bucket) {
         exit(lang('035'));
     }
     if (!$rurl) {
         exit(lang('199'));
     }
     require_once FCPATH . 'omooo/libraries/BaiduBCS/bcs.class.php';
     $bcs = new BaiduBCS($ak, $sk, $host);
     $opt = array();
     $opt['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE;
     $opt['curlopts'] = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 1800);
     $response = $bcs->create_object($bucket, '/test.txt', FCPATH . 'index.php', $opt);
     if ($response->status == 200) {
         if (strpos(dr_catcher_data($rurl . '/test.txt'), 'omooo.com') === FALSE) {
             exit(lang('200'));
         }
         $bcs->delete_object($bucket, '/test.txt');
         exit('ok');
     } else {
         exit('error');
     }
 }
Esempio n. 5
0
 /**
  * 上传
  *
  * @param	intval	$uid	uid	用户id
  * @param	array	$info	ci 文件上传成功返回数据
  * @return	array
  */
 public function upload($uid, $info)
 {
     $_ext = strtolower(substr($info['file_ext'], 1));
     $author = $this->_get_member_name($uid);
     $content = file_get_contents($info['full_path']);
     // 入库附件
     $this->db->replace('attachment', array('uid' => $uid, 'author' => $author, 'siteid' => $this->siteid, 'tableid' => 0, 'related' => '', 'fileext' => $_ext, 'filemd5' => $content ? md5($content) : 0, 'download' => 0, 'filesize' => $info['file_size'] * 1024));
     $id = $this->db->insert_id();
     unset($content);
     // 入库失败,返回错误且删除附件
     if (!$id) {
         @unlink($info['full_path']);
         return lang('m-145');
     }
     $remote = 0;
     $attachment = $file = substr($info['full_path'], strlen(FCPATH));
     // 附件配置信息
     $config = $this->ci->get_cache('siteinfo', SITE_ID, 'remote', SITE_ATTACH_REMOTE);
     // 远程附件模式
     if ($config && $config['SITE_ATTACH_EXTS'] && ($_exts = explode(',', $config['SITE_ATTACH_EXTS'])) && in_array($_ext, $_exts)) {
         // 根据模式来存储
         set_time_limit(0);
         if (SITE_ATTACH_REMOTE == 1) {
             // ftp附件模式
             $this->load->library('ftp');
             if ($this->ftp->connect(array('port' => $config['SITE_ATTACH_PORT'], 'debug' => FALSE, 'passive' => $config['SITE_ATTACH_PASV'], 'hostname' => $config['SITE_ATTACH_HOST'], 'username' => $config['SITE_ATTACH_USERNAME'], 'password' => $config['SITE_ATTACH_PASSWORD']))) {
                 // 连接ftp成功
                 $dir = basename(dirname($info['full_path'])) . '/';
                 $file = basename($info['full_path']);
                 $path = $config['SITE_ATTACH_PATH'] . '/' . $dir;
                 $this->ftp->mkdir($path);
                 if ($this->ftp->upload($info['full_path'], $path . $file, $config['SITE_ATTACH_MODE'], 0775)) {
                     $remote = 1;
                     $attachment = $dir . $file;
                     $file = $config['SITE_ATTACH_URL'] . '/' . $attachment;
                     unlink($info['full_path']);
                 }
                 $this->ftp->close();
             } else {
                 log_message('error', '远程附件ftp模式:ftp连接失败');
             }
         } elseif (SITE_ATTACH_REMOTE == 2) {
             // 百度云存储模式
             $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']);
             require_once FCPATH . 'omooo/libraries/BaiduBCS/bcs.class.php';
             $bcs = new BaiduBCS($config['ak'], $config['sk'], $config['host']);
             $opt = array();
             $opt['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE;
             $opt['curlopts'] = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 1800);
             $response = $bcs->create_object($config['bucket'], '/' . $file, $info['full_path'], $opt);
             if ($response->status == 200) {
                 $remote = 2;
                 $attachment = $file;
                 $file = $config['SITE_ATTACH_URL'] . '/' . $attachment;
                 unlink($info['full_path']);
             } else {
                 log_message('error', '远程附件百度云存储失败');
             }
         } elseif (SITE_ATTACH_REMOTE == 3) {
             // 阿里云存储模式
             $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']);
             require_once FCPATH . 'omooo/libraries/AliyunOSS/sdk.class.php';
             $oss = new ALIOSS($config['id'], $config['secret'], $config['host']);
             $oss->set_debug_mode(FALSE);
             $response = $oss->upload_file_by_file($config['bucket'], $file, $info['full_path']);
             if ($response->status == 200) {
                 $remote = 3;
                 $attachment = $file;
                 $file = $config['SITE_ATTACH_URL'] . '/' . $attachment;
                 unlink($info['full_path']);
             } else {
                 log_message('error', '远程附件阿里云存储模式:' . $response->body);
             }
         }
     }
     // 非远程附件补全本地地址
     if (!$remote) {
         $file = SITE_URL . $file;
     }
     $pos = strrpos($info['client_name'], '.');
     $filename = strpos($info['client_name'], 'http://') === 0 ? trim(strrchr($info['client_name'], '/'), '/') : $info['client_name'];
     $filename = $pos ? substr($filename, 0, $pos) : $filename;
     // 增加至未使用附件表
     $this->db->replace('attachment_unused', array('id' => $id, 'uid' => $uid, 'author' => $author, 'siteid' => $this->siteid, 'remote' => $remote, 'fileext' => $_ext, 'filename' => $filename, 'filesize' => $info['file_size'] * 1024, 'inputtime' => SYS_TIME, 'attachment' => $attachment, 'attachinfo' => ''));
     return array($id, $file, $_ext);
 }
function file_upload($src_file, $dest_file)
{
    if (!IS_BAE) {
        $pdir = dirname($dest_file);
        if (!is_dir($pdir)) {
            @mkdir($pdir, 0777);
        }
        return copy($src_file, $dest_file);
    }
    $arr = explode('/', ltrim($dest_file, './'));
    $bucket = C('BUCKET_PREFIX') . strtolower(array_shift($arr));
    $save_path = implode('/', $arr);
    try {
        $bcs = new BaiduBCS();
        $response = $bcs->create_object($bucket, '/' . $save_path, $src_file, array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ));
        return $response->isOK() ? true : false;
    } catch (Exception $e) {
        return false;
    }
}
require_once '../bcs.class.php';
$host = '';
$ak = '';
$sk = '';
$bucket = 'bcs-phpsdk-simple-start-' . time() . "-" . rand(1000, 999);
$object = '/a.txt';
$fileUpload = './a.txt';
$fileWriteTo = './a.' . time() . '.txt';
$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);
Esempio n. 8
0
/**
 * @param $tmp
 * @param $upload
 */
function CloudStorage($tmp, &$upload)
{
    global $zbp;
    $bucket = $zbp->Config('CloudStorage')->CS_Bucket;
    //云文件夹
    $filename = date("Ymd", time()) . mt_rand(1000, 9999) . '_' . mt_rand(0, 1000) . '.' . GetFileExt($upload->SourceName);
    $object = $zbp->Config('CloudStorage')->CS_Dir . date("Y/m/", time()) . $filename;
    //构造云文件名
    $file_path = $zbp->usersdir . 'upload/tmp.data';
    //本地临时文件地址
    @move_uploaded_file($tmp, $file_path);
    //先上传到本地
    $upload->Name = $filename;
    switch ($zbp->Config('CloudStorage')->CS_Storage) {
        case '1':
            define('OSS_ACCESS_ID', $zbp->Config('CloudStorage')->CS_Ali_KeyID);
            //ACCESS_ID
            define('OSS_ACCESS_KEY', $zbp->Config('CloudStorage')->CS_Ali_KeySecret);
            //ACCESS_KEY
            require_once dirname(__FILE__) . '/api/oss/sdk.class.php';
            $os_service = new ALIOSS();
            $os_service->set_debug_mode(FALSE);
            //$os_service->set_enable_domain_style(TRUE);//二级域名
            $response = $os_service->upload_file_by_file($bucket, $object, $file_path);
            //将本地文件上传到云
            $upload->Metas->CS_URL = $response->header['_info']['url'];
            break;
        case '2':
            require_once dirname(__FILE__) . '/api/qiniu/io.php';
            require_once dirname(__FILE__) . '/api/qiniu/rs.php';
            $accessKey = $zbp->Config('CloudStorage')->CS_QNiu_KeyID;
            $secretKey = $zbp->Config('CloudStorage')->CS_QNiu_KeySecret;
            Qiniu_SetKeys($accessKey, $secretKey);
            $putPolicy = new Qiniu_RS_PutPolicy($bucket);
            $upToken = $putPolicy->Token(null);
            //$putExtra = new Qiniu_PutExtra();
            //$putExtra->Crc32 = 1;
            list($ret, $err) = Qiniu_PutFile($upToken, $object, $file_path, null);
            $upload->Metas->CS_URL = 'http://' . $bucket . '.qiniudn.com/' . $ret['key'];
            //20140509更新V1.1:删除u
            break;
        case '3':
            define('BCS_AK', $zbp->Config('CloudStorage')->CS_Baidu_KeyID);
            //AK 公钥
            define('BCS_SK', $zbp->Config('CloudStorage')->CS_Baidu_KeySecret);
            //SK 私钥
            require_once dirname(__FILE__) . '/api/bcs/bcs.class.php';
            $baidu_bcs = new BaiduBCS();
            $object = "/" . $object;
            $response = $baidu_bcs->create_object($bucket, $object, $file_path, array('acl' => 'public-read'));
            //var_dump($response);die();
            $upload->Metas->CS_URL = "http://bcs.duapp.com/" . $bucket . $object;
            //$response->header['_info']['url'];
            //未解决文件标识
            break;
        default:
            break;
    }
    $upload->Metas->CS_Tpye = $zbp->Config('CloudStorage')->CS_Storage;
    unlink($file_path);
    //删除本地文件
    $GLOBALS['Filter_Plugin_Upload_SaveFile']['CloudStorage'] = PLUGIN_EXITSIGNAL_RETURN;
    return true;
}
 /**
  * 上传
  *
  * @param	intval	$uid	uid	用户id
  * @param	array	$info	ci 文件上传成功返回数据
  * @param	intval	$id	id	指定附件id
  * @return	array
  */
 public function upload($uid, $info, $id = 0)
 {
     $_ext = strtolower(substr($info['file_ext'], 1));
     $author = $this->_get_member_name($uid);
     $replace = 0;
     $content = file_get_contents($info['full_path']);
     // 查询指定附件
     if ($id) {
         $row = $this->db->where('id', $id)->get('attachment')->row_array();
         if ($row) {
             $replace = 1;
             $this->siteid = intval($row['siteid']);
         } else {
             return '当前附件不存在';
         }
     }
     // 入库附件
     if (!$id) {
         $this->db->replace('attachment', array('uid' => (int) $uid, 'author' => $author, 'siteid' => $this->siteid, 'tableid' => 0, 'related' => '', 'fileext' => $_ext, 'filemd5' => $content ? md5($content) : 0, 'download' => 0, 'filesize' => $info['file_size'] * 1024));
         $id = $this->db->insert_id();
         // 入库失败,返回错误且删除附件
         if (!$id) {
             @unlink($info['full_path']);
             return lang('m-145');
         }
     }
     $remote = 0;
     $attachment = $file = (SYS_UPLOAD_DIR ? SYS_UPLOAD_DIR . '/' : '') . trim(substr($info['full_path'], strlen(SYS_UPLOAD_PATH)), '/');
     // 远程附件信息
     $remote_cfg = $this->ci->get_cache('attachment');
     if (isset($remote_cfg[$this->siteid]['ext'][$_ext]) && ($rid = $remote_cfg[$this->siteid]['ext'][$_ext])) {
         $config = $remote_cfg[$this->siteid]['data'][$rid];
         // 根据模式来存储
         set_time_limit(0);
         if ($config['type'] == 1) {
             // ftp附件模式
             $this->load->library('ftp');
             if ($this->ftp->connect(array('port' => $config['value']['port'], 'debug' => FALSE, 'passive' => $config['value']['pasv'], 'hostname' => $config['value']['host'], 'username' => $config['value']['username'], 'password' => $config['value']['password']))) {
                 // 连接ftp成功
                 $dir = basename(dirname($info['full_path'])) . '/';
                 $file = basename($info['full_path']);
                 $path = $config['value']['path'] . '/' . $dir;
                 $this->ftp->mkdir($path);
                 if ($this->ftp->upload($info['full_path'], $path . $file, $config['value']['mode'], 0775)) {
                     $remote = $config['id'];
                     $attachment = $dir . $file;
                     $file = $config['url'] . '/' . $attachment;
                     unlink($info['full_path']);
                 }
                 $this->ftp->close();
             } else {
                 log_message('error', '远程附件ftp模式:ftp连接失败');
             }
         } elseif ($config['type'] == 2) {
             // 百度云存储模式
             $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']);
             require_once FCPATH . 'dayrui/libraries/BaiduBCS/bcs.class.php';
             $bcs = new BaiduBCS($config['value']['ak'], $config['value']['sk'], $config['value']['host']);
             $opt = array();
             $opt['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE;
             $opt['curlopts'] = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 1800);
             $response = $bcs->create_object($config['value']['bucket'], '/' . $file, $info['full_path'], $opt);
             if ($response->status == 200) {
                 $remote = $config['id'];
                 $attachment = $file;
                 $file = $config['url'] . '/' . $attachment;
                 unlink($info['full_path']);
             } else {
                 log_message('error', '远程附件百度云存储失败');
             }
         } elseif ($config['type'] == 3) {
             // 阿里云存储模式
             $file = basename(dirname($info['full_path'])) . '/' . basename($info['full_path']);
             require_once FCPATH . 'dayrui/libraries/AliyunOSS/sdk.class.php';
             $oss = new ALIOSS($config['value']['id'], $config['value']['secret'], $config['value']['host']);
             $oss->set_debug_mode(FALSE);
             $response = $oss->upload_file_by_file($config['value']['bucket'], $file, $info['full_path']);
             if ($response->status == 200) {
                 $remote = $config['id'];
                 $attachment = $file;
                 $file = $config['url'] . '/' . $attachment;
                 unlink($info['full_path']);
             } else {
                 log_message('error', '远程附件阿里云存储模式:' . $response->body);
             }
         } else {
             log_message('error', '远程附件类别(#' . (int) $config['type'] . ')未定义');
         }
     }
     // 非远程附件补全本地地址
     $file = !$remote ? SYS_ATTACHMENT_URL . $file : $file;
     $pos = strrpos($info['client_name'], '.');
     $filename = strpos($info['client_name'], 'http://') === 0 ? trim(strrchr($info['client_name'], '/'), '/') : $info['client_name'];
     $filename = $pos ? substr($filename, 0, $pos) : $filename;
     if ($replace) {
         // 替换主表
         $this->db->where('id', $id)->update('attachment', array('author' => $author, 'fileext' => $_ext, 'filemd5' => $content ? md5($content) : 0, 'filesize' => $info['file_size'] * 1024));
         // 更新替换已使用的附件表
         $this->db->where('id', $id)->update('attachment_' . $row['tableid'], array('uid' => $uid, 'author' => $author, 'remote' => $remote, 'fileext' => $_ext, 'filename' => $filename, 'filesize' => $info['file_size'] * 1024, 'attachment' => $attachment));
     } else {
         // 增加至未使用附件表
         $this->db->replace('attachment_unused', array('id' => $id, 'uid' => $uid, 'author' => $author, 'siteid' => $this->siteid, 'remote' => $remote, 'fileext' => $_ext, 'filename' => $filename, 'filesize' => $info['file_size'] * 1024, 'inputtime' => SYS_TIME, 'attachment' => $attachment, 'attachinfo' => ''));
     }
     return $replace ? $row : array($id, $file, $_ext);
 }