Ruby SDK for Sirv S3

On this page

Use the well documented AWS SDK for Ruby to upload and manage your images on Sirv.

Start by trying the Ruby code examples below. Replace the capitalized items with your Sirv S3 access key, secret key and bucket from your Sirv account Settings page.

List your S3 buckets

require 'aws-sdk'

s3 = Aws::S3::Resource.new(
  :endpoint => 'https://s3.sirv.com',
  :region => 'sirv',
  :force_path_style => true,
  :signature_version => 's3',
  :credentials => Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
    'ENTER_YOUR_SIRV_S3_KEY_HERE',
    'ENTER_YOUR_SIRV_S3_SECRET_HERE'
  )
)
s3.buckets.limit(50).each do |b|
  puts "#{b.name}"
end

Upload file

require 'aws-sdk'

s3 = Aws::S3::Resource.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

# Example of SIRV_S3_FILE_PATH: 'example/folder/image.jpg'
# Example of LOCAL_FILE_PATH: 'local/folder/image.jpg'
s3.bucket('ENTER_YOUR_SIRV_S3_BUCKET_HERE').object('ENTER_SIRV_S3_FILE_PATH').upload_file('ENTER_LOCAL_FILE_PATH')

Create folder

require 'aws-sdk'

s3 = Aws::S3::Client.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

# Example of SIRV_S3_FOLDER_PATH: 'folder/'
s3.put_object(bucket: 'ENTER_SIRV_S3_BUCKET_HERE', key: 'ENTER_SIRV_S3_FOLDER_PATH')

List folder contents

require 'aws-sdk'

s3 = Aws::S3::Client.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

object_list = s3.list_objects(bucket: 'ENTER_YOUR_SIRV_S3_BUCKET_HERE', prefix: '/')

puts object_list.common_prefixes #<——— to get a list of “folders”
puts object_list.contents #<——— to get a list of files

Alternative folder content listing method:

require 'aws-sdk'

s3 = Aws::S3::Resource.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

bucket = 'ENTER_YOUR_SIRV_S3_BUCKET_HERE'
client = s3.client

# Root files and directories
root = client.list_objects({
                               prefix: '',
                               delimiter: '/',
                               bucket: bucket,
                               encoding_type: 'url'
                           })

puts root['contents']        # files
puts root['common_prefixes'] # directories

Fetch file information

require 'aws-sdk'

s3 = Aws::S3::Resource.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

# Example of SIRV_S3_FILE_PATH: 'example/folder/image.jpg'
obj = s3.bucket('ENTER_YOUR_SIRV_S3_BUCKET_HERE').object('ENTER_SIRV_S3_FILE_PATH')
puts "Object content length is #{obj.content_length}"
puts "Object was last modified at #{obj.last_modified}"

Copy file

require 'aws-sdk'

s3 = Aws::S3::Client.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

# Example of source_key: Images/example-image.jpg
source_bucket_name = 'ENTER_SIRV_S3_BUCKET_HERE'
target_bucket_name = 'ENTER_SIRV_S3_BUCKET_HERE'
# Example of target_key: Images/exmple-image.jpg
source_key = 'ENTER_SOURCE_KEY_HERE'
target_key = 'ENTER_TARGET_KEY_HERE'

s3.copy_object({bucket: target_bucket_name, copy_source: source_bucket_name + '/' + source_key, key: target_key})

puts "Copying file #{source_key} to #{target_key}."

Alternative copy method:

require 'aws-sdk'

bucket = Aws::S3::Bucket.new(
    name: 'ENTER_SIRV_S3_BUCKET_HERE',
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

# Example of target_key: example/folder/image.jpg
new_object = bucket.object("ENTER_TARGET_KEY_HERE")
# Example of source_key: example/folder/image.jpg
aws_response = new_object.copy_from(bucket.object("ENTER_SOURCE_KEY_HERE"))

Move file

require 'aws-sdk'

s3 = Aws::S3::Client.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
# S3 Access Key
     'ENTER_YOUR_SIRV_S3_KEY_HERE',
# S3 Secret Key
     'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

source_bucket_name = 'ENTER_YOUR_SIRV_S3_BUCKET_HERE'
target_bucket_name = 'ENTER_YOUR_SIRV_S3_BUCKET_HERE'
# Example of source_key: example/folder/image.jpg
source_key = 'ENTER_SIRV_S3_SOURCE_FILE_PATH'
# Example of target_key: example/folder/image.jpg
target_key = 'ENTER_SIRV_S3_TARGET_FILE_PATH'

s3.copy_object({bucket: target_bucket_name, copy_source: source_bucket_name + '/' + source_key, key: target_key})

 s3.delete_object({
    bucket: source_bucket_name,
    key: source_key
})

Delete file

require 'aws-sdk'

s3 = Aws::S3::Client.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

s3.delete_objects(
    bucket: 'ENTER_YOUR_SIRV_S3_BUCKET_HERE',
    delete: {
        objects: [
            {
# Example of SIRV_S3_FILE_PATH: 'example/folder/image.jpg'
                key: 'ENTER_SIRV_S3_FILE_PATH'
            }
        ]
    }
)

Alternative delete method:

require 'aws-sdk'

s3 = Aws::S3::Resource.new(
    endpoint: 'https://s3.sirv.com',
    region: 'sirv',
    force_path_style: true,
    signature_version: 's3',
    credentials: Aws::Credentials.new(
# Enter your Sirv S3 access key & secret key from https://my.sirv.com/#/account/settings
        'ENTER_YOUR_SIRV_S3_KEY_HERE',
        'ENTER_YOUR_SIRV_S3_SECRET_HERE'
    )
)

bucket = s3.bucket('ENTER_YOUR_SIRV_S3_BUCKET_HERE')
# Example of SIRV_S3_FILE_PATH: 'example/folder/image.jpg'
file_to_delete = "ENTER_SIRV_S3_FILE_PATH"
obj = bucket.object("#{file_to_delete}")
obj.delete
puts "#{file_to_delete} successfully deleted."

Was this article helpful?

Array

Get help from a Sirv expert

help ukraine help ukraine Powered by Ukrainian determination and British ingenuity

How can you support Ukraine