public static function upload_image($obj_path, $file_path) { $bucket = SampleUtil::get_bucket_name(); $oss = SampleUtil::get_oss_client(); SampleUtil::create_bucket(); $object = $obj_path; /*%**************************************************************************************************************%*/ // Multipart 相关的示例 /** *通过multipart上传文件 *如果上传的文件小于partSize,则直接使用普通方式上传 */ $filepath = $file_path; $options = array(ALIOSS::OSS_FILE_UPLOAD => $filepath, 'partSize' => 5242880); $res = $oss->create_mpu_object($bucket, $object, $options); $msg = "通过multipart上传文件"; OSSUtil::print_res($res, $msg); }
function sign_url_for_put($obj) { global $bucket; global $object; $timeout = 3600; //通过content上传 $options = NULL; $response = $obj->presign_url($bucket, $object, $timeout, "PUT", $options); SampleUtil::my_echo("签名的URL为:" . $response); $content = 'abcdefg'; $request = new RequestCore($response); $request->set_method('PUT'); $request->add_header('Content-Type', ''); $request->add_header('Content-Length', strlen($content)); $request->set_body($content); $request->send_request(); $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code()); if ($res->isOK()) { SampleUtil::my_echo("签名上传字符串成功"); } else { SampleUtil::my_echo("签名上传字符串失败"); } //通过file上传 $file = __FILE__; if (!file_exists($file)) { throw new OSS_Exception($file . OSS_FILE_NOT_EXIST); } $options = array('Content-Type' => 'txt'); $response = $obj->presign_url($bucket, $object, $timeout, "PUT", $options); SampleUtil::my_echo("签名的URL为:" . $response); $request = new RequestCore($response); $request->set_method('PUT'); $request->add_header('Content-Type', 'txt'); $request->set_read_file($file); $request->set_read_stream_size(filesize($file)); $request->send_request(); $res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code()); if ($res->isOK()) { SampleUtil::my_echo("签名上传文件成功"); } else { SampleUtil::my_echo("签名上传字符串失败"); } }
function get_lifecycle($oss, $bucket) { sleep(1); $res = $oss->get_bucket_lifecycle($bucket); $msg = "获取lifecycle规则 bucket " . $bucket; OSSUtil::print_res($res, $msg); if ($res->isOK()) { SampleUtil::my_echo($res->body); } else { if ($res->status === 404) { SampleUtil::my_echo("无lifecycle规则"); } } }
$index++; } } if (empty($next_marker)) { break; } } echo "<hr/>"; /** *列出目录下的文件和子目录 */ $prefix = ''; $marker = ''; $delimiter = '/'; $next_marker = ''; $maxkeys = 1000; $index = 1; $options = array('delimiter' => $delimiter, 'prefix' => $prefix, 'max-keys' => $maxkeys, 'marker' => $next_marker); $res = $oss->list_object($bucket, $options); $msg = "列出目录下的文件和子目录: " . $bucket; OSSUtil::print_res($res, $msg); if ($res->isOk()) { $body = $res->body; $xml = new SimpleXMLElement($body); foreach ($xml->Contents as $content) { SampleUtil::my_echo("文件列表: " . $content->Key); } foreach ($xml->CommonPrefixes as $content) { SampleUtil::my_echo("子目录列表: " . $content->Prefix); } }
$res = $oss->get_bucket_acl($bucket); $msg = "获取 bucket " . $bucket . " acl"; OSSUtil::print_res($res, $msg); if ($res->isOK()) { $xml = new SimpleXMLElement($res->body); SampleUtil::my_echo("Bucket acl is " . $xml->AccessControlList->Grant); } /** *显示创建的bucket *列出用户所有的Bucket */ $res = $oss->list_bucket(); $msg = "列出用户所有的Bucket"; OSSUtil::print_res($res, $msg); $index = 1; $max = 10; if ($res->isOk()) { $xml = new SimpleXMLElement($res->body); $_bucket_list = array(); foreach ($xml->Buckets->Bucket as $_buckets) { array_push($_bucket_list, $_buckets->Name); } foreach ($_bucket_list as $key) { SampleUtil::my_echo("Bucket No. " . $index . ": " . $key); $index++; if ($index > $max) { SampleUtil::my_echo("for test, list " . $max . " buckets at most, break"); break; } } }
$res = $oss->get_object_meta($bucket, $object); $msg = "获取object meta"; OSSUtil::print_res($res, $msg); if ($res->isOK()) { SampleUtil::my_echo("object content-type is " . $res->header['content-type']); SampleUtil::my_echo("object content-length is " . $res->header['content-length']); } /** *删除object */ $res = $oss->delete_object($bucket, $object); $msg = "删除object"; OSSUtil::print_res($res, $msg); /** *批量删除object */ $objects = array($object); $options = array(); $res = $oss->delete_objects($bucket, $objects, $options); $msg = "批量删除object"; OSSUtil::print_res($res, $msg); /** *判断object是否存在 */ $res = $oss->is_object_exist($bucket, $object); if ($res->status === 404) { SampleUtil::my_echo("object 不存在"); } if ($res->status === 200) { SampleUtil::my_echo("object 存在"); }
<?php if ($_POST['atc'] == 'up') { header("Content-Type: text/html; charset=UTF-8"); require_once 'sample_base.php'; //初始化 //$bucket = SampleUtil::get_bucket_name(); $bucket = 'fangpinhui-api'; $oss = SampleUtil::get_oss_client(); //SampleUtil::create_bucket(); /*%**************************************************************************************************************%*/ /** *简单上传 *上传指定的本地文件内容 */ $format = explode('.', $_FILES["img"]['name']); $file_tmp = $_FILES["img"]['tmp_name']; $real_name = $_FILES["img"]['name']; $file_name = dirname($file_tmp) . "/" . $real_name; rename($file_tmp, $file_name); $object = $real_name; $file_path = $file_name; $options = array(); $res = $oss->upload_file_by_file($bucket, $object, $file_path, $options); echo '<pre>'; print_r((array) $res); $msg = "上传本地文件 :" . $file_path . " 到 /" . $bucket . "/" . $object; OSSUtil::print_res($res, $msg); } ?>
$msg = "获取所有已上传的块信息"; OSSUtil::print_res($res, $msg); if ($res->isOK()) { SampleUtil::my_echo($res->body); } //完成分块上传 #complete as string #此处只是一个例子,执行不会成功 $msg = "完成分块上传"; $xml_string = '<?xml version="1.0" encoding="utf-8"?><CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>test</ETag></Part></CompleteMultipartUpload>'; $res = $oss->complete_multipart_upload($bucket, $object, $upload_id, $xml_string); OSSUtil::print_res($res, $msg); #complete as xml #此处只是一个例子,执行不会成功 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><CompleteMultipartUpload></CompleteMultipartUpload>'); $part = $xml->addChild('Part'); $part->addChild('PartNumber', 1); $part->addChild('ETag', 'abcdeft'); $input = $xml; $res = $oss->complete_multipart_upload($bucket, $object, $upload_id, $input); OSSUtil::print_res($res, $msg); #complete as array #执行会成功 $res = $oss->complete_multipart_upload($bucket, $object, $upload_id, $upload_parts); OSSUtil::print_res($res, $msg); //取消分块上传事件 $upload_id = $oss->init_multipart_upload($bucket, $object); SampleUtil::my_echo("初始化一个分块上传事件 upload_id is:" . $upload_id); $res = $oss->abort_multipart_upload($bucket, $object, $upload_id); $msg = "取消分块上传事件"; OSSUtil::print_res($res, $msg);
function uploadDir($oss, $bucket) { $dir = getcwd(); $options = array('bucket' => $bucket, 'object' => 'picture', 'directory' => $dir); $res = $oss->batch_upload_file($options); if ($res === true) { SampleUtil::my_echo("通过multipart上传整个目录成功"); } }