flownative/azure-blobstorage

将Flow和Neos应用程序的资源存储在Azure Blob Storage中

v1.1.0 2023-10-05 12:47 UTC

This package is auto-updated.

Last update: 2024-09-05 14:44:16 UTC


README

MIT license Packagist Packagist Maintenance level: Love

Azure Blob Storage适配器用于Neos和Flow

Flow软件包允许您将资产(资源)存储在Azure Blob Storage中,并将资源发布在那里。由于Neos CMS在底层使用Flow的资源管理,因此此适配器也适用于Neos中的所有类型的资产。

主要功能

  • 在私有容器中存储所有资产或特定集合
  • 将资产发布到私有或公共容器
  • 支持选定媒体类型的GZIP压缩
  • 命令行界面用于基本任务,如连接检查或重新发布资源

使用此连接器,您可以在不存储任何资产(图像、PDF等)的Web服务器上运行Neos网站。

安装

Flownative Azure Blob Storage连接器通过Composer作为常规Flow软件包安装。对于您的现有项目,只需将flownative/azure-blobstorage包含到您的Flow或Neos分发的依赖项中即可。

$ composer require flownative/azure-blobstorage

配置

凭证

为了与Azure API通信,您需要提供具有访问ABS的账户的凭证。将以下配置添加到您希望使用的Flow上下文的Settings.yaml中(例如在Configuration/Production/Settings.yaml中),并确保用您自己的数据替换凭证

Flownative:
  Azure:
    BlobStorage:
      profiles:
        default:
          credentials:
            accountName: 'myaccountname'
            accountKey: 'myaccountkey'

除了使用默认连接字符串(默认为DefaultEndpointsProtocol=https;AccountName=myAccountName;AccountKey=myAccountKey)中的名称和密钥外,还可以直接指定连接字符串。这允许提供在Azure文档中描述的变体。

Flownative:
  Azure:
    BlobStorage:
      profiles:
        default:
          credentials:
            connectionString: 'UseDevelopmentStorage=true'

目前,您只能定义一个连接配置文件,即“默认”配置文件。未来版本可能支持其他配置文件。

容器设置

您需要一个容器用于资源存储和一个用于发布目标的容器。它们的名称由您自己决定。用作存储容器的那个不应该公开访问,用作发布目标的容器必须公开访问blob。请参阅Azure文档中配置匿名公开读取访问的部分以获取如何操作的说明。

测试设置

您可以通过使用所选容器执行connect命令来测试您的设置。

$ ./flow abs:connect storage.example.net

将资产发布到Azure Blob Storage

一旦安装了连接器包,您就可以添加一个新的发布目标,该目标使用该连接并将其分配给您的集合。

Neos:
  Flow:
    resource:
      collections:
        persistent:
          target: 'azurePersistentResourcesTarget'
      targets:
        azurePersistentResourcesTarget:
          target: 'Flownative\Azure\BlobStorage\AbsTarget'
          targetOptions:
            container: 'target.example.net'
            keyPrefix: '/'
            baseUri: 'https://myaccountname.blob.core.windows.net/target.example.net/'

由于新的发布目标最初将是空的,您需要使用resource:publish命令将您的资产发布到新目标。

$ ./flow resource:publish

此命令将上传您的文件到目标,并从现在起使用计算出的远程URL为您的所有资产。

切换集合的存储

如果您想从默认的本地文件系统存储迁移到远程存储,您需要将所有现有的持久化资源复制到新的存储,并在之后默认使用该存储。

首先,您需要将ABS连接器添加到您的配置中。由于您可能还想通过远程存储系统提供资产,因此还需要添加一个包含您已发布资源的目标。

Neos:
  Flow:
    resource:
      storages:
        azurePersistentResourcesStorage:
          storage: 'Flownative\Azure\BlobStorage\AbsStorage'
          storageOptions:
            container: 'storage.example.net'
            keyPrefix: '/'
      targets:
        azurePersistentResourcesTarget:
          target: 'Flownative\Azure\BlobStorage\AbsTarget'
          targetOptions:
            container: 'target.example.net'
            keyPrefix: '/'
            baseUri: 'https://myaccountname.blob.core.windows.net/target.example.net/'

关于配置选项的一些说明

keyPrefix选项允许您在多个网站或应用程序之间共享一个容器。所有对象键都将以前面的字符串作为前缀。

baseUri选项定义了指向您的发布资源的公开访问地址的根。在上面的示例中,baseUri指向一个需要单独设置的子域。如果baseUri为空,Azure Blob Storage发布目标将自动确定公开URL。

为了将资源复制到新的存储,我们需要一个临时集合,该集合使用存储和新的发布目标。

Neos:
  Flow:
    resource:
      collections:
        tmpNewCollection:
          storage: 'azurePersistentResourcesStorage'
          target: 'azurePersistentResourcesTarget'

现在您可以使用resource:copy命令

$ ./flow resource:copy persistent tmpNewCollection --publish

这将从您当前存储(本地文件系统)复制所有文件到新的远程存储。--publish标志表示此命令还发布了所有资源到新的目标,您在当前存储和发布目标上的状态与在新的目标上一致。

现在您可以覆盖旧的集合配置并删除临时配置

Neos:
  Flow:
    resource:
      collections:
        persistent:
          storage: 'azurePersistentResourcesStorage'
          target: 'azurePersistentResourcesTarget'

清除缓存,操作完成。

$ ./flow flow:cache:flush

双容器设置

由于Azure Blob Storage中blob的公共访问处理方式,因此只能实现双容器设置:一个容器是私有的,另一个是公开可访问的。

在双容器设置中,资源将被复制:原始资源存储在“存储”容器中,然后复制到“目标”容器。每次创建或导入新资源时,它将存储在存储容器中,然后自动发布(即复制)到目标容器。

