主页 > 编程资料 > PHP >
发布时间:2015-12-29 作者:网络 阅读:225次

阿里云提供了基于命名空间的 V2 版 SDK,但是文档不是很完整,使用门槛比较高,于是我封装了一个 Composer 包:https://github.com/johnlui/AliyunOSS

安装

将以下内容增加到 composer.json:


require: {

    "johnlui/aliyun-oss": "dev-master"

}

然后运行 composer update

使用


use JohnLui\AliyunOSS\AliyunOSS;


// 构建 OSSClient 对象
// 三个参数:服务器地址、阿里云提供的AccessKeyId、AccessKeySecret
$oss = AliyunOSS::boot('http://oss-cn-qingdao.aliyuncs.com',  $AccessKeyId, $AccessKeySecret);

// 设置 Bucket
$oss = $oss->setBucket($bucketName);

// 上传一个文件(示例文件为 public 目录下的 robots.txt)
// 两个参数:资源名称、文件路径
$oss->uploadFile('robots.txt', public_path('robots.txt'));

// 从服务器获取这个资源的 URL 并打印
// 两个参数:资源名称、过期时间
echo $oss->getUrl('robots.txt', new DateTime("+1 day"));

就是这么简单,喜欢可以去 Github 上 Star 哦!

关键字词: