【问题标题】:What design pattern should I use for using Cloudstorage like S3 and/or Azure Blob?我应该使用什么设计模式来使用像 S3 和/或 Azure Blob 这样的 Cloudstorage?
【发布时间】:2017-05-08 22:40:35
【问题描述】:

我正在建立一个电子邮件系统,它可以检索带有附件的传入电子邮件并回复电子邮件;这需要将附件保存并存储到 Azure blob 和/或 AWS S3 存储中...

我有两个控制器,CommentsControllerTicketsController;两个控制器都需要使用 Azure API 或 AWS Storage 来存储和检索文件。

这是我当前的代码

Class CommentsController extends Controller {

     $accountKey2 = "xxxxxxxxxxxxxxxx";
     $accountName1 = "xxxxxxxx";
     $connectionString = "DefaultEndpointsProtocol=http;AccountName=" . $accountName1 . ";AccountKey=" . $accountKey2;
     $blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);


}

正如您在上面看到的,我需要在每个控制器上使用 azure $blobRestProxy,并且每当我需要上传文件时。

在这种情况下使用什么好的设计模式?

我正在考虑一种存储库模式,我会将 azure 接口注入到控制器中。

请指教。

【问题讨论】:

  • 欢迎来到stackoverflow。请,您能否提供一些您的具体问题的代码。这证明了您尝试了多远,它将帮助其他成员更好地理解您的问题,当时,您将向他们提供您的问题的背景。请检查以下链接:stackoverflow.com/help/mcvestackoverflow.com/help/how-to-ask
  • 我明白了,我会更新我的问题,谢谢提醒
  • 不客气... :)

标签: php laravel azure amazon-web-services design-patterns


【解决方案1】:

在 laravel 中有很多方法可以与所有控制器共享变量。我想使用Service Providers 来实现这一点。

首先创建一个BlobServiceProvider 类。

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use WindowsAzure\Common\ServicesBuilder;

class BlobServiceProvider extends ServiceProvider
{

    public function boot() {}

    public function register() {

        $this->app->singleton('blobRestProxy', function() {
            $accountKey2 = "xxxxxxxxxxxxxxxx";
            $accountName1 = "xxxxxxxx";
            $connectionString = "DefaultEndpointsProtocol=http;AccountName=" . $accountName1 . ";AccountKey=" . $accountKey2;

            return ServicesBuilder::getInstance()->createBlobService($connectionString);
        });
    }
}

然后将下面这行代码添加到config\app.php文件中的providers数组中。

App\Providers\BlobServiceProvider::class,

现在,您可以在任何控制器中使用此变量 $blobRestProxy,如下所示。

$blobRestProxy = \App::make('blobRestProxy');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 2019-09-05
    • 2018-06-23
    相关资源
    最近更新 更多