当前位置:网站首页>PHP uses Alibaba cloud storage
PHP uses Alibaba cloud storage
2022-07-07 23:21:00 【Code is a bug】
Alibaba cloud object storage website
Alibaba cloud object storage website
Install alicloud sdk
Use composer Install or use other methods according to the official website
composer require aliyuncs/oss-sdk-php
Upload files
<?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)
{
// 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 = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// 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 = "yourEndpoint";
// Fill in Bucket name , for example examplebucket.
$bucket= "examplebucket";
// Fill in Object The full path , for example exampledir/exampleobject.txt.Object The full path cannot contain Bucket name .
$object = "uploads/".date('Y',time()).date('m',time()).date('d',time())."/".$file['name']; // Storage path of files in Alibaba cloud
// <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 =$file['tmp_name']; // File path
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; // Upload successful , Return file path
}
}
<?php
namespace app\app\controller;
use app\app\model\AppLandpoint;
use app\common\controller\Api;
use app\common\controller\OssUpload;
class LandPoint
{
/* * @description Photo upload */
public function ImgUpload() {
$file=$_FILES['img'];
$upload=new OssUpload();
$data=$upload->upload($file);
return json(['code','data'=>$data]);
}
}
边栏推荐
- Conversion between commonsmultipartfile and file
- Brush question 6
- 网络安全-联合查询注入
- Introduction to redis and jedis and redis things
- FPGA基础篇目录
- Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
- 二叉树(Binary Tree)
- Install Fedora under RedHat
- Brush question 5
- 网络安全-sqlmap与DVWA爆破
猜你喜欢

re1攻防世界逆向
![[microservices SCG] gateway integration Sentinel](/img/f3/410d7228b4b253ebf41015a785099f.png)
[microservices SCG] gateway integration Sentinel

Brush question 4

USB(十五)2022-04-14

Oracle-数据库的备份与恢复

聊聊支付流程的设计与实现逻辑

Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid

Specific method example of V20 frequency converter manual automatic switching (local remote switching)

ROS2专题(03):ROS1和ROS2的区别【02】

【微服务|SCG】gateway整合sentinel
随机推荐
Dynamics 365 查找字段过滤
leetcode-520. Detect capital letters -js
Install a new version of idea. Double click it to open it
Conversion between commonsmultipartfile and file
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
opencv scalar传入三个参数只能显示黑白灰问题解决
PCI-Express接口的PCB布线规则
oc 可变參数传递
统计电影票房排名前10的电影并存入还有一个文件
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Kubernetes' simplified data storage storageclass (creation, deletion and initial use)
海内外技术人们“看”音视频技术的未来
Cloud native is devouring everything. How should developers deal with it?
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
Ros2 topic (03): the difference between ros1 and ros2 [01]
网络安全-sqlmap与DVWA爆破
云原生数据仓库AnalyticDB MySQL版用户手册
Two kinds of curves in embedded audio development