Skip to content

Ruby SDK for Sirv S3 - S3 API guide

By , Customer Success DirectorUpdated

Open this page in a new chat

Short answer

Use the AWS SDK for Ruby with the S3 access key, secret key and bucket from your Sirv account settings, and the endpoint s3.sirv.com. The examples on this page list buckets and folders, fetch file information, and upload, copy, move and delete files, and create folders.

After reading this you will know

  • How to connect the AWS SDK for Ruby to Sirv
  • How to upload and list files with Ruby
  • How to copy, move and delete files

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.

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://app.sirv.com/settings/uploads
'ENTER_YOUR_SIRV_S3_KEY_HERE',
'ENTER_YOUR_SIRV_S3_SECRET_HERE'
)
)
s3.buckets.limit(50).each do |b|
puts "#{b.name}"
end
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://app.sirv.com/settings/uploads
'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')
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://app.sirv.com/settings/uploads
'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')
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://app.sirv.com/settings/uploads
'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://app.sirv.com/settings/uploads
'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
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://app.sirv.com/settings/uploads
'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}"
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://app.sirv.com/settings/uploads
'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://app.sirv.com/settings/uploads
'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"))
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://app.sirv.com/settings/uploads
# 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
})
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://app.sirv.com/settings/uploads
'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://app.sirv.com/settings/uploads
'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."

Deleted files go to Trash in your Sirv account, where you can restore them for 30 days.

Back to top