优点在于,这允许您有对人类和SEO友好的URL指向您的资源,因为复制到目标容器的对象可以有更说明性的名称,其中包括资源的原始文件名(请参阅下面的publicPersistentResourceUris选项)。

自定义公开URL

Azure Blob Storage目标支持自定义用户看到的URL的方式。尽管容器中对象的路径和文件名相当固定(请参阅上面的baseUrikeyPrefix选项),您可能希望使用反向代理或内容分发网络来提供存储在目标容器中的资源。在这种情况下,您可以指示目标根据您自己的规则生成URL。然后,您的责任是确保这些URL确实有效。

假设我们已设置一个作为反向代理的web服务器。对assets.flownative.com的请求被重写,使用类似于https://assets.flownative.com/a817…cb1/logo.svg的URI实际上会提供存储在存储容器中并由SHA1指定的文件。

您可以通过定义包含占位符的模式来告诉目标生成这样的URI

      targets:
        azurePersistentResourcesTarget:
          target: 'Flownative\Azure\BlobStorage\AbsTarget'
          targetOptions:
            container: 'flownativecom.flownative.cloud'
            baseUri: 'https://assets.flownative.com/'
            persistentResourceUris:
              pattern: '{baseUri}{sha1}/{filename}'

可能的占位符包括

  • {baseUri} 在目标选项中定义的基本URI
  • {containerName} 目标容器的名称
  • {keyPrefix} 目标配置的关键前缀
  • {sha1} 资源的SHA1
  • {filename} 资源的全名,例如“logo.svg”
  • {fileExtension} 资源的文件扩展名,例如“svg”

出于遗留和方便的原因,默认模式取决于使用的设置

  • 没有模式且没有设置baseUri: https://myaccountname.blob.core.windows.net/{containerName}/{keyPrefix}{sha1}
  • 没有设置模式: {baseUri}/{keyPrefix}{sha1}/{filename}

相应的设置由目标自动检测,并据此设置模式。当然,您可以通过指定如上所述的 pattern 设置来覆盖模式。

动态自定义基本 URI

您的应用程序可以负责通过注册自定义方法来渲染基本 URI。设置选项后,目标将调用您的方法,并使用返回的字符串作为基本 URI。

此机制允许您根据当前请求调整域名或其他基本 URI 的部分。在以下示例中,我们使用自定义基本 URI 方法将域名 "example.com" 替换为 "replaced.com"。

namespace Flownative\Test;

class BlobStorageDemo {
    /**
     * @param array $targetOptions
     * @return string
     */
    public function renderBaseUri(array $targetOptions): string
    {
        return str_replace('example.com', 'replaced.com', $targetOptions['baseUri']);
    }
}
      targets:
        azurePersistentResourcesTarget:
          target: 'Flownative\Azure\BlobStorage\AbsTarget'
          targetOptions:
            customBaseUriMethod:
              objectName: 'Flownative\Test\BlobStorageDemo'
              methodName: 'renderBaseUri'

以下选项将传递给您的渲染方法

  • targetClass
  • containerName
  • keyPrefix
  • baseUri
  • persistentResourceUriEnableSigning

GZIP 压缩

Azure Blob Storage 支持对用户传输文件时使用 GZIP 压缩,但是这些文件需要在 Azure Blob Storage 外部压缩,然后作为 GZIP 压缩数据上传。此插件支持在发布时实时转码资源。存储在 storage 中的数据始终以未压缩的形式存储。在发布到 target 时,配置为 GZIP 压缩的媒体类型的文件会自动转换为 GZIP。

您可以配置压缩级别以及应该以这种方式压缩的媒体类型

Neos:
  Flow:
    resource:
      targets:
        azurePersistentResourcesTarget:
          target: 'Flownative\Azure\BlobStorage\AbsTarget'
          targetOptions:
            gzipCompressionLevel: 9
            gzipCompressionMediaTypes:
            - 'text/plain'
            - 'text/css'
            - 'text/xml'
            - 'text/mathml'
            - 'text/javascript'
            - 'application/x-javascript'
            - 'application/xml'
            - 'application/rss+xml'
            - 'application/atom+xml'
            - 'application/javascript'
            - 'application/json'
            - 'application/x-font-woff'
            - 'image/svg+xml'

请注意,为已压缩的数据(例如图像或电影)添加媒体类型可能会增加数据大小,因此应避免这样做。

ABS 的完整示例配置

Neos:
  Flow:
    resource:
      storages:
        azurePersistentResourcesStorage:
          storage: 'Flownative\Azure\BlobStorage\AbsStorage'
          storageOptions:
            container: 'storage.example.net'
            keyPrefix: '/'
      collections:
        # Collection which contains all persistent resources
        persistent:
          storage: 'azurePersistentResourcesStorage'
          target: 'azurePersistentResourcesTarget'
      targets:
        localWebDirectoryPersistentResourcesTarget:
          target: 'Neos\Flow\ResourceManagement\Target\FileSystemTarget'
          targetOptions:
            path: '%FLOW_PATH_WEB%_Resources/Persistent/'
            baseUri: '_Resources/Persistent/'
            subdivideHashPathSegment: false
        azurePersistentResourcesTarget:
          target: 'Flownative\Azure\BlobStorage\AbsTarget'
          targetOptions:
            container: 'target.example.net'
            keyPrefix: '/'
            baseUri: 'https://demostorage.blob.core.windows.net/target.example.net/'

Flownative:
  Azure:
    BlobStorage:
      profiles:
        default:
          credentials:
            accountName: 'demostorage'
            accountKey: '…fgur674wurz…gfhsdjvru4brtfg…'