PHP code example of wtto / ali-oss-storage
1. Go to this page and download the library: Download wtto/ali-oss-storage library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wtto / ali-oss-storage example snippets
Wtto\AliOSS\AliOssServiceProvider::class,
'disks'=>[
...
'oss' => [
'driver' => 'oss',
'access_id' => env('ALIOSS_KEYID', null), //Your Aliyun OSS AccessKeyId
'access_key' => env('ALIOSS_KEYSECRET', null), //Your Aliyun OSS AccessKeySecret
'bucket' => env('ALIOSS_BUCKETNAME', null), //OSS bucket name
'endpoint' => env('ALIOSS_ENDPOINT', null), //<the endpoint of OSS, E.g: oss-cn-hangzhou.aliyuncs.com | custom domain, E.g:img.abc.com> OSS 外网节点或自定义外部域名
'endpoint_internal' => env('ALIOSS_ENDPOINT_INTERNAL', null), //<internal endpoint [OSS内网节点] 如:oss-cn-shenzhen-internal.aliyuncs.com> v2.0.4 新增配置属性,如果为空,则默认使用 endpoint 配置(由于内网上传有点小问题未解决,请大家暂时不要使用内网节点上传,正在与阿里技术沟通中)
'cdnDomain' => env('ALIOSS_DOMAIN', null), //<CDN domain, cdn域名> 如果isCName为true, getUrl会判断cdnDomain是否设定来决定返回的url,如果cdnDomain未设置,则使用endpoint来生成url,否则使用cdn
'ssl' => env('ALIOSS_SSL', false), // true to use 'https://' and false to use 'http://'. default is false,
'isCName' => env('ALIOSS_CNAME', false), // 是否使用自定义域名,true: 则Storage.url()会使用自定义的cdn或域名生成文件url, false: 则使用外部节点生成url
'debug' => env('ALIOSS_DEBUG', true),
],
...
]
FILESYSTEM_DRIVER=oss
ALIOSS_KEYID=<Your Aliyun OSS AccessKeyId>
ALIOSS_KEYSECRET=<Your Aliyun OSS AccessKeySecret>
ALIOSS_BUCKETNAME=<OSS bucket name>
ALIOSS_ENDPOINT=<the endpoint of OSS, E.g: oss-cn-hangzhou.aliyuncs.com | custom domain, E.g:img.abc.com>
ALIOSS_ENDPOINT_INTERNAL=<internal endpoint [OSS内网节点] 如:oss-cn-shenzhen-internal.aliyuncs.com>
ALIOSS_DOMAIN=<CDN domain, cdn域名>
ALIOSS_SSL=<true|false>
ALIOSS_CNAME=<true|false>
ALIOSS_DEBUG=<true|false>
use Illuminate\Support\Facades\Storage;
// 如果你设置的默认文件驱动时oss的话,这一步可以跳过
Storage::disk('oss');
// 获取目录下所有的文件
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');
// 获得指定对象的大小
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');
// v2.0.1 添加插件:
// 获得指定文件的网址
Storage::url('path/to/img.jpg'); // get the file url
// v2.1.1 添加插件:
// 获得私有 Bucket 的指定文件的临时公开网址 默认临时公开失效是 3600 秒
Storage::signUrl('path/to/img.jpg',$timeout);
// v2.3.0 添加插件:
// 获得指定位置的所有对象 包括目录以及文件 文件会放回相关属性
Storage::objects($directory);
// 递归获得指定位置的所有对象 包括目录以及文件 文件会放回相关属性
Storage::allObjects($directory);
// v2.3.1 添加插件:
// 通过url获得文件所在位置
Storage::url2path($url);
// 复制目录
Storage::copyDirectory('path/from/','path/to/');
// 移动目录
Storage::moveDirectory('path/from/','path/to/');
// 重命名目录
Storage::renameDirectory('path/from/','path/to/');
// 分页查询指定位置的对象 并返回下一页的标记
Storage::objects($directory, $page_size = null, $next_marker = '');