JohnLui/AliyunOSS v2.0 发布,大幅优化了 API 易用性,改进了文件结构
___ __ _ ____ _____ _____ / | / / (_) __ __ __ __ ____ / __ \ / ___/ / ___/ / /| | / / / / / / / / / / / / / __ \ / / / / \__ \ \__ \ / ___ | / / / / / /_/ / / /_/ / / / / / / /_/ / ___/ / ___/ / /_/ |_|/_/ /_/ \__, / \__,_/ /_/ /_/ \____/ /____/ /____/ /____/
适配 Laravel 7 的新包已经发布:AliyunOSS-Laravel7
AliyunOSS 是阿里云 OSS 官方 SDK 的 Composer 封装,支持任何 PHP 项目,包括 Laravel、Symfony、TinyLara 等等。Github 地址:https://github.com/johnlui/AliyunOSS
更新记录
-
2017-03-08
Release v2.0.0
v2 发布,在 API 易用性上进行了大量优化 -
2016-09-12
Release v1.3.5
加入文件元信息的设置功能 -
2016-07-20
Release v1.3.4
加入文件元信息的获取功能 -
2016-07-20
Release v1.3.4
加入文件元信息的获取功能 -
2016-01-31
Release v1.3.2
获取指定虚拟文件夹下的所有文件 -
2015-10-23
Release v1.3
增加删除、复制、移动文件功能 -
2015-08-07
Release v1.2
修复内存泄露 bug -
2015-01-12
Release v1.1
增加内外网配置分离 -
2015-01-09
Release v1.0
完善功能,增加 Laravel 框架详细使用教程及代码
安装
安装有两种方式:
① 直接编辑配置文件
将以下内容增加到 composer.json:
require: { "johnlui/aliyun-oss": "~2.0" }
然后运行 composer update
。
② 执行命令安装
运行命令:
composer require johnlui/aliyun-oss:~2.0
使用(以 Laravel 为例)
构建 Service 文件
新建 app/services/OSS.php,内容可参考:OSS.php,然后修改配置:
... ... private $city = '青岛'; // 经典网络 or VPC private $networkType = '经典网络'; private $AccessKeyId = ''; private $AccessKeySecret = ''; ... ...
放入自动加载
遵循 psr-0 的项目(如Laravel 4、CodeIgniter、TinyLara)中:
在 composer.json 中 autoload -> classmap 处增加配置:
"autoload": { "classmap": [ "app/services" ] }
然后运行 composer dump-autoload。
遵循 psr-4 的项目(如 Laravel 5、Symfony)中:
无需配置,保证目录 App/Services
和命名空间 namespace App\Services;
一致即可自动加载。
使用
use App\Services\OSS; // 在外网上传一个文件并指定 options 如:Content-Type 类型 // 更多 options 见:https://github.com/johnlui/AliyunOSS/blob/master/src/oss/src/Aliyun/OSS/OSSClient.php#L142-L148 OSS::publicUpload('bucket', '目标 object 名', '本地文件路径', [ 'ContentType' => 'application/pdf', ... ... ]);
更多用法等待着你去发现:https://github.com/johnlui/AliyunOSS/blob/master/example/OSS.php
反馈
有问题请在本文下面留言。
License
除 “版权所有(C)阿里云计算有限公司” 的代码文件外,遵循 MIT license 开源。
评论:
2021-06-01 17:27
[0] ThrowableError in EventDispatcher.php line 48
类型错误: Argument 1 passed to Symfony\Component\EventDispatcher\EventDispatcher::dispatch() must be an object, string given, called in /www/wwwroot/jiaoyou_web/vendor/johnlui/aliyun-oss/src/oss/libs/guzzle/common/Guzzle/Common/AbstractHasDispatcher.php on line 40
{
if (__CLASS__ === static::class) {
$this->optimized = [];
}
}
/**
* {@inheritdoc}
*/
public function dispatch(object $event, string $eventName = null): object
{
$eventName = $eventName ?? \get_class($event);
if (null !== $this->optimized) {
$listeners = $this->optimized[$eventName] ?? (empty($this->listeners[$eventName]) ? [] : $this->optimizeListeners($eventName));
} else {
$listeners = $this->getListeners($eventName);
}
Call Stack
in EventDispatcher.php line 48
at EventDispatcher->dispatch('client.create_reques...', object(Event)) in AbstractHasDispatcher.php line 40
at AbstractHasDispatcher->dispatch('client.create_reques...', ['client' => object(Client), 'request' => object(EntityEnclosingRequest)]) in Client.php line 450
at Client->prepareRequest(object(EntityEnclosingRequest), []) in Client.php line 199
at Client->createRequest('PUT', 'http://test-jiaoyou....', ['Content-Length' => '12227', 'Content-Type' => 'application/octet-st...', 'Date' => 'Tue, 01 Jun 2021 09:...', ...], object(ReadLimitEntityBody)) in HttpServiceClient.php line 119
at HttpServiceClient->buildCoreRequest(object(HttpRequest)) in HttpServiceClient.php line 56
at HttpServiceClient->sendRequest(object(HttpRequest), object(ExecutionContext)) in OpenServiceClient.php line 37
at OpenServiceClient->sendRequest(object(HttpRequest), object(ExecutionContext)) in RetryableServiceClient.php line 32
at RetryableServiceClient->sendRequestImpl(object(HttpRequest), object(ExecutionContext), 0) in RetryableServiceClient.php line 27
at RetryableServiceClient->sendRequest(object(HttpRequest), object(ExecutionContext)) in Command.php line 121
at Command->execute(['Endpoint' => 'http://oss-cn-shenzh...', 'AccessKeyId' => 'LTAIeOopj0aHBw7Y', 'AccessKeySecret' => 'HsdlmsiBVIorEwGTLs1E...'], ['Bucket' => 'test-jiaoyou', 'Key' => 'jiaoyou/jyaaaaaaaaaa...', 'Content' => resource, ...]) in OSSClient.php line 429
at OSSClient->execute('putObject', ['Bucket' => 'test-jiaoyou', 'Key' => 'jiaoyou/jyaaaaaaaaaa...', 'Content' => resource, ...]) in OSSClient.php line 153
at OSSClient->putObject(['Bucket' => 'test-jiaoyou', 'Key' => 'jiaoyou/jyaaaaaaaaaa...', 'Content' => resource, ...]) in AliyunOSS.php line 101
at AliyunOSS->uploadFile('jiaoyou/jyaaaaaaaaaa...', '/www/wwwroot/jiaoyou...', []) in FileLogic.php line 141
at FileLogic::ossUploadFile('/www/wwwroot/jiaoyou...', 'teacher', '', 1, '', []) in FileLogic.php line 96
at FileLogic::uploadLocalFile('/www/wwwroot/jiaoyou...', 'teacher') in Test.php line 99
at Test->upload()
at ReflectionMethod->invokeArgs(object(Test), []) in Container.php line 395
at Container->invokeReflectMethod(object(Test), object(ReflectionMethod), ['s' => '//admin/web.test/upl...']) in Module.php line 131
at Module->think\route\dispatch\{closure}(object(Request), object(Closure), null)
at call_user_func_array(object(Closure), [object(Request), object(Closure), null]) in Middleware.php line 185
at Middleware->think\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Middleware.php line 130
at Middleware->dispatch(object(Request), 'controller') in Module.php line 136
at Module->exec() in Dispatch.php line 168
at Dispatch->run() in App.php line 432
at App->think\{closure}(object(Request), object(Closure), null)
at call_user_func_array(object(Closure), [object(Request), object(Closure), null]) in Middleware.php line 185
at Middleware->think\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Middleware.php line 130
at Middleware->dispatch(object(Request)) in App.php line 435
at App->run() in index.php line 26
2020-08-28 17:23
Fatal error: Cannot declare class Symfony\Component\ClassLoader\UniversalClassLoader, because the name is already in use in /home/wwwroot/app/vendor/johnlui/aliyun-oss-laravel7/src/oss/libs/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php on line 61
怎么避免引入文件冲突
2020-04-07 16:50
laravel版本 -- Laravel Framework 7.2.2
2020-01-07 11:45
2019-09-30 09:43
返回结果 :"http://phpnb.oss-cn-hongkong.aliyuncs.com/89005FCCF6D9D2126A29CCFF9DAEAC52"
请问 大佬 我是上传的图片 怎么能让他直接显示啊?
2019-08-22 17:10
$bucket = '*******‘
我现在的情况是:
我的阿里oss 有一个bucket名为 blog


当我用这个插件把bucket设置为blog时,图片能正常上传到该目录下

但是我想把图片上传到二级目录,也就是blog/picture等

我试图把bucket名定义为 /blog/picture 运行的时候发现行不通

请帮帮忙~!

2018-07-02 20:14
这个是什么原因呀,用AliyunOSS.php里面的getUrl方法都能正常获取到图片下载地址呢,求解答呀
2018-06-01 11:10
Symfony\Component\Debug\Exception\FatalThrowableError
/vendor/johnlui/aliyun-oss/src/AliyunOSS.php86
86行内容是 public static function boot($city, $networkType, $isInternal, $AccessKeyId, $AccessKeySecret)
请问 在app/services/oss.php文件 从您Git上粘贴下来 还有要修改的地方吗?
vendor下的包里 vendor/johnlui/aliyun-oss/src/AliyunOSS.php里还有要 改的地方吗
2021-06-24 15:30