一、下载安装
composer require jacobcyl/ali-oss-storage
二、注册服务提供者
在config/app.php的providers下添加:
//阿里云OSS对象存储提供者Jacobcyl\AliOSS\AliOssServiceProvider::class,
三、配置文件系统
在app/filesystems.php中的disks里下添加:
'disks' => [ //... 'oss' => [ 'driver' => 'oss', 'access_id' => '',//Your Aliyun OSS AccessKeyId 'access_key' => '',//Your Aliyun OSS AccessKeySecret 'bucket' => '',//OSS bucket name 'endpoint' => 'oss-cn-shenzhen.aliyuncs.com', //OSS 外网节点或自定义外部域名 //'endpoint_internal' => '', // v2.0.4 新增配置属性,如果为空,则默认使用 endpoint 配置(由于内网上传有点小问题未解决,请大家暂时不要使用内网节点上传,正在与阿里技术沟通中) //'cdnDomain' => '', // 如果isCName为true, getUrl会判断cdnDomain是否设定来决定返回的url,如果cdnDomain未设置,则使用endpoint来生成url,否则使用cdn 'ssl' => false, // true to use 'https://' and false to use 'http://'. default is false, 'isCName' => false, // 是否使用自定义域名,true: 则Storage.url()会使用自定义的cdn或域名生成文件url, false: 则使用外部节点生成url 'debug' => true, ], //... ],
四、基础用法
Storage::disk('oss'); // 如果默认文件系统驱动程序是oss,则可以跳过此步骤//获取指定存储桶的所有文件(请参阅upond配置)Storage::files($directory);Storage::allFiles($directory);Storage::put('path/to/file/file.jpg', $contents); //第一个参数是目标文件路径,第二个参数是文件内容Storage::putFile('path/to/file/file.jpg', 'local/path/to/local_file.jpg'); //从本地路径上传文件Storage::get('path/to/file/file.jpg'); // 通过路径获取文件对象Storage::exists('path/to/file/file.jpg'); // 确定存储(OSS)上是否存在给定文件Storage::size('path/to/file/file.jpg'); // 获取文件大小(字节)Storage::lastModified('path/to/file/file.jpg'); // 获取最后修改日期Storage::directories($directory); // 获取给定目录中的所有目录Storage::allDirectories($directory); // 获取给定目录中的所有(递归)目录Storage::copy('old/file1.jpg', 'new/file1.jpg');//拷贝文件Storage::move('old/file1.jpg', 'new/file1.jpg');//移动文件Storage::rename('path/to/file1.jpg', 'path/to/file2.jpg');//重命名文件Storage::prepend('file.log', 'Prepended Text'); // 在文件前面追加内容Storage::append('file.log', 'Appended Text'); // 在文件后面追加内容Storage::delete('file.jpg');//删除文件Storage::delete(['file1.jpg', 'file2.jpg']);//删除多个文件Storage::makeDirectory($directory); // 创建一个目录Storage::deleteDirectory($directory); //递归删除目录。它将删除给定目录中的所有文件,因此请谨慎使用。// 升级日志// 适用于v2.0版本的新插件Storage::putRemoteFile('target/path/to/file/jacob.jpg', 'http://example.com/jacob.jpg'); //通过远程URL将远程文件上传到存储// v2.0.1版本的新功能Storage::url('path/to/img.jpg'); // 获取文件网址
六、参考文档
packagist地址:
阿里云OSS官方文档: