ruby aws sns_使用适用于Ruby的AWS开发工具包-第一部分

ruby aws sns

In this post, I’ll explain some of the concepts around the AWS SDK for Ruby that I found hard to understand at the beginning like majors SDK versions, what’s the difference between the Client and Resource objects or how to “stub” requests for the Client so we can add tests to our code using the AWS SDK for Ruby.

在这篇文章中,我将解释我一开始就难以理解的,适用于Ruby的AWS开发工具包(SDK)的一些概念,例如专业SDK版本, 客户端对象和资源对象之间的区别是什么,或者如何对请求的“存根”。客户端,因此我们可以使用适用于Ruby的AWS开发工具包将测试添加到代码中。

The AWS SDK for Ruby is an alternative to manage resources and interact with your AWS account using the popular — or maybe not so much nowadays 🙃 — Ruby programming language. It offers an abstraction for different AWS services like S3, EC2, DynamoDB, etc, through a Ruby idiomatic wrapper using classic Object-Oriented Programming (OOP) paradigm. There are other AWS SDK implementations for different programming languages like C++, Go, Java, JavaScript, .NET, Node.js, PHP, and Python.

适用于Ruby的AWS开发工具包是一种管理资源并使用流行的Ruby编程语言(或现在不那么流行)与您的AWS账户进行交互的替代方法。 它通过使用经典的面向对象编程(OOP)范例的Ruby惯用包装器,为S3,EC2,DynamoDB等不同的AWS服务提供了抽象。 还有其他针对不同编程语言的AWS开发工具包实现,例如C ++,Go,Java,JavaScript,.NET,Node.js,PHP和Python。

组态 (Configuration)

The AWS CLI and SDK uses the same credentials mechanism, environment variables, name profile, etc. More info at AWS documentation. If you already have configured the AWS CLI on your local environment, the SDK will reuse that config.

AWS CLI和SDK使用相同的凭证机制,环境变量,名称配置文件等。有关更多信息,请参阅AWS文档 。 如果您已经在本地环境中配置了AWS CLI,则SDK将重复使用该配置。

You just need to install the EC2 aws-sdk-ec2 specific gem that we're going to use in the examples below. table_print will be used as an auxiliary gem to easily print Ruby objects:

您只需要安装特定于EC2 aws-sdk-ec2 gem,我们将在以下示例中使用它。 table_print将用作辅助gem,以轻松打印Ruby对象:

gem install aws-sdk-ec2 table_print

aws-sdk-ruby (aws-sdk-ruby)

The aws-sdk-ruby got split into several ruby gems at version 3, each gem matches specific AWS services: aws-sdk-s3 for S3, aws-sdk-ec2 for EC2, and so on. The following code snippet prints the ids, instance_types and the state for EC2 instances in the us-west-2 region:

在版本3中, aws-sdk-ruby被拆分为多个Ruby,每个宝石都与特定的AWS服务匹配:用于S3的aws-sdk-s3 ,用于EC2的aws-sdk-ec2等。 以下代码段显示us-west-2区域中EC2实例的idsinstance_typesstate

ID                  | TYPE       | STATE  
--------------------|------------|--------
i-0c2d688b44c5a6324 | t2.micro | stopped
i-04e764ff7cc2f9f33 | t2.micro | running
i-0fe8144847b3c49ac | c5.2xlarge | running

版本号 (Versions)

This was really misleading at the beginning for me because I found a lot of examples on the Internet that didn’t use version 3, or didn’t explain the differences between major versions.

在一开始,这确实使我产生了误解,因为我在Internet上发现了很多不使用版本3或没有解释主要版本之间的差异的示例。

There are three major versions, our main focus today is version 3, which is the latest. How do we know if we’re using the SDK version 3? Well, If you’re using a specific AWS service require as I did in the previous code snippet for EC2 (aws-sdk-ec2) then you're using the SDK version 3:

主要有三个版本,今天我们主要关注的是最新的版本3。 我们如何知道我们是否使用的是SDK版本3? 好吧,如果您使用的是特定的AWS服务require就像我在上一个EC2代码片段( aws-sdk-ec2 )中所做的那样,那么您正在使用SDK版本3:

require 'aws-sdk-ec2'
require 'aws-sdk-s3'
require 'aws-sdk-sqs'

