当前位置:网站首页>PHP (TP framework) uses Alibaba cloud OSS storage
PHP (TP framework) uses Alibaba cloud OSS storage
2022-07-21 20:54:00 【Amorous feelings】
This is just a demonstration thinkphp5.0 The framework uploads files to Alibaba cloud oss Object store . In fact, the official documents say very clearly , But in practice, we always make mistakes in various details . I want to have a beginner's tutorial from beginning to end .
Official documents :PHP - Object storage OSS - Alibaba cloud
1. Preparatory work
Apply for alicloud account registration , Open alicloud oss I won't talk about object storage . Then enter the object storage console , Create a Bucket. I created Bucket The name is liuqingwushui. Be careful , This nickname will be used later . Then create a folder dream. After creation, it looks like this :
In Ali cloud oss Object Storage Console Alicloud login - Welcome to alicloud , Secure and stable cloud computing service platform , Don't forget to apply for one first accessKeyId and accessKeySecret. We'll use that later . The official suggestion is to use RAM. This is for testing , I'm useless .
After that, there is another Endpoint Regional nodes , Was created by you Bucket According to the region you choose . I choose southwest Chengdu here , therefore Endpoint yes oss-cn-chengdu.aliyuncs.com.
2. Integration upload test
The preparations are complete , Note that the parameters marked in red are all used , Now let's start the upload test . I can use thinkphp5.0( Later referred to as" tp5) frame , No, tp5 Framework can see my other blogs , There is a download address . Alibaba cloud officially provides many sdk Install the initialization method . I choose the simplest and most direct source method .https://github.com/aliyun/aliyun-oss-php-sdk?spm=a2c4g.11186623.0.0.4dae2ceasCmOmJ
github Download the source code , If nothing else , The downloaded directory structure is like this :
Then copy this folder to tp5 Of vendor Next , Naming aliyun:
Let's start using :
Imgupload.php Code :
<?php
namespace app\aliyunoss\controller;
use think\Controller;
use think\Db; // Database connection class Db
require_once '/../vendor/aliyun/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
class Imgupload extends Controller{
public function checkToken(){
// Alicloud account AccessKey Have all the API Access rights of , The risk is high . It is highly recommended that you create and use it RAM The user carries out API Visit or daily operations , Please log in RAM Console creation RAM user .
$accessKeyId = " Your own accessKeyId";
$accessKeySecret = " Your own accessKeySecret";
// yourEndpoint Fill in Bucket The region corresponding to Endpoint. East China 1( Hangzhou ) For example ,Endpoint Fill in for https://oss-cn-hangzhou.aliyuncs.com.
$endpoint = "oss-cn-chengdu.aliyuncs.com";
// Fill in Bucket name , for example examplebucket.
$bucket= "liuqingwushui";
// Fill in Object The full path , for example exampledir/exampleobject.txt.Object The full path cannot contain Bucket name .
$object = "dream/exampleobject.txt";
// <yourLocalFile> By the local file path plus file name including suffixes , for example /users/local/myfile.txt.
// Fill in the full path of the local file , for example D:\\localpath\\examplefile.txt. If no local path is specified , By default, the file is uploaded from the local path corresponding to the project to which the sample program belongs .
$filePath = "D:\\examplefile.txt";
try{
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->uploadFile($bucket, $object, $filePath);
} catch(OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . "OK" . "\n");
}
}
Pay attention in advance D Disk create a examplefile.txt file .
Then I visit here :http://localhost/tp5/public/aliyunoss/Imgupload/checkToken
Just succeed , Fuck you again dream You will see one in the directory exampleobject.txt The file .
It's not easy to code words , Reprint please indicate the source , thank you
边栏推荐
- PHP date() 函数
- ECSHOP vulnerability recurrence
- 鼠标禁用样式(cursor: not-allowed)无效和鼠标禁用事件(pointer-events: none)冲突
- Nvm、Nrm使用教程
- 使用nodemon工具使Nodejs服务器自动重启
- The C language header stdio cannot be found after Xcode upgrade H solution
- Help the great God
- Redis与memcached有何不同
- Vulnhub-dc-4 target penetration record
- Large file download solution
猜你喜欢
从去IOE到CIPU,中国云计算要走出自己的路径
墨者学院-WebShell文件上传分析(第3-5题)
24. [judge whether it is an integer with bytes]
DVWA [SQL injection] error injection learning record
DVWA[SQL-Injection]报错注入学习记录
【极客大挑战 2019】Easy,Love,Baby-SQL
Koa2 fast build server
vulnhub-DC-4靶机渗透记录
The C language header stdio cannot be found after Xcode upgrade H solution
TP5对接免签FM支付接口
随机推荐
Thinkphp6 uses easywechat5 Development of official account of X (I)
PHP基础语法
wap绿色传奇搭建(纯净版)
Verify with JWT under thinkphp5.1
PHP environment construction (recommended pagoda panel)
OpenFoam中的VOF相变方程
Custom get request parameter class get post
Nodejs uses the post method to receive JS objects and write JSON files
线性薛定谔方程实现界面推移
PHP介绍
Téléchargement de fichiers buctf n1book [chapitre 2 Web Advanced]
ecshop漏洞复现
网页服务器/客户端搭建(nodejs启动exe程序)
Construction de l'environnement PHP (panneau de pagode recommandé)
第二届网刃杯web题复现
express+ejs+swagger-ui-dist 打造及时更新的rest api 在线接口文档
thinkphp6使用EasyWeChat5.x之公众号开发(一)
npm相关资料
First Working Day~
Porous media in openfoam