当前位置:网站首页>php 使用阿里云存储
php 使用阿里云存储
2022-07-07 21:50:00 【代码就是bug】
阿里云对象存储网站
安装阿里云sdk
使用composer安装或者根据官网使用其他方法安装
composer require aliyuncs/oss-sdk-php
文件上传
<?php
use OSS\OssClient;
use OSS\Core\OssException;
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
class OssUpload
{
public function upload($file)
{
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
$endpoint = "yourEndpoint";
// 填写Bucket名称,例如examplebucket。
$bucket= "examplebucket";
// 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
$object = "uploads/".date('Y',time()).date('m',time()).date('d',time())."/".$file['name']; // 文件在阿里云中的存储路径
// <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
$filePath =$file['tmp_name']; // 文件路径
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");
return $object; // 上传成功,返回文件路径
}
}
<?php
namespace app\app\controller;
use app\app\model\AppLandpoint;
use app\common\controller\Api;
use app\common\controller\OssUpload;
class LandPoint
{
/* * @description 照片上传*/
public function ImgUpload() {
$file=$_FILES['img'];
$upload=new OssUpload();
$data=$upload->upload($file);
return json(['code','data'=>$data]);
}
}
边栏推荐
- 消费品企业敏捷创新转型案例
- ArcGIS: two methods of attribute fusion of the same field of vector elements
- Unity 动态合并网格纹理
- Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
- 微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
- oc 可变參数传递
- Advantages and disadvantages of rest ful API
- The wonderful relationship between message queue and express cabinet
- Innovation today | five key elements for enterprises to promote innovation
- JS triangle
猜你喜欢

Inftnews | web5 vs Web3: the future is a process, not a destination

【编译原理】词法分析设计实现

Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
![[record of question brushing] 3 Longest substring without duplicate characters](/img/44/1cd8128d93c9c273e0f4718d84936e.png)
[record of question brushing] 3 Longest substring without duplicate characters

DTC社群运营怎么做?

Cases of agile innovation and transformation of consumer goods enterprises

微信论坛交流小程序系统毕业设计毕设(5)任务书

Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night

Line test - graphic reasoning - 1 - Chinese character class

成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
随机推荐
[network] Introduction to C language
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
I wish you all the best and the year of the tiger
Database daily question --- day 22: last login
Knowledge drop - PCB manufacturing process flow
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
ArcGIS: two methods of attribute fusion of the same field of vector elements
Classification and prediction of heartbeat signal
【编译原理】词法分析设计实现
Dynamics 365 查找字段过滤
微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
聊聊 Dart 的空安全 (null safety) 特性
Unity dynamically merges mesh textures
Binary tree
智慧社區和智慧城市之間有什麼异同
网格(Grid)
Why does the market need low code?
Install a new version of idea. Double click it to open it