Ruby SDK for Sirv S3 - S3 API guide
Quick setup: Ruby with Sirv S31. Install the AWS SDK for Ruby.2. Copy your S3 Bucket, S3 Key and S3 Secret from Account › Settings › API.3. Point the SDK at the Sirv endpoint
https://s3.sirv.com No account yet? Create one freeShort 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.
List your S3 buckets
Section titled “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://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}"endUpload file
Section titled “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://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')Create folder
Section titled “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://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')List folder contents
Section titled “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://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 filesAlternative 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 directoriesroot = client.list_objects({ prefix: '', delimiter: '/', bucket: bucket, encoding_type: 'url' })
puts root['contents'] # filesputs root['common_prefixes'] # directoriesFetch file information
Section titled “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://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}"Copy file
Section titled “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://app.sirv.com/settings/uploads 'ENTER_YOUR_SIRV_S3_KEY_HERE', 'ENTER_YOUR_SIRV_S3_SECRET_HERE' ))
# Example of source_key: Images/example-image.jpgsource_bucket_name = 'ENTER_SIRV_S3_BUCKET_HERE'target_bucket_name = 'ENTER_SIRV_S3_BUCKET_HERE'# Example of target_key: Images/exmple-image.jpgsource_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.jpgnew_object = bucket.object("ENTER_TARGET_KEY_HERE")# Example of source_key: example/folder/image.jpgaws_response = new_object.copy_from(bucket.object("ENTER_SOURCE_KEY_HERE"))Move file
Section titled “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://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.jpgsource_key = 'ENTER_SIRV_S3_SOURCE_FILE_PATH'# Example of target_key: example/folder/image.jpgtarget_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
Section titled “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://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.deleteputs "#{file_to_delete} successfully deleted."Deleted files go to Trash in your Sirv account, where you can restore them for 30 days.
