How do I use AWS SDK for Ruby with Wasabi?

AWS SDK for Ruby has been certified for use with Wasabi. 

 

To use the Ruby SDK execute the following steps:

1) Install the AWS SDK for Ruby

2) Configure additional AWS CLI profile for Wasabi account using the Wasabi keys (optional)

In this example, we have set the profile name as "wasabi" in the "~/.aws/credentials" file. To help our customers use this SDK with Wasabi, we have provided examples for both IAM and S3. This example shows: 

  1. How to set the credentials.
  2. Connect to IAM and S3 endpoints
  3. Create a user using IAM
  4. Create a Bucket
  5. Upload an Object to the Bucket
  6. Read an Object from the Bucket
  7. Delete the Object from the Bucket
  8. Other Examples

Other examples can be referred from the AWS documentation.   

Note that this example discusses the use of Wasabi's us-east-1 storage region. To use other Wasabi storage regions, please use the appropriate Wasabi service URL as described in this article

1. How to set the credentials

require 'aws-sdk'

# To use the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')

# To use Access and Secret keys directly, specify them directly in the function.

 

2.Connect to IAM and S3 endpoints

IAM:

require 'aws-sdk'

# To use the Access and Secret key from the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')


iam = Aws::IAM::Client.new(
access_key_id: credentials.credentials.access_key_id,
secret_access_key: credentials.credentials.secret_access_key,
region: 'us-east-1',
endpoint: 'https://iam.wasabisys.com'
)

# # To use Access and secret keys directly.

# iam = Aws::IAM::Client.new(
#   access_key_id: '<access-key>',
#   secret_access_key: '<secret-key>',
#   region: 'us-east-1',
#   endpoint: 'https://iam.wasabisys.com'
# )


S3:

require 'aws-sdk'

# To use the Access and Secret key from the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')

s3 = Aws::S3::Client.new(
access_key_id: credentials.credentials.access_key_id,
secret_access_key: credentials.credentials.secret_access_key,
region: 'us-east-1',
endpoint: 'https://s3.wasabisys.com'
)

# # To use Access and secret keys directly.
# s3 = Aws::S3::Client.new(
#   access_key_id: '<access-key>',
#   secret_access_key: '<secret-key>',
#   region: 'us-east-1',
#   endpoint: 'https://s3.wasabisys.com'
# )

3. Create a user using IAM

require 'aws-sdk'

# To use the Access and Secret key from the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')


iam = Aws::IAM::Client.new(
access_key_id: credentials.credentials.access_key_id,
secret_access_key: credentials.credentials.secret_access_key,
region: 'us-east-1',
endpoint: 'https://iam.wasabisys.com'
)

# # To use Access and secret keys directly.

# iam = Aws::IAM::Client.new(
#   access_key_id: '<access-key>',
#   secret_access_key: '<secret-key>',
#   region: 'us-east-1',
#   endpoint: 'https://iam.wasabisys.com'
# )

user_name = '<user-name>'

# create IAM User
iam.create_user(user_name: user_name)

4. Create a Bucket

require 'aws-sdk'

# To use the Access and Secret key from the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')

s3 = Aws::S3::Client.new(
access_key_id: credentials.credentials.access_key_id,
secret_access_key: credentials.credentials.secret_access_key,
region: 'us-east-1',
endpoint: 'https://s3.wasabisys.com'
)

# # To use Access and secret keys directly.
# s3 = Aws::S3::Client.new(
#   access_key_id: '<access-key>',
#   secret_access_key: '<secret-key>',
#   region: 'us-east-1',
#   endpoint: 'https://s3.wasabisys.com'
# )


bucket_name = '<bucket-name>'

# create a bucket
s3.create_bucket(bucket: bucket_name)

5. Upload an object to the Bucket

require 'aws-sdk'

# To use the Access and Secret key from the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')

s3 = Aws::S3::Client.new(
access_key_id: credentials.credentials.access_key_id,
secret_access_key: credentials.credentials.secret_access_key,
region: 'us-east-1',
endpoint: 'https://s3.wasabisys.com'
)

# # To use Access and secret keys directly.
# s3 = Aws::S3::Client.new(
#   access_key_id: '<access-key>',
#   secret_access_key: '<secret-key>',
#   region: 'us-east-1',
#   endpoint: 'https://s3.wasabisys.com'
# )

