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 |
public function create_object($bucket, $object, $file, $opt = array())
{
$this->assertParameterArray($opt);
$opt[self::BUCKET] = $bucket;
$opt[self::OBJECT] = $object;
$opt['fileUpload'] = $file;
$opt[self::METHOD] = 'PUT';
if (isset($opt['acl'])) {
if (in_array($opt['acl'], self::$ACL_TYPES)) {
self::set_header_into_opt("x-bs-acl", $opt['acl'], $opt);
} else {
throw new BCS_Exception("Invalid acl string, it should be acl_type", -1);
}
unset($opt['acl']);
}
if (isset($opt['filename'])) {
self::set_header_into_opt("Content-Disposition", 'attachment; filename=' . $opt['filename'], $opt);
}
$response = $this->authenticate($opt);
//$this->log ( $response->isOK () ? "Create object[$object] file[$file] success!" : "Create object[$object] file[$file] failed! Response: [" . $response->body . "] Logid[" . $response->header ["x-bs-request-id"] . "]", $opt );
return $response;
}
/** * 兼容 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; }