gwsn / flysystem-sharepoint-adapter

[beta] 将 flysystem 与 SharePoint 作为文件存储适配器

1.1.1 2024-05-09 16:01 UTC

This package is auto-updated.

Last update: 2024-09-09 16:48:22 UTC


README

本软件包包含一个适配器,用于将 Flysystem 与 SharePoint 文件存储进行集成。

安装

您可以通过 composer 安装此软件包。

composer require gwsn/flysystem-sharepoint-adapter

用法

您需要在 Azure 上为新应用程序请求一个新的 clientId 和 clientSecret。

  1. 前往 Azure 门户 https://portal.azure.com

  2. 前往 Azure Active Directory

  3. 前往 应用注册

  4. 点击 新注册 并按照向导操作。
    (给它起一个名字,比如我的名字是 'gwsn-sharepoint-connector',并决定支持的账户,单一租户应该足够,但这取决于您的组织)

  5. 当应用程序创建后,请记下以下详细信息

  6. '应用程序(客户端)ID',这将是您的 $clientId

  7. '目录(租户)ID',这将是您的 $tenantId

  8. 然后我们在菜单中转到 API 权限 来设置所需的权限

  9. 点击 添加权限 并添加以下权限
    Microsoft Graph

    • Files.ReadWrite.All
    • Sites.ReadWrite.All
    • User.Read
  10. 点击 为...公司...授予权限

  11. 在菜单中转到 证书 & 密钥

  12. 点击 新建客户端密钥

  13. 为其添加描述和过期日期,其值将是您的 $clientSecret

  14. 最后一个参数将是 SharePoint 的 'slug',这是您要使用的 SharePoint 站点 URL 的一部分,SharePoint 站点的创建超出了本说明的范围。
    当您的 SharePoint URL 如下所示时:https://{tenant}.sharepoint.com/sites/{site-slug}/Shared%20Documents/Forms/AllItems.aspx
    您需要将 $sharepointSite 设置为 {site-slug}

    示例

    • Sharepoint 站点 URL: https://GWSN.sharepoint.com/sites/gwsn-documents-store/Shared%20Documents/Forms/AllItems.aspx
    • Sharepoint 站点变量: $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);

测试

$ composer run-script test

安全性

如果您发现任何与安全相关的问题,请通过电子邮件 info@gwsn.nl 联系,而不是使用问题跟踪器。

Laravel

要在 Laravel 中使用 flysystem,需要执行一些额外的步骤。

首先,我们需要创建一个 FlySystemSharepointProvider 并将其注册到 config/app.php 中。

然后,我们需要在 config/filesystem.php 中创建配置。

创建 FlySystemSharepointProvider

我们需要创建一个提供者以注册自定义 FlySystem 适配器。

app/Providers 目录中创建一个名为 FlySystemSharepointProvider.php 的新文件,内容如下

<?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
            );
        });
    }
}

在 App 配置中注册提供者

将我们需要添加的提供者添加到列表底部

 'providers' => [

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

更新文件系统配置

添加文件系统磁盘部分,我们将添加一个新的自定义磁盘:sharepoint。

我们使用环境变量作为配置,但您也可以直接将其作为字符串输入

 /*
    |--------------------------------------------------------------------------
    | 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'),
        ]

    ],

Laravel 中的用法

将逻辑放入控制器中是不好的做法,但为了示例目的,我们在控制器中展示了它

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';
    }
}

许可证

麻省理工学院许可证(MIT)。请参阅许可文件获取更多信息。