当前位置:网站首页>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?
- 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
- Network security -beef
- What are the similarities and differences between smart communities and smart cities
- Install a new version of idea. Double click it to open it
- oc 可变參数传递
- Brush question 6
- 2021-01-12
- Txt file virus
- 网络安全-burpsuit
猜你喜欢
Transform XL translation
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
Software test classification
Introduction to redis and jedis and redis things
The wonderful relationship between message queue and express cabinet
Personal statement of testers from Shuangfei large factory: is education important for testers?
Microbial Health Network, How to restore Microbial Communities
Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
Innovation today | five key elements for enterprises to promote innovation
随机推荐
Txt file virus
USB(十六)2022-04-28
Installing vmtools is gray
UE4_UE5蓝图command节点的使用(开启关闭屏幕响应-log-发布全屏显示)
聊聊支付流程的设计与实现逻辑
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
Inftnews | web5 vs Web3: the future is a process, not a destination
Why does the market need low code?
【编译原理】词法分析设计实现
Grid
每日一题——PAT乙级1002题
Talk about DART's null safety feature
14、 Two methods of database export and import
【刷题记录】3. 无重复字符的最长子串
Brush question 5
PMP项目管理考试过关口诀-1
Network security - phishing
Unity dynamically merges mesh textures
[untitled] reprint melting ice - track icedid server with a few simple steps
JMeter-接口自动化测试读取用例,执行并结果回写