jlm/aws-bundle

此包已被放弃且不再维护。未建议替代包。

Symfony 对 Amazon PHP SDK 2.x 的包装

安装次数: 2,824

依赖者: 0

建议者: 0

安全性: 0

星级: 0

关注者: 0

分支: 0

开放问题: 6

类型:symfony-bundle

1.0.0 2014-02-14 09:50 UTC

This package is not auto-updated.

Last update: 2021-04-30 21:48:44 UTC


README

Bitdeli Badge Build Status Coverage Status Stories in Ready Total Downloads Latest Stable Version

JLMAwsBundle

此包作为 Amazon 的 AWS SDK 2.x 的较薄的包装,使其更容易配置 SDK 并将其服务注册为 Symfony 服务。

SDK 本身是基于 Guzzle 构建的,使用 Guzzle 的服务构建器和配置风格。此包允许您以更符合 Symfony 项目的风格配置 AWS。然后,将 Symfony 配置直接转换为 Guzzle 配置,并将其传递给 SDK 以生成 Guzzle 服务客户端。然后,将这些 Guzzle 服务客户端注册为 Symfony DI 容器中的服务,以便在您的应用程序中使用。

注意:这是一个相对较新的包,尚未得到广泛使用。虽然有许多单元测试,但仍可能存在一些高级配置的问题。还有一些 AWS 配置选项尚未通过配置支持。欢迎所有贡献!

安装

您可以通过向您的 composer.json 文件中添加以下内容来通过 Composer 安装此包

"require": {
    # ..
    "jlm/aws-bundle": "dev-master"
    # ..
}

然后将包添加到您的 AppKernel.php 中的 Kernel(如果使用 Symfony Standard)

public function registerBundles()
    {
        $bundles = array(
            /* ... */
            new JLM\AwsBundle\JLMAwsBundle(),
        );

        return $bundles;
    }

就是这样!您现在应该能够通过 Symfony 配置 SDK 并从 DIC 获取其服务。

配置

要真正理解配置,您应该对没有此包的 AWS 配置有很好的了解。我们为大多数事物使用相同的名称和非常相似的结构,但出于各种原因,某些事物有所改变。如果您不熟悉,请参阅 AWS 配置文档

