function file_writeBAE($file, $data) { global $_W; $file = str_replace(IA_ROOT.'/', '', $file); $file = $file[0] == '/' ? $file : '/'.$file; $pathinfo = pathinfo($file); $baiduBCS = new BaiduBCS($_W['config']['bae']['ak'], $_W['config']['bae']['sk']); $response = $baiduBCS->create_object_by_content($_W['config']['bae']['bucket'], $file, $data, array('acl' => BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_READ)); if ($response->isOK()) { $baiduBCS->set_object_meta($_W['config']['bae']['bucket'], $file, array("Content-Type" => BCS_MimeTypes::get_mimetype($pathinfo['extension']))); $result['success'] = true; } }
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; }
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'); } }
<?php require dirname(realpath(__FILE__)) . "/../../bcs.class.php"; require dirname(__FILE__) . "/conf.php"; /* * 获取线程数和线程id */ if ($argc != 3) { trigger_error("Can not get thread num"); exit; } $thread_num = $argv[1]; $thread_id = $argv[2]; trigger_error("Upload dir thread start, thread_num=[{$thread_num}], thread_id=[{$thread_id}]"); //开始上传 $baidu_bcs = new BaiduBCS($ak, $sk, $host); $opt = array(BaiduBCS::IMPORT_BCS_PRE_FILTER => 'pre_filter', BaiduBCS::IMPORT_BCS_POST_FILTER => 'post_filter', "prefix" => $prefix, "has_sub_directory" => $has_sub_directory); if (isset($seek_object_id)) { $opt["seek_object_id"] = $seek_object_id; } if (isset($seek_object)) { $opt["seek_object"] = $seek_object; } $baidu_bcs->upload_directory($bucket, $upload_dir, $opt); /* * 可定制化,发送前操作 * 比如返回 boolean型 */ function pre_filter($bucket, $object, $file, &$opt) { //文件名的md5 % thread_num 得到的余数若等于thread_id,上传该文件
/** * 上传 * * @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_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); } }
<?php 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
<?php /** * BCS API sample */ require_once '../bcs.class.php'; $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 );
function del_attachments_from_bcs($file) { 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']); } if (!is_object($baidu_bcs)) { $baidu_bcs = new BaiduBCS($bcs_ak, $bcs_sk); } $bucket = $bcs_bucket; $upload_dir = wp_upload_dir(); $object = str_replace($upload_dir['basedir'], '', $file); $object = ltrim($object, '/'); $object = str_replace('http://bcs.duapp.com/' . $bucket, '', $object); $baidu_bcs->delete_object($bcs_bucket, $object); return $file; }
if ($argc > 1) { $log_path = $argv[1]; } //preg $get_object_all_p = '/id\\[.*\\]/i'; $get_object_p = '/object\\[(.*)\\]file\\[' . preg_quote($upload_dir, '/') . '/i'; $get_file_p = '/file\\[(.*)\\]$/i'; $get_directory_p = '/upload_dir\\[(.+?)\\]/i'; $get_bucket_p = '/bucket\\[(.+?)\\]/i'; $get_file_sum_p = '/file_sum\\[(.+?)\\]/i'; $get_seek_object_id_p = '/seek_object_id\\[(.+?)\\]/i'; $get_seek_object_p = '/seek_object\\[(.+?)\\]/i'; $success_p = "/^Notice: Upload Success .*/"; $failed_p = "/^Notice: Upload Failed .*/"; $directory_info_p = '/^Notice: Upload directory: .*/'; $baidu_bcs = new BaiduBCS($ak, $sk, $host); //找出日志文件 $log_files = BaiduBCS::get_filetree($log_path, $log_file_prefix); sort($log_files); if (!empty($log_files)) { echo "************************************************************\r\n"; echo "************************** log files ***********************\r\n"; echo "************************************************************\r\n"; foreach ($log_files as $log_file) { echo "{$log_file}\r\n"; } } else { echo "No log files found in[{$log_path}]. Please check."; exit(0); } //逐行扫描日志文件,并head 文件是否存在
function file_delete($filename) { if (!IS_BAE) { return unlink($filename); } $arr = explode('/', ltrim($filename, './')); $bucket = C('BUCKET_PREFIX') . strtolower(array_shift($arr)); $path = implode('/', $arr); try { $bcs = new BaiduBCS(); $response = $bcs->delete_object($bucket, '/' . $path); return $response->isOK() ? true : false; } catch (Exception $e) { return false; } }
/** * @param $upload */ function CloudStorage_Del(&$upload) { global $zbp; $bucket = $zbp->Config('CloudStorage')->CS_Bucket; switch ($upload->Metas->CS_Tpye) { 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); $object = str_replace("http://" . $bucket . ".oss.aliyuncs.com/", '', $upload->Metas->CS_URL); $os_service->delete_object($bucket, $object); 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); $client = new Qiniu_MacHttpClient(null); $object = str_replace("http://" . $bucket . ".u.qiniudn.com/", '', $upload->Metas->CS_URL); Qiniu_RS_Delete($client, $bucket, $object); 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 = str_replace("http://bcs.duapp.com/" . $bucket, '', $upload->Metas->CS_URL); $baidu_bcs->delete_object($bucket, $object); break; default: break; } return true; $GLOBALS['Filter_Plugin_Upload_DelFile']['CloudStorage_Del'] = PLUGIN_EXITSIGNAL_RETURN; }
<?php require_once "../include/define.php"; require_once "../include/bcs/bcs.class.php"; set_time_limit(60 * 15); //设置超时为15分钟 error_reporting(E_ERROR | E_PARSE); $bcs = new BaiduBCS(); function curl_download($remote, $local) { $cp = curl_init($remote); $fp = fopen($local, "w"); curl_setopt($cp, CURLOPT_FILE, $fp); curl_setopt($cp, CURLOPT_HEADER, 0); curl_exec($cp); curl_close($cp); fclose($fp); } function upload2bcsbyurl($remote, $url) { global $bcs; $name = basename($url); $local = './objs/' . time() . '_' . $name; curl_download($url, $local); $response = $bcs->create_object(BCS_BUCKET, $remote, $local); unlink($local); echo 'upload "' . $remote . '" to bcs!' . '<br/>'; } function url_exists($urlpath) { $h = get_headers($urlpath);
/** * 上传 * * @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); }