workupsrl / flysystem-sharepoint-adapter
[beta] 将 flysystem 集成到 SharePoint 文件存储的适配器
Requires
- php: ^8.0
- ext-json: *
- league/flysystem: ^3.0
- workupsrl/sharepoint-sdk: ^1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-24 17:56:09 UTC
README
此包包含一个适配器,用于 Flysystem 以将 SharePoint 作为文件存储。
安装
您可以通过 composer 安装此包。
composer require workupsrl/flysystem-sharepoint-adapter
使用方法
您需要在 Azure 上为新的应用程序请求新的 clientId 和 clientSecret。
-
前往
Azure 门户
https://portal.azure.com -
前往
Active Directory
-
前往
应用注册
-
点击
新注册
并按照向导操作。
(给它起一个名字,比如我的名字是 'gwsn-sharepoint-connector',并决定支持的帐户,单租户应该足够了,但这也取决于您的组织) -
当应用程序创建完成后,请记下以下详细信息
-
'应用程序(客户端)ID',这将成为您的
$clientId
-
'目录(租户)ID',这将成为您的
$tenantId
-
然后我们在菜单中选择
API 权限
以设置所需的权限 -
点击
添加权限
并添加以下权限
Microsoft Graph- Files.ReadWrite.All
- Sites.ReadWrite.All
- User.Read
-
点击
为 ...Company... 授予管理员同意
-
在菜单中选择
证书与密钥
-
点击
新客户端密钥
-
为其提供描述和过期日期,其值将是您的
$clientSecret
-
最后一个参数将是 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'
- Sharepoint 网站URL:
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 许可证 (MIT)。有关更多信息,请参阅 许可文件。