gwsn/flysystem-sharepoint-adapter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gwsn/flysystem-sharepoint-adapter

Composer 安装命令:

composer require gwsn/flysystem-sharepoint-adapter

包简介

[beta] Adapter for flysystem to use sharepoint as filestore

README 文档

README

This package contains a adapter for Flysystem to use Sharepoint as filestorage.

Installation

You can install the package via composer:

composer require gwsn/flysystem-sharepoint-adapter

Usage

You need to request a new clientId and clientSecret for a new application on Azure.

  1. Go to Azure portal https://portal.azure.com

  2. Go to Azure Active Directory

  3. Go to App registrations

  4. Click on new Registration and follow the wizard.
    (give it a name like mine is 'gwsn-sharepoint-connector' and make a decision on the supported accounts, single tenant should be enough but this depends on your organisation)

  5. When created the application is created write down the following details

  6. 'Application (client) id', this will be your $clientId

  7. 'Directory (tenant) id', this will be your $tenantId

  8. Then we go in the menu to the API permissions to set the permissions that are required

  9. The click on Add a permission and add the following permissions:
    Microsoft Graph:

    • Files.ReadWrite.All
    • Sites.ReadWrite.All
    • User.Read
  10. Click on the Grant admin consent for ...Company...

  11. Go in the menu to Certificates & secrets

  12. Click on new client secret

  13. Give it a description and expiry date and the value will be your $clientSecret

  14. The last parameter will be the sharepoint 'slug', this is part of the url of the sharepoint site what you want to use and creation of sharepoint site is out of scope of this readme.
    When you sharepoint url is like https://{tenant}.sharepoint.com/sites/{site-slug}/Shared%20Documents/Forms/AllItems.aspx
    You need to set the $sharepointSite as {site-slug}

    Example:

    • Sharepoint site url: https://GWSN.sharepoint.com/sites/gwsn-documents-store/Shared%20Documents/Forms/AllItems.aspx
    • Sharepoint site variable: $sharepointSite = 'gwsn-documents-store'
use GWSN\FlysystemSharepoint\FlysystemSharepointAdapter;
use GWSN\FlysystemSharepoint\SharepointConnector;
use League\Flysystem\Filesystem;

$tenantId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
$clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$sharepointSite = 'your-path-to-your-site';

$connector = new SharepointConnector($tenantId, $clientId, $clientSecret, $sharepointSite);

$prefix = '/test'; // optional
$adapter = new FlysystemSharepointAdapter($connector, $prefix);


$flysystem = new Filesystem($adapter);

Testing

$ composer run-script test

Security

If you discover any security related issues, please email info@gwsn.nl instead of using the issue tracker.

Laravel

To use the flysystem in Laravel there are additional steps required:

First we need to create a FlySystemSharepointProvider and register this in the config/app.php

Then we need to create the config into the config/filesystem.php

Create the FlySystemSharepointProvider

we need to create a provider to register the custom FlySystem Adapter

create a new file in the app/Providers directory called FlySystemSharepointProvider.php with the following content:

<?php

namespace App\Providers;

use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use GWSN\FlysystemSharepoint\FlysystemSharepointAdapter;
use GWSN\FlysystemSharepoint\SharepointConnector;

class FlySystemSharepointProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register() { }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Storage::extend('sharepoint', function ($app, $config) {

            $adapter = new FlysystemSharepointAdapter(new SharepointConnector(
                    $config['tenantId'],
                    $config['clientId'],
                    $config['clientSecret'],
                    $config['sharepointSite'],
                ),
                $config['prefix'],
            );

            return new FilesystemAdapter(
                new Filesystem($adapter, $config),
                $adapter,
                $config
            );
        });
    }
}

Register the provider in the App config

Add the bottom of the list with providers we need to add the previous created Provider:

 'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
         [...]
         App\Providers\FlySystemSharepointProvider::class,
 ]

Update the Filesystem config

Add filesystem Disks section we will add a new custom disk: sharepoint.

We use env variables as config but you could also enter them directly as string

 /*
    |--------------------------------------------------------------------------
    | Filesystem Disks
    |--------------------------------------------------------------------------
    |
    | Here you may configure as many filesystem "disks" as you wish, and you
    | may even configure multiple disks of the same driver. Defaults have
    | been set up for each driver as an example of the required values.
    |
    | Supported Drivers: "local", "ftp", "sftp", "s3"
    |
    */

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],

        'sharepoint' => [
            'driver' => 'sharepoint',
            'tenantId' => env('SHAREPOINT_TENANT_ID', 'secret'),
            'clientId' => env('SHAREPOINT_CLIENT_ID', 'secret'),
            'clientSecret' => env('SHAREPOINT_CLIENT_SECRET_VALUE', 'secret'),
            'sharepointSite' => env('SHAREPOINT_SITE', 'laravelTest'),
            'prefix' => env('SHAREPOINT_PREFIX', 'test'),
        ]

    ],

Usage in laravel

it is bad practice to use logic into a controller but for example purpose we show it in the controller:

App\Http\Controllers\Controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Storage;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function index() {

        try {
            Storage::disk('sharepoint')->put('test.txt', 'testContent');
            return Storage::disk('sharepoint')->get('test.txt');
            
        } catch (\Exception $exception) {
            dd($exception);
        }
        return 'error';
    }
}

License

The MIT License (MIT). Please see License File for more information.

gwsn/flysystem-sharepoint-adapter 适用场景与选型建议

gwsn/flysystem-sharepoint-adapter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 94.91k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2022 年 02 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sharepoint」 「Flysystem」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gwsn/flysystem-sharepoint-adapter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gwsn/flysystem-sharepoint-adapter 我们能提供哪些服务?
定制开发 / 二次开发

基于 gwsn/flysystem-sharepoint-adapter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 94.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-08