bucket_name = '<bucket-name>'
object_key = '<key-name>'
object_path = '<path-to-object>'


# upload to your Wasabi bucket
File.open(object_path, 'rb') do |file|
s3.put_object(bucket: bucket_name, key: object_key, body: file)
end

6. Read an object from the Bucket

require 'aws-sdk'

# To use the Access and Secret key from the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')

s3 = Aws::S3::Client.new(
access_key_id: credentials.credentials.access_key_id,
secret_access_key: credentials.credentials.secret_access_key,
region: 'us-east-1',
endpoint: 'https://s3.wasabisys.com'
)

# # To use Access and secret keys directly.
# s3 = Aws::S3::Client.new(
#   access_key_id: '<access-key>',
#   secret_access_key: '<secret-key>',
#   region: 'us-east-1',
#   endpoint: 'https://s3.wasabisys.com'
# )

bucket_name = '<bucket-name>'
object_key = '<key-name>'

# get object from your Wasabi bucket
s3.get_object(bucket: bucket_name, key: object_key)

7. Delete the object from the Bucket

require 'aws-sdk'

# To use the Access and Secret key from the aws credentials file.
credentials = Aws::SharedCredentials.new(profile_name: 'wasabi')

s3 = Aws::S3::Client.new(
access_key_id: credentials.credentials.access_key_id,
secret_access_key: credentials.credentials.secret_access_key,
region: 'us-east-1',
endpoint: 'https://s3.wasabisys.com'
)

# # To use Access and secret keys directly.
# s3 = Aws::S3::Client.new(
#   access_key_id: '<access-key>',
#   secret_access_key: '<secret-key>',
#   region: 'us-east-1',
#   endpoint: 'https://s3.wasabisys.com'
# )

bucket_name = '<bucket-name>'
object_key = '<key-name>'


# delete object from Wasabi bucket
s3.delete_object(bucket: bucket_name, key: object_key)

 

8. Other Examples

The below example shows how to create Access Keys for IAM users on Wasabi using Ruby.

require 'aws-sdk-iam'
#v2: require 'aws-sdk'

Aws::IAM::Errors::ServiceError
iam = Aws::IAM::Client.new(
{
region: 'us-east-1',
credentials: Aws::Credentials.new('Wasabi-Access-Key', 'Wasabi-Secret-Key'),
endpoint: 'https://iam.wasabisys.com'
}
)

user_name = "User-Name"
def list_keys(iam, user_name)

begin
list_access_keys_response = iam.list_access_keys({ user_name: user_name })
if list_access_keys_response.access_key_metadata.count == 0
puts"No access keys."

else

puts"Access keys:"
list_access_keys_response.access_key_metadata.each do |key_metadata|
puts" Access key ID: #{key_metadata.access_key_id}"
end
end

rescueAws::IAM::Errors::NoSuchEntity
puts"Cannot find user '#{user_name}'."
exit(false)

end
end

list_keys(iam, user_name)
puts "\nCreating access key..."

begin
iam.create_access_key({ user_name: user_name })
list_keys(iam, user_name)
rescue Aws::IAM::Errors::LimitExceeded
puts"Too many access keys. Can't create any more."

end



The below example shows how to copy object(s) between buckets using Ruby.

require 'aws-sdk-s3' 
#v2: require 'aws-sdk'

Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new('Wasabi-Access-Key', 'Wasabi-Secret-Key'),
endpoint: 'https://s3.wasabisys.com'
})

source_bucket_name = 'bucket-name-1'
target_bucket_name = 'bucket-name-2'

#note that your source and destination bucket needs to be in the same region as you cannot specify and point to two different endpoints

source_key = 'object-key'
target_key = 'object-key'
#you can either use the same name or copy them with a different name

begin
s3 = Aws::S3::Client.new(region: 'us-east-1')
s3.copy_object(bucket: target_bucket_name, copy_source: source_bucket_name + '/' + source_key, key: target_key)
rescue StandardError => ex
puts'Caught exception copying object ' + source_key + ' from bucket ' + source_bucket_name + ' to bucket ' + target_bucket_name + ' as ' + target_key + ':'
puts ex.message

end

puts 'Copied ' + source_key + ' from bucket ' + source_bucket_name + ' to bucket ' + target_bucket_name + ' as ' + target_key 

 

Have more questions? Submit a request