当前位置:网站首页>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]);
}
}
边栏推荐
- 云原生数据仓库AnalyticDB MySQL版用户手册
- FPGA基础篇目录
- PMP项目管理考试过关口诀-1
- leetcode-520. Detect capital letters -js
- LeeCode -- 6. Z 字形变换
- Network security - joint query injection
- opencv scalar传入三个参数只能显示黑白灰问题解决
- 网络安全-sqlmap与DVWA爆破
- Install Fedora under RedHat
- Solution: prompt "unsupported video format" when inserting avi format video into the message
猜你喜欢

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

LDO穩壓芯片-內部框圖及選型參數

V20变频器手自动切换(就地远程切换)的具体方法示例

海内外技术人们“看”音视频技术的未来
![[microservices SCG] gateway integration Sentinel](/img/f3/410d7228b4b253ebf41015a785099f.png)
[microservices SCG] gateway integration Sentinel

漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)

Wechat forum exchange applet system graduation design (3) background function
![Ros2 topic (03): the difference between ros1 and ros2 [01]](/img/20/39d47c93400050a7bc8ad7efea51b3.png)
Ros2 topic (03): the difference between ros1 and ros2 [01]

Installing spss25

微信论坛交流小程序系统毕业设计毕设(1)开发概要
随机推荐
ArcGIS:矢量要素相同字段属性融合的两种方法
Mitsubishi PLC SLmP (MC) protocol
Grid
Install a new version of idea. Double click it to open it
re1攻防世界逆向
JMeter-接口自动化测试读取用例,执行并结果回写
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
PMP项目管理考试过关口诀-1
Bea-3xxxxx error code
Byte hexadecimal binary understanding
Add data analysis tools in Excel
云原生正在吞噬一切,开发者该如何应对?
定位到最底部[通俗易懂]
iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
Brush question 6
Technology at home and abroad people "see" the future of audio and video technology
About idea cannot find or load the main class
统计电影票房排名前10的电影并存入还有一个文件
LDO voltage stabilizing chip - internal block diagram and selection parameters
Network security - joint query injection