当前位置:网站首页>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]);
}
}
边栏推荐
- 网络安全-sqlmap与DVWA爆破
- USB(十五)2022-04-14
- I wish you all the best and the year of the tiger
- Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
- iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
- 【微服务|SCG】gateway整合sentinel
- PCL . VTK files and Mutual conversion of PCD
- 30讲 线性代数 第五讲 特征值与特征向量
- Introduction to redis and jedis and redis things
- 智慧社區和智慧城市之間有什麼异同
猜你喜欢
七月第一周
iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
Personal statement of testers from Shuangfei large factory: is education important for testers?
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
海内外技术人们“看”音视频技术的未来
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Inftnews | the wide application of NFT technology and its existing problems
随机推荐
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
七月第一周
Inftnews | web5 vs Web3: the future is a process, not a destination
Byte hexadecimal binary understanding
2021-01-12
嵌入式音频开发中的两种曲线
FPGA基础篇目录
Network security - joint query injection
Inftnews | the wide application of NFT technology and its existing problems
UE4_UE5结合罗技手柄(F710)使用记录
2021-01-11
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
Network security - information query of operating system
Wechat forum exchange applet system graduation design completion (6) opening defense ppt
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
Understand the session, cookie and token at one time, and the interview questions are all finalized
QT graphicsview graphical view usage summary with flow chart development case prototype
网络安全-CSRF
解决:信息中插入avi格式的视频时,提示“unsupported video format”
Cause analysis and solution of too laggy page of [test interview questions]