Laravel 7.x 整合阿里云oss储存
1、什么是oss
阿里云对象存储服务( Object Storage Service,简称 OSS)为您提供基于网络的数据存取服务。使用OSS,您可以通过网络随时存储和调用包括文本、图片、音频和视频等在内的各种数据文件。.
阿里云OSS将数据文件以对象(Object)的形式上传到存储空间(Bucket)中。您可以进行以下操作:
创建一个或者多个存储空间,向每个存储空间中添加一个或多个文件。
通过获取已上传文件的地址进行文件的分享和下载。
通过修改存储空间或文件的读写权限(ACL)来设置访问权限。
通过阿里云管理控制台、各种便捷工具以及丰富的SDK包执行基本和高级OSS操作。
它支持现在市面上的java、python、go、c++、PHP、c、.net、Node.js、browser.js、android、ios、rubu几乎覆盖的非常全面了。
2、阿里云OSS-SDK简单使用方法
我的PHP版本为 : 7.3.10 laravel版本:7.x
1.先通过composer require aliyuncs/oss-sdk-php
安装阿里云oss的SDK.
2.创建储存空间
use OSS\OssClient;
use OSS\Core\OssException;
// 阿里云主账号AccessKey。
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 存储空间名称
$bucket = "<yourBucketName>";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->createBucket($bucket);
} catch (OssException $e) {
print $e->getMessage();
}
3.上传字文件
(1) 字符串文件
use OSS\OssClient;
use OSS\Core\OssException;
// 阿里云主账号AccessKey
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 存储空间名称
$bucket= " <yourBucketName>";
// 文件名称
$object = " <yourObjectName>";
//文件内容
$content = "Hi, OSS.";
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
print $e->getMessage();
}
(2)图片等其它类型文件
use OSS\OssClient;
use OSS\Core\OssException;
// 阿里云主账号AccessKey
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 设置存储空间名称。
$bucket= "<yourBucketName>";
// 设置文件名称。
$object = "<yourObjectName>";
// <yourLocalFile>由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
$filePath = "<yourLocalFile>";
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");
上传成功的图
3、laravel 整合阿里云oss
由于每次使用oss的时候都需要创建一个新的示例$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
很麻烦,可能把我们累死,这个时候我们勤劳的大脑就想出一个不让我们累死的方法,那就是那这个新建上面的操作分装成一个工具类,哎这个时候就只需要去调用这个工具类就可以了岂不是美哉。
<?php
/**
* Notes:
* User: Mr.w
* DateTime: 2020/10/10 12:12
*/
namespace App\Utils;
use App\Models\File;
use App\Uitls\EncryptionKohana;
use OSS\Core\OssException;
use OSS\OssClient;
class OssUtil
{
protected $accessKeyId;
protected $accessKeySecret;
protected $endpoint;
protected $bucket;
protected $file_name;
public function __construct()
{
$this->accessKeyId = config('app.wx.sms_access_key_id');
$this->accessKeySecret = config('app.wx.sms_access_key_secret');
$this->endpoint=config('app.wx.oss_endpoint');
// 存储空间名称
$this->bucket= config('app.wx.oss_bucket');
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
$this->file_name= "probject-files/";
}
/**
* Notes:删除文件
* User: Mr.w
* DateTime: 2020/10/10 12:13
* @param $file_name
*/
public function delFile($file_name)
{
try{
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
$ossClient->deleteObject($this->bucket, $file_name);
} catch(OssException $e) {
info("文件删除错误信息 -> ".$e->getMessage());
return;
}
}
/**
* Notes:添加文件
* User: Mr.w
* DateTime: 2020/10/10 12:13
* @param $user
* @param $file
* @param null $proces_id
* @param null $project_id
*/
public function addFile($user,$file,$proces_id=null,$project_id=null)
{
$object = $this->file_name.$user->username."-".EncryptionKohana::random_string(10). "." . $file->getClientOriginalExtension();
info("文件名".$object);
$filePath = $file;
try {
$oss = new OssClient($this->accessKeyId, $this->accessKeySecret,$this->endpoint);
$oss->uploadFile($this->bucket, $object, $filePath);
} catch (OssException $e) {
info("文件添加错误信息 -> ".$e->getMessage());
}
return $object;
}
}
这个时候有人问在
$this->accessKeyId = config('app.wx.sms_access_key_id');
$this->accessKeySecret = config('app.wx.sms_access_key_secret');
$this->endpoint=config('app.wx.oss_endpoint');
// 存储空间名称
$this->bucket= config('app.wx.oss_bucket');
这个是啥,我来告诉你们 这个是通过配置文件读取调取阿里云osssdk所需的参数做法如下图在env里面进行配置
然后在在config/app.php
里面配置一下这样可以提高配置参数的读取效率,但是每次更改.env
文件的时候需要执行php artisan config:cache
更新缓存才可以生效。
在代码中的实际使用
public function testFile()
{
$user = User::find($this->user_id);
$oss = new OssUtil();
if (is_array($this->files)) {
//多个文件
foreach ($this->files as $file) {
//上传文件
$file_name = $oss->addFile($user, $file, $this->proces_id);
$file_model = new File();
$file_model->path = $file_name;
$file_model->model_id = $this->proces_id == null ? $this->project_id : $this->proces_id;
//1代表 项目文件 2代表流程文件
$file_model->model_type = $this->proces_id == null ? File::PROJECT : File::PROCES;
//默认任何文件
$file_model->type = 0;
$file_model->save();
}
//记录保存至数据库
info("多个文件上传成功!");
return;
}
//单个文件
//上传文件
$file_name = $oss->addFile($user, $this->files, $this->proces_id);
$file_model = new File();
$file_model->path = $file_name;
$file_model->model_id = $this->proces_id == null ? $this->project_id : $this->proces_id;
//1代表 项目文件 2代表流程文件
$file_model->model_type = $this->proces_id == null ? File::PROJECT : File::PROCES;
//默认任何文件
$file_model->type = 0;
$file_model->save();
info("单个文件上传成功!");
}
好了到这里阿里云的oss的简单使用就到这里的,上面的代码可以拷贝直接使用。
望各个关注和点赞。
有不妥当地方还希望有大佬指出来。🙇