Requiring specific gems for AWS services wasn’t possible before version 3, this version modularizes the monolithic SDK into multiple specific gems, and each gem contains both client and resource interfaces. There is a really interesting post about it in the Ruby AWS blog that explains all the motivations for this change:

在版本3之前,不可能为AWS服务要求特定的gem,该版本将单片SDK模块化为多个特定的gem,并且每个gem都包含客户端和资源接口。 Ruby AWS博客中有一篇非常有趣的文章,解释了此更改的所有动机:

Image for post
AWS SDK for Ruby major versions
适用于Ruby的AWS开发工具包主要版本

替换 (REPL)

The AWS SDK has an interactive command line (REPL) aws-v3.rb. AWS documentation mentions that the aws-sdk-core gem contains the REPL, but actually it's inside the aws-sdk-resources gem, which actually installs all the aws-sdk-* gems 👀

AWS开发工具包具有交互式命令行(REPL) aws-v3.rbAWS文档提到aws-sdk-core gem包含REPL,但实际上它位于aws-sdk-resources gem内部 ,实际上安装了所有aws-sdk-* gems👀

gem install aws-sdk-resources
Fetching aws-sdk-acmpca-1.26.0.gem Fetching aws-sdk-acm-1.34.0.gem Fetching aws-sdk-amplify-1.21.0.gem Fetching aws-sdk-apigatewaymanagementapi-1.16.0.gem Fetching aws-sdk-accessanalyzer-1.9.0.gem
...
233 gems installed

aws-v3.rb enables us to quickly test and use the AWS SDK in an interactive console. We can use the SDK REPL to execute the same previous example:

aws-v3.rb使我们能够在交互式控制台中快速测试和使用AWS开发工具包。 我们可以使用SDK REPL执行相同的先前示例:

aws-v3.rb
Pry not available, falling back to irb
2.7.1 :001 > require 'table_print'
=> true
2.7.1 :002 > ec2 = Aws::EC2::Resource.new
2.7.1 :003 > tp ec2.instances.map{|i|{id: i.instance_id, type: i.instance_type, state: i.sta
te.name}}
[Aws::EC2::Client 200 0.857933 0 retries] describe_instances()
ID | TYPE | STATE
--------------------|------------|--------
i-0c2d688b44c5a6324 | t2.micro | stopped
i-04e764ff7cc2f9f33 | t2.micro | running
i-0fe8144847b3c49ac | c5.2xlarge | running

In this case, we don’t need to require the aws-sdk-ec2 because the SDK REPL requires the whole SDK inside the irb session (or Pry).

在这种情况下,我们不需要aws-sdk-ec2因为SDK REPL在irb会话(或Pry )内需要整个SDK。

As I mentioned before, each gem contains client and resource interfaces. We’re only going to talk about the aws-sdk-ec2 today but the concepts apply to other services/gems.

如前所述,每个gem都包含客户端和资源接口。 今天我们仅讨论aws-sdk-ec2 ,但是这些概念适用于其他服务/宝石。

客户 (Client)

The Client is the basic object and represents an API client for EC2. It has a 1-to-1 relationship with actual AWS API calls. If you have ever use the AWS CLI, you can found some similarities. Let’s see for example the EC2 DescribeInstances API, using the AWS CLI you’ll make something like this:

客户端是基本对象,代表EC2的API客户端。 它与实际的AWS API调用具有一对一的关系。 如果您曾经使用过AWS CLI,则会发现一些相似之处。 例如,让我们看一下EC2 DescribeInstances API ,使用AWS CLI进行如下操作:

aws ec2 describe-instances

And you’ll get an output like this:

然后您将获得如下输出:

