介绍
不用介绍了吧,都知道是干啥用的,话不多少,开搞!!!
开搞
1:使用composer安装扩展包
composer require aliyuncs/oss-sdk-php
2:配置你的OSS,我这里是配置到 .env文件里
OSS_ACCESS_KEY_ID= #你的oss服务的key_id
OSS_ACCESS_KEY_SECRET= #你的oss服务的key_secret
OSS_ENDPOINT= #你的oss服务的endpoint,例如 oss-cn-hangzhou.aliyuncs.com
OSS_BUCKET= #你的oss服务的数据桶bucket
OSS_ACL=public-read #设置acl权限,我这里设置的为公共读,需要设置其他可以参阅阿里oss的文档
3:编写OSS的服务提供者
①:在app/Providers下新建个文件,例如 OssServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use OSS\OssClient;
class OssServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register()
{
$this->app->singleton('oss', function () {
$accessKeyId = env('OSS_ACCESS_KEY_ID');
$accessKeySecret = env('OSS_ACCESS_KEY_SECRET');
$endpoint = env('OSS_ENDPOINT');
$bucket = env('OSS_BUCKET');
return new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);
});
}
/**
* Bootstrap any application services.
*/
public function boot()
{
//
}
}
②:在 config/app.php 中的providers 数组里加入服务,并在aliases里引入起个别名
'providers' => ServiceProvider::defaultProviders()->merge([
/*
* Package Service Providers...
*/
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
App\Providers\OssServiceProvider::class, #对,就是这个,你没看错,就是它,它,它
])->toArray(),
'aliases' => Facade::defaultAliases()->merge([
// 'Example' => App\Facades\Example::class,
'Redis' => Illuminate\Support\Facades\Redis::class,
'OSS' => OSS\OssClient::class, #对,就是它,给它换个马甲,让别人更好的认识它
])->toArray(),
③:编写一个类,封装一些常用的方法,我这里封装了【上传文件】【下载文件】【检测文件存不存在】三个方法,其余的可以自行添加
<?php
namespace App\Tools;
use OSS\Core\OssException;
use Illuminate\Support\Facades\Log;
use OSS\OssClient;
/**
*
*/
class AliOss
{
public $sBucket = '';
public $sACL = '';
public function __construct(){
$this->sBucket = env('OSS_BUCKET');
$this->sACL = env('OSS_ACL');
}
/**
* filePath 本地的文件绝对地址
* targetPath oss上要保存的地址路径,例如 voice/ceshi/houlai.mp3 voice为bucket名称,测试为bucket下的目录,houlai.mp3为要保存的文件名
*/
public function uploadFile($filePath, $targetPath)
{
try{
$ossClient = app('oss');
$result = $ossClient->uploadFile($this->sBucket,$targetPath,$filePath);
$httpCode = $result['info']['http_code'] ?? 0;
if ($httpCode == 200) {
$ossClient->putObjectAcl($this->sBucket, $targetPath, $this->sACL);//这里是我这边要设置文件权限为公共读,不设置会默认继承当前bucket上的权限,其他权限请参阅阿里云的oss官方文档
return true;
}
return false;
}catch(OssException $e ){
Log::channel('oss')->info('OSS-uploadFile',[$e->getMessage()]);
return false;
}
}
/**
* targetPath oss上文件的地址
*/
public function existsFile($targetPath){
try{
$ossClient = app('oss');
return $ossClient->doesObjectExist($this->sBucket, $targetPath);
}catch(OssException $e ){
Log::channel('oss')->info('OSS-existsFile',[$e->getMessage()]);
return false;
}
}
/**
* targetPath oss上文件的地址
* localPath 要保存到本地的绝对路径
*/
public function downFile($targetPath, $localPath){
try{
$options = [];
$options[OssClient::OSS_FILE_DOWNLOAD] = $localPath;
$ossClient = app('oss');
return $ossClient->doesObjectExist($this->sBucket, $targetPath, $options);
}catch(OssException $e ){
Log::channel('oss')->info('OSS-downFile',[$e->getMessage()]);
return false;
}
}
}
为了方便管理,可以使用提供的oss客户端进行登录访问查看