oss前端直传

时间:2021-12-11 14:07:18

web端postObject之服务端签名后直传

图片.png

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

/**

     * OSS超直传签名

     * @return array

     * @author: luwc

     * @Time: 2021/12/11 10:56

     */

    public function actionOssSign()

    {

        $oss = \Yii::$app->params['oss'];

        $dir 'userprefix/' date('Ymd') . '/';          // 用户上传文件时指定的前缀。

        $callbackUrl = \Yii::$app->request->hostInfo . '/api/index/oss-call-back';

        $base64_callback_body base64_encode(json_encode([

            'callbackUrl' => $callbackUrl,

            'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}',

            'callbackBodyType' => "application/x-www-form-urlencoded"

        ]));

        $now = time();

        $expire = 30;  //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问。

        $end $now $expire;

        $expiration str_replace('+00:00''.000Z'gmdate('c'$end));

        $conditions = [

            //最大文件大小.用户可以自己设置

            [0 => 'content-length-range', 1 => 0, 2 => 1048576000],

            // 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。

            [0 => 'starts-with', 1 => '$key', 2 => $dir]

        ];

        $base64_policy base64_encode(json_encode(

            ['expiration' => $expiration'conditions' => $conditions]

        ));

        $string_to_sign $base64_policy;

        $signature base64_encode(hash_hmac('sha1'$string_to_sign$oss['accessKeySecret'], true));

        $response = [];

        $response['accessid'] = $oss['accessKeyId'];

        $response['host'] = 'http://' .$oss['bucket'].'.'$oss['endPoint'];

        $response['policy'] = $base64_policy;

        $response['signature'] = $signature;

        $response['expire'] = $end;

        $response['callback'] = $base64_callback_body;

        $response['dir'] = $dir;  // 这个参数是设置用户上传文件时指定的前缀。

        return $this->success($response);

    }

    /**

     * OSS超直传回调

     * @author: luwc

     * @Time: 2021/12/11 11:04

     */

    public function actionOssCallBack()

    {

        // 1.获取OSS的签名header和公钥url header

        $authorizationBase64 "";

        $pubKeyUrlBase64 "";

        /*

         * 注意:如果要使用HTTP_AUTHORIZATION头,你需要先在apache或者nginx中设置rewrite,以apache为例,修改

         * 配置文件/etc/httpd/conf/httpd.conf(以你的apache安装路径为准),在DirectoryIndex index.php这行下面增加以下两行

            RewriteEngine On

            RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]

         * */

        if (isset($_SERVER['HTTP_AUTHORIZATION'])) {

            $authorizationBase64 $_SERVER['HTTP_AUTHORIZATION'];

        }

        if (isset($_SERVER['HTTP_X_OSS_PUB_KEY_URL'])) {

            $pubKeyUrlBase64 $_SERVER['HTTP_X_OSS_PUB_KEY_URL'];

        }

        \Yii::info(print_r([$authorizationBase64$pubKeyUrlBase64], 'OSS超直传回调'));

        if ($authorizationBase64 == '' || $pubKeyUrlBase64 == '') {

            return $this->error([],'授权参数错误');

        }

        // 2.获取OSS的签名

        $authorization base64_decode($authorizationBase64);

        // 3.获取公钥

        $pubKeyUrl base64_decode($pubKeyUrlBase64);

        \Yii::info(print_r([$pubKeyUrl], 'OSS超直传回调'));

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $pubKeyUrl);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

        $pubKey = curl_exec($ch);

        \Yii::info(print_r([$pubKey], 'OSS超直传回调'));

        if ($pubKey == "") {

//            header("http/1.1 403 Forbidden");

            return $this->error([],'获取公钥失败');

        }

        // 4.获取回调body

        $body file_get_contents('php://input');

        // 5.拼接待签名字符串

        $authStr '';

        $path $_SERVER['REQUEST_URI'];

        $pos strpos($path'?');

        if ($pos === false) {

            $authStr = urldecode($path) . "\n" $body;

        else {

            $authStr = urldecode(substr($path, 0, $pos)) . substr($path$posstrlen($path) - $pos) . "\n" $body;

        }

        // 6.验证签名

        $ok = openssl_verify($authStr$authorization$pubKey, OPENSSL_ALGO_MD5);

        if ($ok == 1) {

            $oss = \Yii::$app->params['oss'];

            $arrBody = [];

            parse_str(urldecode($body), $arrBody);

            $ossUrl $oss['domain'] . '/' $arrBody['filename'];

            \Yii::info(print_r([$arrBody$body], true), 'OSS超直传回调');

            // 入库

            ImageType::insertData(['info_type' => 0, 'info_id' => 0, 'key' => $arrBody['filename'], 'oss_path' => $ossUrl]);

            return $this->success(['url' => $ossUrl]);

        else {

//            header("http/1.1 403 Forbidden");

            return $this->error([], '验证签名失败');

        }

    }

官方例子:

upload.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

function send_request()

{

    var xmlhttp = null;

    if (window.XMLHttpRequest)

    {

        xmlhttp=new XMLHttpRequest();

    }

    else if (window.ActiveXObject)

    {

        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    }

    if (xmlhttp!=null)

    {

        // serverUrl是 用户获取 '签名和Policy' 等信息的应用服务器的URL,请将下面的IP和Port配置为您自己的真实信息。

        // serverUrl = 'http://88.88.88.88:8888/aliyun-oss-appserver-php/php/get.php'

        serverUrl = '页面不存在_百度搜索'

        xmlhttp.open( "GET", serverUrl, false );

        xmlhttp.send( null );

        return xmlhttp.responseText

    }

    else

    {

        alert("Your browser does not support XMLHTTP.");

    }

};

  • 22
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值