{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
...

If you want to make the same API Call using the SDK for Ruby you’ll be doing something like this using the REPL:

如果您想使用Ruby的SDK进行相同的API调用,则可以使用REPL进行如下操作:

[1] pry(Aws)> aws_ec2 = Aws::EC2::Client.new                                         
=> #<Aws::EC2::Client>
[2] pry(Aws)> aws_ec2.describe_instances
[3] pry(Aws)> aws_ec2.describe_instances
[Aws::EC2::Client 200 0.886631 0 retries] describe_instances()
=> #<struct Aws::EC2::Types::DescribeInstancesResult
reservations=
[#<struct Aws::EC2::Types::Reservation
groups=[],
instances=
[#<struct Aws::EC2::Types::Instance
ami_launch_index=0,

As you can see, in practice, the returning info is the same. JSON for the CLI and a Ruby object representation for the same info in the SDK. So, you can consider the Client (Aws::EC2::Client) object as the Ruby implementation for the aws ec2 option in the AWS CLI.

如您所见,实际上返回的信息是相同的。 CLI的JSON和SDK中相同信息的Ruby对象表示。 因此,您可以将Client( Aws::EC2::Client )对象视为AWS CLI中aws ec2选项的Ruby实现。

But if we’re using the AWS SDK, what we’re actually looking for, are higher levels of abstractions for AWS Services.

但是,如果我们使用的是AWS开发工具包,那么我们真正要寻找的是更高层次的AWS服务抽象。

资源资源 (Resources)

Resources (e.g. Aws::EC2::Vpc, Aws::EC2::Instance, etc) are Ruby OOP representations for AWS Services and each of those resources implements internally the Client (Aws::EC2::Client), this is really important to understand, the resources use the Client in the background to make AWS API calls. If we keep this in mind, it will make our life easier in the next post of this series when we talk about how to use waiters and how to stub the Client API calls.

资源(例如Aws::EC2::VpcAws::EC2::Instance等)是AWS服务的Ruby OOP表示形式,并且这些资源中的每一个都在内部实现了Client( Aws::EC2::Client ),这确实是重要的是要理解,资源在后台使用Client进行AWS API调用。 如果我们牢记这一点,那么当我们谈论如何使用服务员以及如何对客户端API调用进行存根时,这将使本系列的下一篇文章更加轻松。

The Aws::EC2::Resource class is built on top of other Resource definitions like Aws::EC2::Instance, Aws::EC2::InternetGateway, etc. It doesn't act as a base class for the other more specific resources, it provides a resource-oriented interface for EC2.

Aws::EC2::Resource类是在其他资源定义(如Aws::EC2::InstanceAws::EC2::InternetGateway等)的基础上构建的。它不充当其他更具体的基类资源,它为EC2提供了一个面向资源的接口。

But that’s enough talking, let’s see an example to fully understand the difference between these concepts inside the AWS SDK for Ruby.

但这已经足够了,让我们看一个示例,以充分了解适用于Ruby的AWS开发工具包中这些概念之间的区别。

例子 (Examples)

So, What if we want to list EC2 instances? Let’s see which options do we have inside the aws-sdk-ec2 gem. The output for all examples is the same:

那么,如果我们要列出EC2实例怎么办? 让我们看看aws-sdk-ec2 gem中有哪些选项。 所有示例的输出是相同的:

ID                  | TYPE       | STATE  
--------------------|------------|--------
i-0c2d688b44c5a6324 | t2.micro | stopped
i-04e764ff7cc2f9f33 | t2.micro | running
i-0fe8144847b3c49ac | c5.2xlarge | running

客户Aws::EC2::Client (Client Aws::EC2::Client)

资源Aws::EC2::Resource (Resource Aws::EC2::Resource)

资源VPC Aws::EC2::Vpc (Resource VPC Aws::EC2::Vpc)

The important thing here is that even the Aws::EC2::Resource and Aws::EC2::Vpc options are using the Client Aws::EC2::Client in the background to trigger the EC2 DescribeInstances API call.

这里重要的是,即使Aws::EC2::ResourceAws::EC2::Vpc选项也在后台使用Client Aws::EC2::Client来触发EC2 DescribeInstances API调用。

For example, this is the actual code for AWS::EC2::Vpc.instances method:

例如,这是AWS::EC2::Vpc.instances方法的实际代码

As we can see at this line:

正如我们在这一行看到的:

resp = @client.describe_instances(options)

The #instances method from Aws::EC2::Vpc class it's just an abstraction for the #describe_instances method at Aws::EC2::Client.

#instances Aws::EC2::Vpc类中的#describe_instances方法只是#describe_instances Aws::EC2::Client #describe_instances方法的抽象。

翻译自: https://medium.com/@pabloxio/working-with-the-aws-sdk-for-ruby-part-i-fb1ba48b15c6

ruby aws sns

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值