___ __ _ ____ _____ _____
/ | / / (_) __ __ __ __ ____ / __ \ / ___/ / ___/
/ /| | / / / / / / / / / / / / / __ \ / / / / \__ \ \__ \
/ ___ | / / / / / /_/ / / /_/ / / / / / / /_/ / ___/ / ___/ /
/_/ |_|/_/ /_/ \__, / \__,_/ /_/ /_/ \____/ /____/ /____/
/____/
AliyunOSS 是阿里云 OSS 官方 SDK 的 Composer 封装,支持任何 PHP 项目,包括 Laravel、Symfony、TinyLara 等等。Github 地址:https://github.com/johnlui/AliyunOSS
更新记录
- 2015-01-09 `Release v1.0`
安装
将以下内容增加到 composer.json:
require: {
"johnlui/aliyun-oss": "*"
}
然后运行 `composer update`。
使用(以 Laravel 为例)
构建 Service 文件
新建 `app/services/OSS.php`,内容可参考:OSSExample.php:
<?php namespace App\Services; use JohnLui\AliyunOSS\AliyunOSS; use Config; class OSS { private $ossClient; public function __construct() { $this->ossClient = AliyunOSS::boot( Config::get('app.ossServerInternal'), Config::get('app.AccessKeyId'), Config::get('app.AccessKeySecret') ); } public static function upload($ossKey, $filePath) { $oss = new OSS(); $oss->ossClient->setBucket('yishuodian-test'); $oss->ossClient->uploadFile($ossKey, $filePath); } public static function getUrl($ossKey) { $oss = new OSS(); $oss->ossClient->setBucket('yishuodian-test'); return $oss->ossClient->getUrl($ossKey, new \DateTime("+1 day")); } public static function createBucket($bucketName) { $oss = new OSS(); return $oss->ossClient->createBucket($bucketName); } public static function getAllObjectKey($bucketName) { $oss = new OSS(); return $oss->ossClient->getAllObjectKey($bucketName); } }
###放入自动加载
在 `composer.json` 中 `autoload -> classmap` 处增加配置:
"autoload": {
"classmap": [
"app/services"
]
}
然后运行 `composer dump-autoload`。
增加相关配置
在 app/config/app.php 中增加三项配置:
'ossServerInternal' => '服务器内网地址', //青岛为 http://oss-cn-qingdao-internal.aliyuncs.com 'AccessKeyId' => '阿里云给的AccessKeyId', 'AccessKeySecret' => '阿里云给的AccessKeySecret',
使用
use App\Services\OSS;
OSS::upload('文件名', '本地路径'); // 上传一个文件
echo OSS::getUrl('某个文件的名称'); // 打印出某个文件的外网链接
OSS::createBucket('一个字符串'); // 新增一个 Bucket。注意,Bucket 名称具有全局唯一性,也就是说跟其他人的 Bucket 名称也不能相同。
OSS::getAllObjectKey('某个 Bucket 名称'); // 获取该 Bucket 中所有文件的文件名,返回 Array。