cwssrl/laravel-azure-storage

Laravel 的 Storage API 的 Microsoft Azure Blob Storage 集成

v1.2.0 2024-01-22 10:35 UTC

This package is auto-updated.

Last update: 2024-09-23 12:03:18 UTC


README

CircleCI

Microsoft Azure Blob Storage 集成于 Laravel 的 Storage API。

这是一个 Laravel 文件存储 API 的自定义驱动程序,该 API 本身建立在 Flysystem 3 之上。它使用 Flysystem 的 Azure Blob 存储适配器,因此不能轻松添加比这些更多的功能——实际上,添加这些功能将超出项目范围。

安装

使用 composer 安装该包

composer require cwssrl/laravel-azure-storage

然后将其添加到 config/filesystems.php 文件的 disks 部分

        'azure' => [ // NB This need not be set to "azure", because it's just the name of the connection - feel free to call it what you want, or even set up multiple blobs with different names
            'driver'    => 'azure', // As this is the name of the driver, this MUST be set to "azure"
            'name'      => env('AZURE_STORAGE_NAME'),
            'key'       => env('AZURE_STORAGE_KEY'),
            'container' => env('AZURE_STORAGE_CONTAINER'),
            'url'       => env('AZURE_STORAGE_URL'),
            'prefix'    => null,
            'connection_string' => env('AZURE_STORAGE_CONNECTION_STRING') // optional, will override default endpoint builder
        ],

最后,将 AZURE_STORAGE_NAMEAZURE_STORAGE_KEYAZURE_STORAGE_CONTAINERAZURE_STORAGE_URL 字段添加到您的 .env 文件中,并使用相应的凭据。 AZURE_STORAGE_URL 字段是可选的,这允许您设置一个自定义 URL,以便从 Storage::url() 返回,如果使用 $root 容器,则 URL 将返回而不包含容器路径。可以可选地使用 prefix。如果没有设置,则使用容器根目录。然后您可以将 azure 驱动程序设置为默认或云驱动程序,并像往常一样使用它来获取和检索文件。

有关如何使用此驱动程序的详细信息,请参阅 Laravel 文件存储 API 文档

自定义端点

该包支持使用自定义端点,如下例所示

        'azure' => [
            'driver'    => 'azure',
            'name'      => env('AZURE_STORAGE_NAME'),
            'key'       => env('AZURE_STORAGE_KEY'),
            'container' => env('AZURE_STORAGE_CONTAINER'),
            'url'       => env('AZURE_STORAGE_URL'),
            'prefix'    => null,
            'connection_string' => null,
            'endpoint'  => env('AZURE_STORAGE_ENDPOINT'),
        ],

然后您可以在 .env 文件中指定一个合适的 AZURE_STORAGE_ENDPOINT 值。

SAS 令牌身份验证

使用 SAS 令牌身份验证时,需要端点。值的格式如下: https://[accountName].blob.core.windows.net

        'azure' => [
            'driver'    => 'azure',
            'sasToken'  => env('AZURE_STORAGE_SAS_TOKEN'),
            'container' => env('AZURE_STORAGE_CONTAINER'),
            'url'       => env('AZURE_STORAGE_URL'),
            'prefix'    => null,
            'endpoint'  => env('AZURE_STORAGE_ENDPOINT'),
        ],

重试

Azure Storage SDK 提供了一个 重试失败的中间件。要启用重试中间件,请在磁盘的配置选项中添加一个 retry 指令。

        'azure' => [
            'driver'    => 'azure',
            // Other Disk Options...
            'retry'     => [
                'tries' => 3,                   // number of retries, default: 3
                'interval' => 500,              // wait interval in ms, default: 1000ms
                'increase' => 'exponential'     // how to increase the wait interval, options: linear, exponential, default: linear
            ]
        ],

不支持的功能

由于该包使用 Flysystem Azure 集成,因此它与以下限制相同

  • 不支持设置或检索可见性。
  • Mimetypes 总是解析,而其他则不解析。
  • 不支持以任何方式创建目录。

支持策略

此包支持当前 Laravel LTS 版本,以及任何后续版本。如果您正在使用较旧的 Laravel 版本,它可能可以工作,但我提供不了任何保证,也不会接受添加此支持的拉取请求。

通过扩展,由于当前 Laravel LTS 版本需要 PHP 7.0 或更高版本,因此我没有对其进行 PHP < 7 的测试,也不会接受添加此支持的任何拉取请求。

您能添加对存储后端 X 的支持吗?

不。这 仅限 Azure Blob 存储使用,我明确不感兴趣扩大其范围以支持其他后端。

只要 Flysystem 支持它,您就可以像在 https://laravel.net.cn/docs/9.x/filesystem#custom-filesystems 中描述的那样轻松地自己实现 Laravel 文件系统驱动程序,如果需要的话——这是构建此包的方法。