Browse Source

oss上传

zac3533 1 year ago
parent
commit
9a57f37287
3 changed files with 74 additions and 4 deletions
  1. 18 3
      application/admin/controller/Ajax.php
  2. 54 0
      application/common/library/Oss.php
  3. 2 1
      composer.json

+ 18 - 3
application/admin/controller/Ajax.php

@@ -13,6 +13,9 @@ use think\Db;
 use think\Lang;
 use think\Response;
 use think\Validate;
+use think\Env;
+use OSS\OssClient;
+
 
 /**
  * Ajax异步请求接口
@@ -73,6 +76,9 @@ class Ajax extends Backend
             $chunkcount = $this->request->post("chunkcount/d");
             $filename = $this->request->post("filename");
             $method = $this->request->method(true);
+
+         
+
             if ($action == 'merge') {
                 $attachment = null;
                 //合并分片文件
@@ -109,13 +115,22 @@ class Ajax extends Backend
             //默认普通上传文件
             $file = $this->request->file('file');
             try {
-                $upload = new Upload($file);
-                $attachment = $upload->upload();
+
+                // $upload = new Upload($file);
+                // $attachment = $upload->upload();
+           
+                $filename = $file->getInfo();
+                //获取oss实例
+                $ossClient = new OssClient(Env::get('oss.key_id'), Env::get('oss.key_secret'), Env::get('oss.endpoint'));
+                $bucket    = Env::get('oss.dbucket'); 
+                $uploadpath = "tou-biao/" . date("Ymd") . "/" . $filename['name'];
+                $rs = $ossClient->uploadFile($bucket, $uploadpath, $file->getRealPath());
+  
             } catch (UploadException $e) {
                 $this->error($e->getMessage());
             }
 
-            $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
+            $this->success(__('Uploaded successful'), '', ['url' => $rs['info']['url'], 'fullurl' => $rs['info']['url']]);
         }
     }
 

+ 54 - 0
application/common/library/Oss.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace app\common\library;
+
+use Psr\Log\AbstractLogger;
+use think\Hook;
+
+/**
+ * Oss文件上传
+ */
+class Oss extends AbstractLogger
+{
+
+       /**
+     * phpmailer对象
+     */
+    protected $mail = null;
+
+    /**
+     * 错误内容
+     */
+    protected $error = '';
+
+
+
+    /**
+     * 构造函数
+     * @param array $options
+     */
+    public function __construct($options = [])
+    {
+ 
+
+        //设置发件人
+        $this->from($this->options['mail_from'], $this->options['mail_smtp_user']);
+    }
+
+
+    /**
+     * Logs with an arbitrary level.
+     *
+     * @param mixed  $level
+     * @param string $message
+     * @param array  $context
+     *
+     * @return void
+     *
+     * @throws \Psr\Log\InvalidArgumentException
+     */
+    public function Init($level, $message, array $context = array())
+    {
+        \think\Log::write($message);
+    }
+}

+ 2 - 1
composer.json

@@ -30,7 +30,8 @@
         "ext-curl": "*",
         "ext-pdo": "*",
         "ext-bcmath": "*",
-        "txthinking/mailer": "^2.0"
+        "txthinking/mailer": "^2.0",
+        "aliyuncs/oss-sdk-php": "^2.7"
     },
     "config": {
         "preferred-install": "dist",