当前位置:网站首页>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]);
}
}
边栏推荐
猜你喜欢
Are the microorganisms in the intestines the same as those on the skin?
Wechat forum exchange applet system graduation design (5) assignment
Cases of agile innovation and transformation of consumer goods enterprises
【编译原理】词法分析设计实现
STL标准模板库(Standard Template Library)一周学习总结
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template
How to operate DTC community?
Personal statement of testers from Shuangfei large factory: is education important for testers?
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
十四、数据库的导出和导入的两种方法
随机推荐
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
oc 可变參数传递
Talk about DART's null safety feature
【刷题记录】3. 无重复字符的最长子串
USB (十七)2022-04-15
Mitsubishi PLC SLmP (MC) protocol
Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
Wechat forum exchange applet system graduation design (5) assignment
海内外技术人们“看”音视频技术的未来
Advantages and disadvantages of rest ful API
Interview questions: how to test app performance?
What are the similarities and differences between smart communities and smart cities
V20变频器手自动切换(就地远程切换)的具体方法示例
微信论坛交流小程序系统毕业设计毕设(3)后台功能
网络安全-永恒之蓝
Binary tree
iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
CXF call reports an error. Could not find conduct initiator for address:
2021-01-12
Statistical method for anomaly detection