AWS SDK for Ruby With Wasabi

Prev Next

How do I use AWS SDK for Ruby with Wasabi?

AWS SDK for Ruby.

Using the Ruby SDK

Execute the following steps:

  1. Install the AWS SDK for Ruby.

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

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

  • Setting the Credentials

  • Connecting to IAM and S3 Endpoints

  • Creating a User Using IAM

  • Creating a Bucket

  • Uploading an Object to the Bucket

  • Reading an Object From the Bucket

  • Deleting an Object From the Bucket

  • Other Examples

Additional examples are available from the AWS documentation.

The examples use Wasabi's us-east-1 storage region. For other Wasabi storage regions, use the appropriate Wasabi service URL.

Send all IAM requests to iam.wasabisys.com.

Setting 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.

Connecting 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'
# )

Creating 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)

Creating 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)

Uploading 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

Reading 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)

Deleting 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>'


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

Other Examples

This 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


This 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