已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > PHP > 教程

标题: 快速使用又拍云OSS-SDK

作者: @Ta

时间: 2019-06-28发布,2019-06-28修改

点击: 2271

使用Composer安装
 composer require upyun/sdk
安装完毕
635385b8ffb087932f7b3ef4d193b18633204.png

上传到Upyun 一共两种方式:
1:使用一次性TOKEN嵌入FORM表单,由Cient直接对Upyun服务器传输。(平民用这个,平民靠科技)
2:使用Server服务器对Upyun服务器传输,是一种中转传输的方式。(土豪用这个)

1:方法
<?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']
        ]);
    }
}

2f2d6fdbd0921f0e47081b70a89bafa786092.png
如果填写return-url 传递完毕后client端会被301到填写的地址,并且带上参数,主要是给不太熟悉Ajax上传,使用form上传的后端开发者使用



查看OSS文件
 public function getIndex()
    {
        $result=$this->upyun->read('/',null,['X-List-Limit'=>2]);
        dump($result);
        return view('oss/index');
    }


ad1186e93edae8ef8298e4e9e008662757590.png

写在最后:
1:防止盗链,强烈建议使用upyun的签名防盗链,类似于支付宝签名校验,不可伪造,无法盗链。
类似  hash("文件名-有效时间-私钥-时间戳","md5")
2:图片处理(自动水印),请现在Upyun新建图片模板,例如我取名为right.bottom.api(右下角水印文字)
则,你的水印缩略图访问地址为(使用!分割,可以自定义)  http://你的oss域名.com/logo.png!right.bottom.api


[隐藏样式|查看源码]


『回复列表(0|隐藏机器人聊天)』

帖子没有回复
回复需要登录

8月22日 21:27 星期五

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1