jlm_aws:

    # Default service prefix used for all AWS service names in the Symfony Container, eg: jlm_aws.ec2
    service_prefix:       jlm_aws # Example: jlm_aws

    # Default base class to use as the AWS factory. Note: You *must* extend the JLM version of Aws or implement a similar version as the default Aws base class is not compatible with this bundle!
    aws_base_class:       JLM\AwsBundle\Aws\Common\Aws # Example: JLM\AwsBundle\Aws\Common\Aws

    # Enables the S3 stream wrapper by either passing true (which will use the default S3 service) or by passing an S3 service name directly. Note: the service (whether default or not) *must* be enabled in the services configuration or you will receive an error!
    s3_stream_wrapper:    false # Example: my_s3|true

    # Default settings applied to all services. These override the SDK's defaults. (@see http://docs.aws.amazon.com/aws-sdk-php/guide/latest/configuration.html)
    default_settings:
        credentials:

            # A valid AWS access key ID
            key:                  ~ # Example: YOUR_AWS_ACCESS_KEY_ID

            # AWS secret for AWS access key ID
            secret:               ~ # Example: YOUR_AWS_SECRET_ACCESS_KEY

            # @Name of service that will provide credentials. Servicem ust be instance of Aws\Common\Credentials\Credentials
            provider_service:     ~ # Example: @my_provider_service

            # Optional custom cache key to use with the credentials
            cache_key:            ~

            # @Name of optional custom Guzzle client interface to use if your credentials require an HTTP request to acquire, such as with RefreshableInstanceProfileCredentials. Service must be an instance of Guzzle\Http\ClientInterface
            client_service:       ~ # Example: @my_guzzle_client
        endpoint:

            # Region
            region:               ~ # Example: us-east-1

            # Scheme to use for HTTP requests
            scheme:               ~ # One of "http"; "https", Example: true

            # Allows you to specify a custom endpoint instead of having the SDK build one automatically from the region and scheme
            base_url:             ~ # Example: http://mycustom.com/endpoint

            # Overrides the signature used by the client
            signature:
                version:              ~ # One of "v4"; "v3https"; "v2", Example: v4

                # Rather than defining the version explicitly, provide a service @name that is an instance of Aws\Common\Signature\SignatureInterface in order to provide the signature version
                version_service:      ~ # Example: @my_signature_service

                # Not to be confused with version_service, this is the service scope used for Signature V4.
                service:              ~

                # The signature region scope for Signature V4
                region:               ~
        client:

            # Set to false to disable SSL. Otherwise, true to use the SDK bundled SSL cert bundle, 'system' to use the bundle on your system, or a string pointing to the path of a specific cert file or directory of cert files.
            ssl_ca:               ~ # Example: system

            # Associative array of cURL options to apply to every request created by the client. Strings will be converted to matching cURL PHP constants
            curl_options:

                # Example:
                CURLOPT_PROXY:       user:password@host:port

                # Prototype
                name:                 ~

            # Associate array of Guzzle reqeust options. (@see http://docs.guzzlephp.org/en/latest/http-client/client.html#request-options)
            request_options:

                # Associative array of headers to pass with each request
                headers:

                    # Example:
                    X-Foo:               bar

                    # Prototype
                    name:                 ~

                # Associative array of query string parameters to add to each request
                query_params:

                    # Example:
                    abc:                 123

                    # Prototype
                    name:                 ~

                # Specifies HTTP authorization parameters to use with each request
                auth:
                    username:             ~
                    password:             ~
                    type:                 ~ # One of "basic"; "digest"; "ntlm"; "any"

                # Associative array of cookie values to send along with each request
                cookies:

                    # Example:
                    foo:                 bar

                    # Prototype
                    name:                 ~
                allow_redirects:      ~

                # Path to a file where the body of a response is downloaded. Alternatively, a service reference to an instance of Guzzle\Http\EntityBodyInterface may be provided
                save_to:              ~ # Example: /path/to/file

                # Whether or not to throw exceptions for unsuccessful HTTP response codes (eg: 404, 500, etc.)
                throw_exceptions:     ~

                # An associative array of data parameters to send along with each request. Note that these are not query parameters
                params:

                    # Example:
                    foo:                 bar

                    # Prototype
                    name:                 ~

                # The maximum number of seconds to allow for an entire transfer to take place before timing out
                timeout:              ~ # Example: 20

                # The maximum number of seconds tow ait while trying to connect
                connect_timeout:      ~ # Example: 1.5

                # True to enable SSL, false to disable SSL, or supply the path to a CA bundle to enable verification using a custom certificate
                verify:               ~ # Example: 1

                # Allows you to specify a PEM formatted SSL client certificate to use with servers that require one
                cert:

                    # Path to PEM formatted SSL client certificate
                    path:                 ~ # Required, Example: /path/to/cert.pem

                    # Password for PEM certificate
                    password:             ~ # Example: YOUR_PASSWORD

                # Allows you to specify a file containing your PEM formatted private key
                ssl_key:

                    # Path to PEM formatted private key
                    path:                 ~ # Required, Example: /path/to/key.pem

                    # Password for your PEM private key
                    password:             ~ # Example: YOUR_PASSWORD

                # Specifies an HTTP proxy to be used for each request
                proxy:                ~ # Example: http://username:password@host:port

                # Whether or not to show verbose cURL output for each request
                debug:                ~

                # When using a static client you can set the sream option to true to return a GuzzlEStreamStream object
                stream:               ~

            # An associative array of default options to set on each command created by the client
            command_params:

                # Example:
                foo:                 bar

                # Prototype
                name:                 ~

            # A service reference to an instance of Guzzle\Log\LogAdapterInterface used to log backoff retries. May also be set to the string 'debug' in order to emit PHP warnings when a retry is issued.
            backoff_logger:       ~ # Example: debug

            # Optional template to use for exponential backoffl og messages.
            backoff_logger_template:  ~

    # Enable and configure the services available in the AWS SDK
    services:
        autoscaling:

            # Prototype
            default:
                # Allows a custom class to be set as the service client.
                class:                ~

                # Extend an existing configuration. By default, the "default" for each service type is extended which in turn extends "default_settings"
                extends:              ~

                credentials:
                    # ... #
                endpoint:
                    # ... #
                client:
                    # ... #
            my_custom_autoscaler:
                # ... #
        ec2:
            default: false
            my_ec2: ~
            
        s3: ~
        # ... #

基本思想是存在按服务类型和名称键控的服务定义重复块。根级别的第一个块是 default_settings。这些是所有其他服务默认扩展的全球设置(默认情况下)并且在许多配置中是您需要添加配置(如凭证)的唯一地方。

