连接aws
After you’ve created your app with rails new <app_name> you’ll want to jump into your config/storage.yml file. There you’ll see that Amazon and other storage locations for cloud services are ready to be commented in out of the box with rails.
使用rails new <app_name>创建应用程序后,您将想要跳入config / storage.yml文件。 在那里,您将看到Amazon和其他用于云服务的存储位置已准备就绪,可以使用Rails开箱即用。
Simply uncomment the amazon part. We will come back to this later.
只需取消注释亚马逊部分。 我们稍后会再谈。
After that go into your gemfile and add gem “aws-sdk-s3”, require: false then bundle install.
之后,进入您的gemfile并添加gem“ aws-sdk-s3”,要求:false然后捆绑安装。
Next go into your config/environments folder. You’re going to do the following change for both development and production. Find where it says
接下来进入config / environments文件夹。 您将为开发和生产进行以下更改。 找到它说的地方
config.active_storage.service = :local
config.active_storage.service =:本地
And change the :local to :amazon because that’s what the space name is inside your storage.yml. (if you decided to change what was in your storage.yml file to bubble_gum that you’d set your active_storage.service to :bubble_gum)
并将:local更改为:amazon,因为这就是storage.yml中的空间名称。 (如果您决定将storage.yml文件中的内容更改为bubble_gum,则将active_storage.service设置为:bubble_gum)
Okay now we need to hop over to the AWS site: https://aws.amazon.com/
好的,现在我们需要跳到AWS站点: https : //aws.amazon.com/
If you don’t already have an account go ahead and make one. By clicking the orange “Create an AWS Account” button.
如果您还没有帐户,请继续注册。 单击橙色的“创建AWS账户”按钮。
Fill in your information on the next page. All fields are required.
在下一页上填写您的信息。 各个领域都需要。
WARNING WARNING!!
警告警告!
You will need to add a credit card to your account, however Amazon only charges you for downloading from the storage and it’s absurdly cheap. Even if you used it regularly to maintain a database it might run you $30 a year.
您将需要在您的帐户中添加信用卡,但是Amazon仅向您收取从存储区下载的费用,而且价格便宜得荒唐。 即使您定期使用它来维护数据库,也可能每年要花$ 30。
Continue on with the registration…
继续进行注册…
select the Free Plan
选择免费计划
Now that orange button says “Sign in to the Console”…Go ahead and do that. Select Root user.
现在,橙色按钮显示“登录到控制台” ...继续进行操作。 选择“ root用户”。
Enter s3 into the Find Services field. Select s3.
在“查找服务”字段中输入s3。 选择s3。
You’ll see a dashboard that looks something like this:
您会看到一个仪表盘,看起来像这样:
Click the Create bucket button.
点击创建存储区按钮。
Give the bucket a name. Note that bucket names must be unique across all bucket names so you may have to get creative.
给桶命名。 请注意,存储桶名称在所有存储桶名称中必须唯一,因此您可能必须具有创造力。
If you’re in NYC like I am you’re going to want to pick US East (Virginia) I promise that is much closer than Ohio, which is the other tempting server location. You can check out more about the AWS global infrastructure and find the closest server to you here.
如果您像我一样在纽约,您将要选择US East(Virginia),我保证那将比俄亥俄州(Ohio)更近,后者是另一个诱人的服务器位置。 您可以在此处查看有关AWS全局基础架构的更多信息,并找到离您最近的服务器。
Hit next to continue. You can decide if you want to add any of the options you see but it’s fine to just click next again.
点击下一步继续。 您可以决定是否要添加看到的任何选项,但是再次单击下一步就可以了。
The Public Access on the next page is set to blocked. This is something you can change later. If you’re just testing things out with your app you can uncheck.
下一页的“公共访问”设置为“阻止”。 您稍后可以更改。 如果您只是通过应用程序进行测试,则可以取消选中。
Then create the bucket! You should see your bucket name on the dashboard now.
然后创建存储桶! 您现在应该在信息中心上看到您的存储桶名称。
Okay, the next Step is to create an IAM user. Click on the Services dropdown near the top of your screen. Type in IAM into the search bar and select IAM — manage access to AWS resources. You’ll be brought to a new dashboard. On the left-side menu select Users. Then click the Add user button.
好的,下一步是创建IAM用户。 点击屏幕顶部附近的服务下拉菜单。 在搜索栏中输入IAM并选择IAM-管理对AWS资源的访问。 您将被带到新的仪表板。 在左侧菜单上,选择“用户”。 然后单击添加用户按钮。
Set a name for the user and give it programmatic access
为用户设置一个名称并授予其编程访问权限
Click next then select the Attach existing policies directly. Now let me save you hours of confusing research and tell you that IAMFullAccess will work well. Just search for that and select it and hit next and next again then Create user.
单击下一步,然后选择直接附加现有策略。 现在,让我为您节省数小时的混乱研究,并告诉您IAMFullAccess将很好地工作。 只需搜索并选择它,然后单击下一步,然后单击创建用户。
This is a VERY IMPORTANT page! DO NOT lose the access key numbers. (also don’t post them online unless you plan on deleting them right away)
这是一个非常重要的页面! 不要丢失访问密钥号。 (也不要在线发布它们,除非您打算立即删除它们)
Download the .csv and keep it somewhere safe where you can access it. You’ll need these to complete the very first step of this tutorial.
下载.csv并将其保存在可以访问它的安全位置。 您将需要这些来完成本教程的第一步。
Phew, done with that!! Now back to our Rails app. Now we don’t want to put these secret access keys anywhere that would be loaded up to Github.
ew,做完了! 现在回到我们的Rails应用程序。 现在,我们不想将这些秘密访问密钥放在可以加载到Github的任何位置。
There are two easy options to deal with this.
有两个简单的选项可以解决这个问题。
One is to make some environmental variables in a .env file then add .env to the gitignore page. The other way is to use the new rails credentials.yml.enc.
一种是在.env文件中创建一些环境变量,然后将.env添加到gitignore页面。 另一种方法是使用新的rails certificate.yml.enc。
The new rails credentials feature is a neat idea but if you’re new all this and you think you might run into bugs (you will run into bugs) I’d recommend the former option because it’s a pain to redo the credentials over and over if you change your IAM user or start your app over. Personally I had a bunch of issues with this part and it finally worked the one time I used env variables so I love them now even if they don’t deserve the credit.
新的rails凭证功能是一个很好的主意,但是如果您是所有新手,并且认为您可能会遇到bug(您会遇到bug),我建议您使用前一个选项,因为一遍又一遍地重做凭证很麻烦如果您更改IAM用户或重新启动应用程序。 就我个人而言,这部分存在很多问题,并且在我使用env变量的那一次终于奏效了,所以即使它们不值得一试,我现在也喜欢它们。
I’ll quickly explain how to set up the credentials if you want to check that out.
我将快速解释如果您想签出凭证,如何设置凭证。
Type into your console EDITOR=vim rails credentials:edit
在控制台中输入EDITOR = vim rails certificate:edit
You can set your favorite editor. I’ll give you the TLDR on the cute little VIM tutorial that I found after button mashing until I discovered that : was the command to start typing commands… I actually read the whole :help document too. (quit command lets your skip it).
您可以设置自己喜欢的编辑器。 我将在按钮混搭后发现的可爱的VIM小教程中为您提供TLDR,直到我发现:是开始键入命令的命令…实际上我也阅读了整个:help文档。 (退出命令可让您跳过它)。
Okay so how do you edit this thing? Type the letter i to enter edit mode. Press the esc key to exit edit mode. To save enter :w to quit enter :q
好的,那你怎么编辑这个东西? 输入字母i进入编辑模式。 按esc键退出编辑模式。 要保存,请输入:w退出,请输入:q
Once in edit mode copy in your access key and secret. You can also add in your region and bucket name if you want to keep those confidential too.
一旦进入编辑模式,请复制您的访问密钥和机密。 如果您也想保密的话,也可以添加您的地区和存储桶名称。
Then you would go into the storage.yml file and set it up to look like this:
然后,您将进入storage.yml文件并将其设置为如下所示:
Alright, now the other option ENV variables (my personal preference) !!
好了,现在另一个选项是ENV变量(我个人的喜好)!
Create a file called .env in your app directory. So that would be app/.env
在您的应用目录中创建一个名为.env的文件。 这样就可以是app / .env
Create a variable for your key and your secret.
为您的密钥和秘密创建一个变量。
Go to your storage.yml file and change it to look like:
转到您的storage.yml文件并将其更改为如下所示:
AND FINALLY!! DO NOT FORGET to add .env to your git ignore file.
最后!! 不要忘记将.env添加到您的git忽略文件中。
Just throw it in at the bottom as .env and you’re all set.
只需将其作为.env放在底部,就可以了。
That’s all there is to it. Good luck and happy coding.
这里的所有都是它的。 祝您好运,编码愉快。
翻译自: https://medium.com/@andrewjames_3104/connecting-aws-to-your-rails-app-184abfc44744
连接aws
相关资源:SlideHub: SlideHub是Azure AWS的开源幻灯片共享应用程序-源码