基础步骤参照这个流程:一看就懂:阿里云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,在浏览器中访问
结果:会直接让你下载
目前遇到的问题就是,图片是上传上去了,但是用的时候不知道怎么用。。。