wesllenalves/laravel-azure-storage-wesllen

适用于 Laravel 的 Storage API 的 Microsoft Azure Blob Storage 集成

V1.0.1 2022-10-27 19:53 UTC

This package is auto-updated.

Last update: 2024-09-28 00:14:17 UTC


README

CircleCI

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

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

本项目基于原始项目 matthewbdaly/laravel-azure-storage 衍生,并添加了一些功能。

安装

使用 composer 安装此包

composer require wesllenalves/laravel-azure-storage-wesllen

然后将其添加到 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,该 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
            ]
        ],