标题: 快速使用又拍云OSS-SDK
时间: 2019-06-28发布,2019-06-28修改
<?php
/**
* Created by PhpStorm. IEDUser: admin
* Powery by 猫腻
* major-> Mobile Application develop Class
* Date: 2019/6/22 20:13
*/
namespace app\gateway\controller;
use think\Controller;
use Upyun\Config as upyunConfig;
use Upyun\Upyun;
use Upyun\Util;
use Upyun\Signature;
class Upload extends Controller
{
public $upyunConfig;
public $upyun;
/**
* 初始化又拍云对象
*/
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
$this->upyunConfig=new upyunConfig('oss-objectname','username','KEY');
$this->upyun=new Upyun( $this->upyunConfig);
}
//2:使用Server服务器对Upyun服务器传输,是一种中转传输的方式。(土豪用这个)
public function postUpload()
{
//获取到上传图片/文件对象
$png=$this->request->file('img');
$info=$png->getInfo();
//根目录下原来客户端上传的文件名, 从客户端上传上来的文件二进制流
$result=$this->upyun->write('/'.$info['name'], file_get_contents($info['tmp_name']));
dump($result);
}
//在OSS上新建目录,传入目录名
public function getMikdir($dirname)
{
$result=$this->upyun->createdir($dirname);
dump($result);
}
//1:使用一次性TOKEN嵌入FORM表单,由Cient直接对Upyun服务器传输。(平民用这个,平民靠科技)
//请看第2张图片,把policy,authorization,bucket传入到Client端的form中,你可以使用Ajax上传,注意跨域问题
public function getUpload()
{
//保存到baidu文件夹下,并且以随机32位.后缀名保存
$data['save-key'] = '/baidu/{random32}{.suffix}';
$data['expiration'] = time() + 120;
$data['bucket'] = $this->upyunConfig->serviceName;
//如果填写return-url 传递完毕后client端会被301到填写的地址,主要是给不太熟悉Ajax上传,使用form上传的后端开发者使用
$data['return-url']=$this->request->domain().'/gateway/Oss/Upload';
$policy = Util::base64Json($data);
$method = 'POST';
$uri = '/' . $data['bucket'];
$signature = Signature::getBodySignature( $this->upyunConfig, $method, $uri, null, $policy);
return view('oss/upload_page',[
'policy' => $policy,
'authorization' => $signature,
'bucket'=>$data['bucket']
]);
}
}
public function getIndex()
{
$result=$this->upyun->read('/',null,['X-List-Limit'=>2]);
dump($result);
return view('oss/index');
}
『回复列表(0|隐藏机器人聊天)』