[扩展推荐]阿里云对象存储扩展 —— Laravel最好的Aliyun OSS Storage扩展

Posted 悠悠山雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[扩展推荐]阿里云对象存储扩展 —— Laravel最好的Aliyun OSS Storage扩展相关的知识,希望对你有一定的参考价值。

Aliyun-oss-storage for Laravel

扩展借鉴了一些优秀的代码,综合各方,同时做了更多优化,将会添加更多完善的接口和插件,打造Laravel最好的OSS Storage扩展

项目地址: alphasnow.github.io/aliyun-oss-laravel

运行环境

  • php 7.0+
  • cURL extension
  • Laravel 5.5+

安装方法

  1. 如果您通过composer管理您的项目依赖,可以在你的项目根目录运行:

     $ composer require alphasnow/aliyun-oss-laravel
    

    或者在你的composer.json中声明依赖:

     "require": {
         "alphasnow/aliyun-oss-laravel": "~2.0"
     }
    
  2. 修改环境文件.env

    ALIYUN_OSS_ACCESS_ID=
    ALIYUN_OSS_ACCESS_KEY=
    ALIYUN_OSS_BUCKET=
    ALIYUN_OSS_ENDPOINT=oss-cn-shanghai.aliyuncs.com
    ALIYUN_OSS_IS_CNAME=false
    ALIYUN_OSS_CDN_DOMAIN=
    ALIYUN_OSS_IS_SSL=false
  3. (可选)修改配置文件 config/filesystems.php

    \'default\' => env(\'FILESYSTEM_DRIVER\', \'aliyun\'),
    // ...
    \'disks\'=>[
        // ...
        \'aliyun\' => [
            \'driver\'     => \'aliyun\',
            \'access_id\'  => env(\'ALIYUN_OSS_ACCESS_ID\'),
            \'access_key\' => env(\'ALIYUN_OSS_ACCESS_KEY\'),
            \'bucket\'     => env(\'ALIYUN_OSS_BUCKET\'),
            \'endpoint\'   => env(\'ALIYUN_OSS_ENDPOINT\', \'oss-cn-shanghai.aliyuncs.com\'),
            \'is_cname\'   => env(\'ALIYUN_OSS_IS_CNAME\', false),
            \'cdn_domain\' => env(\'ALIYUN_OSS_CDN_DOMAIN\', \'\'),
            \'is_ssl\'     => env(\'ALIYUN_OSS_IS_SSL\', false),
        ],
        // ...
    ]

快速使用

use IlluminateSupportFacadesStorage;
$storage = Storage::disk(\'aliyun\');

文件写入

Storage::disk(\'aliyun\')->putFile(\'prefix/path\', \'/local/path/file.txt\');
Storage::disk(\'aliyun\')->putFileAs(\'prefix/path\', \'/local/path/file.txt\', \'file.txt\');

Storage::disk(\'aliyun\')->put(\'prefix/path/file.txt\', file_get_contents(\'/local/path/file.txt\'));
$fp = fopen(\'/local/path/file.txt\',\'r\');
Storage::disk(\'aliyun\')->put(\'prefix/path/file.txt\', $fp);
fclose($fp);

Storage::disk(\'aliyun\')->putRemoteFile(\'prefix/path/file.txt\', \'http://example.com/file.txt\');

Storage::disk(\'aliyun\')->prepend(\'prefix/path/file.txt\', \'Prepend Text\'); 
Storage::disk(\'aliyun\')->append(\'prefix/path/file.txt\', \'Append Text\');

文件查询

Storage::disk(\'aliyun\')->url(\'prefix/path/file.txt\');
Storage::disk(\'aliyun\')->temporaryUrl(\'prefix/path/file.txt\', CarbonCarbon::now()->addMinutes(30));

Storage::disk(\'aliyun\')->get(\'prefix/path/file.txt\'); 

Storage::disk(\'aliyun\')->exists(\'prefix/path/file.txt\'); 
Storage::disk(\'aliyun\')->size(\'prefix/path/file.txt\'); 
Storage::disk(\'aliyun\')->lastModified(\'prefix/path/file.txt\');

文件操作

Storage::disk(\'aliyun\')->copy(\'prefix/path/file.txt\', \'prefix/path/file_new.txt\');
Storage::disk(\'aliyun\')->move(\'prefix/path/file.txt\', \'prefix/path/file_new.txt\');
Storage::disk(\'aliyun\')->rename(\'prefix/path/file.txt\', \'prefix/path/file_new.txt\');

文件删除

Storage::disk(\'aliyun\')->delete(\'prefix/path/file.txt\');
Storage::disk(\'aliyun\')->delete([\'prefix/path/file1.txt\', \'prefix/path/file2.txt\']);

文件夹操作

Storage::disk(\'aliyun\')->makeDirectory(\'prefix/path\'); 
Storage::disk(\'aliyun\')->deleteDirectory(\'prefix/path\');

// 查询一级子目录文件
Storage::disk(\'aliyun\')->files(\'prefix/path\');
// 递归查询多级子目录文件
Storage::disk(\'aliyun\')->allFiles(\'prefix/path\');

// 查询一级子目录
Storage::disk(\'aliyun\')->directories(\'prefix/path\'); 
// 递归查询多级子目录
Storage::disk(\'aliyun\')->allDirectories(\'prefix/path\'); 
项目GIT仓库
阿里云OSS文档

以上是关于[扩展推荐]阿里云对象存储扩展 —— Laravel最好的Aliyun OSS Storage扩展的主要内容,如果未能解决你的问题,请参考以下文章

扩展云存储边界,阿里云推出全球首个云定义存储产品

Linux扩展阿里云数据盘分区和文件系统

接入阿里云OSS--公有云分布式存储

接入阿里云OSS--公有云分布式存储

接入阿里云OSS--公有云分布式存储

阿里云ACP笔记-ECS