services 之下定义您希望使用的所有服务。在 services 直接之下定义您希望配置的服务类型。在每种服务类型下,您可以定义一个或多个同类型的命名服务。例如,这允许您拥有多个 S3 客户端,也许一个用于每个区域。您可以选择不包含任何命名服务,而是将服务类型的值设置为 truenull。这将创建该服务的一个默认版本。

以下配置将启用所有可用的服务的默认版本

jlm_aws:
  services:
    autoscaling: ~              
    cloud_formation: ~
    cloud_front: ~
    cloud_front_20120505: ~
    cloud_search: ~
    cloud_trail: ~
    cloud_watch: ~
    data_pipeline: ~
    direct_connect: ~
    dynamo_db: ~
    dynamo_db_20111205: ~
    ec2: ~
    elasticache: ~
    elastic_beanstalk: ~
    elastic_load_balancing: ~
    elastic_transcoder: ~
    emr: ~
    glacier: ~
    kinesis: ~
    iam: ~
    import_export: ~
    opsworks: ~
    rds: ~
    redshift: ~
    route53: ~
    s3: ~
    sdb: ~
    ses: ~
    sns: ~
    sqs: ~
    storage_gateway: ~
    sts: ~
    support: ~
    swf: ~

服务类型的每个实例(包括 default_settings)都有完全相同的子选项,包括 credentialsendpointclient

要查看应用程序中的完整配置,请运行

$ app/console config:dump JLMAwsBundle

配置继承

此捆绑包配置涉及两个级别的继承。首先是 Symfony 级别。如果您在父文件(例如:config.yml)中为该捆绑包定义配置,然后在该子文件(例如:config_dev.yml)中导入该配置文件,则子文件可能会覆盖父文件中的任何配置指令。

然而,AWS SDK/Guzzle 级别还有一个继承的次要级别。通过为服务实例提供 extends 属性,您可以继承相同类型的另一个实例的设置。默认情况下,default_settings 会由所有类型继承,但您可以选择将多个 S3 实例连接在一起。

jlm_aws:
  default_settings:
    credentials:
      key: MY_KEY
      secret: MY_SECRET
    endpoint:
      region: us-east-1
  services:
    s3:
      default:
        credentials:
          key: MY_S3_KEY
          secret: MY_S3_SECRET
      s3_west:
        extends: default
        endpoint:
          region: us-west-1
      s3_west_readonly:
        extends: s3_west
        credentials:
          key: READ_ONLY_KEY
          secret: READ_ONLY_SECRET

上述配置将在容器中创建 3 个 S3 服务

  • jlm_aws.s3
  • jlm_aws.s3.s3_west
  • jlm_aws.s3.s3_west_readonly

第一个将继承来自 default_settings 的区域 us-east-1,但使用它自己的 keysecret 值。第二个与第一个相同,除了它将覆盖区域为 us-west-1。最后,第三个与第二个相同,但将使用不同的凭证。

这可以非常强大,但在大多数情况下,配置将比其能力简单得多。

S3 包装器

AWS SDK 提供了一个 S3 包装器。我们可以通过向 jlm_aws 提供配置选项 s3_stream_wrapper 来自动注册特定 S3 服务实例的流包装器。此配置选项接受 true|false 值或您希望用于流的 S3 实例的名称。如果提供 true,则假定您希望使用无名称的默认 S3 服务实例。

只能注册一个 S3 实例的流,因此如果您有多个 S3 服务实例,则无法注册多个流。您可以在运行时通过取消注册先前的流,获取您想要的 S3 客户端实例,然后使用该实例重新注册流来执行此操作。然而,显然我们无法在配置时提供这种便利。

jlm_aws:
  s3_stream_wrapper: true
  services:
    s3: ~

上述配置将创建一个默认的 S3 客户端并注册其流包装器。

重要:如果您选择将 s3_stream_wrapper 的值设置为 true,如我们上面所做的那样,您必须在配置中添加 s3: ~ 以启用 S3 默认服务。您也可以通过以下方式启用它

jlm_aws:
  s3_stream_wrapper: true
  services:
    s3:
      default: ~ # true would also be acceptable

上述两种配置是等效的。同样,如果您选择使用命名的实例,则该实例必须在 S3 服务配置下注册。

集成

此捆绑包自动与 PlaybloomGuzzleBundle 集成。Playbloom 捆绑包允许您在分析器 UI 中查看 Guzzle 客户端请求。集成是通过 Symfony DI 容器中的标签实现的,默认启用。目前无法禁用。然而,如果没有包含 PlaybloomGuzzleBundle 到您的项目中,它没有任何影响。