阿里雲OSS對象存儲服務

基礎步驟參照這個流程:一看就懂:阿里雲oss


TP5 使用 阿里雲oss

官方文檔

安裝SDK

安裝方式有好幾種,這裏使用composer的方式安裝

在tp5項目根目錄下執行:composer require aliyuncs/oss-sdk-php

安裝完成後,可以在

中的 require 看到 

這樣就安裝好了。


以下是使用步驟:(測試圖片上傳)

簡單的寫了一個 test.html 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post"  action="/upload" enctype="multipart/form-data">
        圖片:<input type="file" name="file">
        <button type="submit">提交</button>
    </form>
</body>
</html>

根據文檔:快速入門

直接複製過來改一下

<?php
namespace app\index\controller;

use think\Controller;

use OSS\OssClient;
use OSS\Core\OssException;

class Index extends Controller
{
    public function test()
    {
        return $this->fetch();
    }

    public function upload(){
        if (is_file(__DIR__ . '/../autoload.php')) {
            require_once __DIR__ . '/../autoload.php';
        }
        if (is_file(__DIR__ . '/../vendor/autoload.php')) {
            require_once __DIR__ . '/../vendor/autoload.php';
        }

        $accessKeyId     = '自己的ID';
        $accessKeySecret = '自己的祕鑰';
        $endpoint        = '找到endpoint複製過來即可';
        $bucket          = '自己設置的bucket名';

        $object  = $_FILES['file']['name'];  // 圖片路徑 xxxx.jpg
        $content = 'Hi, OSS.';

        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $res = $ossClient->putObject($bucket, $object, $content);
            // dump($res);
        } catch (OssException $e) {
            print $e->getMessage();
        }
    }
}

 

點擊上傳圖片即可,回到阿里雲

可以查看到

就ok了


後續問題:

這是他返回的鏈接

我們直接複製訪問

拒絕訪問。

回到阿里雲服務器中,點擊詳情

複製他的url,在瀏覽器中訪問

結果:會直接讓你下載

目前遇到的問題就是,圖片是上傳上去了,但是用的時候不知道怎麼用。。。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章