2014-12-30 17:20:57 -05:00
# Fog::Aws
2015-01-05 13:35:35 -05:00
![Gem Version ](https://badge.fury.io/rb/fog-aws.svg )
2021-03-21 11:03:23 -04:00
[![Build Status ](https://github.com/fog/fog-aws/actions/workflows/ruby.yml/badge.svg )](https://github.com/fog/fog-aws/workflows/ruby.yml)
2015-01-05 13:35:35 -05:00
[![Test Coverage ](https://codeclimate.com/github/fog/fog-aws/badges/coverage.svg )](https://codeclimate.com/github/fog/fog-aws)
2016-03-02 14:33:41 -05:00
[![Code Climate ](https://codeclimate.com/github/fog/fog-aws.svg )](https://codeclimate.com/github/fog/fog-aws)
2015-01-02 14:00:04 -05:00
2014-12-30 17:20:57 -05:00
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'fog-aws'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install fog-aws
## Usage
2015-04-13 17:29:11 -04:00
Before you can use fog-aws, you must require it in your application:
```ruby
require 'fog/aws'
```
Since it's a bad practice to have your credentials in source code, you should load them from default fog configuration file: ```~/.fog```. This file could look like this:
```
default:
2015-04-14 10:09:35 -04:00
aws_access_key_id: < YOUR_ACCESS_KEY_ID >
aws_secret_access_key: < YOUR_SECRET_ACCESS_KEY >
2015-04-13 17:29:11 -04:00
```
2019-11-11 16:09:09 -05:00
### EC2
#### Connecting to the EC2 Service:
2015-04-13 17:29:11 -04:00
```ruby
ec2 = Fog::Compute.new :provider => 'AWS', :region => 'us-west-2'
```
You can review all the requests available with this service using ```#requests``` method:
```ruby
ec2.requests # => [:allocate_address, :assign_private_ip_addresses, :associate_address, ...]
```
2019-11-11 16:09:09 -05:00
#### Launch an EC2 on-demand instance:
2015-04-13 17:29:11 -04:00
```ruby
response = ec2.run_instances(
"ami-23ebb513",
1,
1,
"InstanceType" => "t1.micro",
"SecurityGroup" => "ssh",
"KeyName" => "miguel"
)
instance_id = response.body["instancesSet"].first["instanceId"] # => "i-02db5af4"
instance = ec2.servers.get(instance_id)
instance.wait_for { ready? }
2015-04-14 10:09:35 -04:00
puts instance.public_ip_address # => "356.300.501.20"
2015-04-13 17:29:11 -04:00
```
2019-11-11 16:09:09 -05:00
#### Terminate an EC2 instance:
2015-04-13 17:29:11 -04:00
```ruby
instance = ec2.servers.get("i-02db5af4")
instance.destroy
```
2019-11-11 16:09:40 -05:00
`Fog::AWS` is more than EC2 since it supports many services provided by AWS. The best way to learn and to know about how many services are supported is to take a look at the source code. To review the tests directory and to play with the library in ```bin/console``` can be very helpful resources as well.
2019-11-11 16:09:09 -05:00
### S3
#### Connecting to the S3 Service:
```ruby
s3 = Fog::Storage.new(provider: 'AWS', region: 'eu-central-1')
```
#### Creating a file:
```ruby
directory = s3.directories.new(key: 'gaudi-portal-dev')
file = directory.files.create(key: 'user/1/Gemfile', body: File.open('Gemfile'), tags: 'Org-Id=1& Service-Name=My-Service')
```
#### Listing files:
```ruby
directory = s3.directories.get('gaudi-portal-dev', prefix: 'user/1/')
directory.files
```
#### Generating a URL for a file:
```ruby
directory.files.new(key: 'user/1/Gemfile').url(Time.now + 60)
```
2014-12-30 17:20:57 -05:00
2020-11-19 15:14:20 -05:00
#### Copying a file
```ruby
directory = s3.directories.new(key: 'gaudi-portal-dev')
file = directory.files.get('user/1/Gemfile')
file.copy("target-bucket", "user/2/Gemfile.copy")
```
To speed transfers of large files, the `concurrency` option can be used
to spawn multiple threads. Note that the file must be at least 5 MB for
multipart uploads to work. For example:
```ruby
directory = s3.directories.new(key: 'gaudi-portal-dev')
file = directory.files.get('user/1/Gemfile')
file.multipart_chunk_size = 10 * 1024 * 1024
file.concurrency = 10
file.copy("target-bucket", "user/2/Gemfile.copy")
```
2018-03-14 17:32:01 -04:00
## Documentation
See the [online documentation ](http://www.rubydoc.info/github/fog/fog-aws ) for a complete API reference.
2019-11-11 16:09:40 -05:00
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
2014-12-30 17:20:57 -05:00
## Contributing
2015-04-17 05:36:56 -04:00
1. Fork it ( https://github.com/fog/fog-aws/fork )
2014-12-30 17:20:57 -05:00
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request