Home  >  Article  >  php教程  >  Laravel中使用阿里云OSS Composer包分享

Laravel中使用阿里云OSS Composer包分享

WBOY
WBOYOriginal
2016-06-06 20:10:41789browse

这篇文章主要介绍了Laravel中使用阿里云OSS Composer包分享,本文讲解其使用方法,文件下载请前往文件中的github地址,需要的朋友可以参考下

阿里云提供了基于命名空间的 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 哦!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn