laravel 5.5 管理 oss

背景

网上很多文章都是写的 laravel 上传文件到 oss,如果管理已经上传到文件,没有对应的例子(可能也不需要吧)

配置

在app/filesystems.php中的disks里下添加:

php 复制代码
'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', //<the endpoint of OSS, E.g: oss-cn-hangzhou.aliyuncs.com | custom domain, E.g:img.abc.com> OSS 外网节点或自定义外部域名
        //'endpoint_internal' => '', //<internal endpoint [OSS内网节点] 如:oss-cn-shenzhen-internal.aliyuncs.com> v2.0.4 新增配置属性,如果为空,则默认使用 endpoint 配置(由于内网上传有点小问题未解决,请大家暂时不要使用内网节点上传,正在与阿里技术沟通中)
        //'cdnDomain'  => '', //<CDN domain, cdn域名> 如果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,
    ],
],

依赖

  • "iidestiny/laravel-filesystem-oss": "^1.1",

修改第三方

  • vendor/iidestiny/flysystem-oss/src/OssAdapter.php
php 复制代码
/**
 * File list core method.
 *
 * @param string $dirname
 * @param bool   $recursive
 *
 * @return array
 *
 * @throws OssException
 */
public function listAll($dirname = '', $nextMarker = '', $maxkeys = 10)
{
    $result = [
        'objects' => [],
        'prefix' => [],
        'next' => '',
    ];
    $options = [
        'delimiter' => '/',
        'prefix' => $dirname,
        'max-keys' => $maxkeys,
        'marker' => $nextMarker,
    ];
    try {
        $listObjectInfo = $this->client->listObjects($this->bucket, $options);
    } catch (OssException $exception) {
        throw $exception;
    }
    $nextMarker = $listObjectInfo->getNextMarker();
    $objectList = $listObjectInfo->getObjectList();
    $prefixList = $listObjectInfo->getPrefixList();
    if (!empty($objectList)) {
        foreach ($objectList as $objectInfo) {
            $object['Prefix'] = $dirname;
            $object['Key'] = $objectInfo->getKey();
            $object['LastModified'] = $objectInfo->getLastModified();
            $object['eTag'] = $objectInfo->getETag();
            $object['Type'] = $objectInfo->getType();
            $object['Size'] = $objectInfo->getSize();
            $object['StorageClass'] = $objectInfo->getStorageClass();
            $result['objects'][] = $object;
        }
    } else {
        $result['objects'] = [];
    }
    if (!empty($prefixList)) {
        foreach ($prefixList as $prefixInfo) {
            $result['prefix'][] = $prefixInfo->getPrefix();
        }
    }
    $result['next'] = $nextMarker;
    return $result;
}

使用

php 复制代码
/**
 * 文件列表
 * https://help.aliyun.com/zh/oss/developer-reference/list-objects-6
 * http://www.idcbaby.com/43096/
 */
public function ossList(Request $request) {
    $disk = Storage::disk('oss');
    return $disk->getAdapter()->listAll($request->prefix ?: '', $request->next ?: '', $request->max ?: 10);
}
相关推荐
BingoGo17 小时前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack17 小时前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack2 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo2 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
BingoGo3 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·laravel
JaguarJack3 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
QQ5110082853 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe3 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5
longxiangam3 天前
Composer 私有仓库搭建
php·composer