require "aws-sdk-s3"
require "net/http"
require "securerandom"
require "json"
# Create a new S3 resource
telnyx_api_key = ENV["TELNYX_API_KEY"]
resource = Aws::S3::Resource.new(
region: "us-central-1",
endpoint: "https://us-central-1.telnyxcloudstorage.com",
access_key_id: telnyx_api_key,
secret_access_key: "doesn't matter"
)
bucket_name = "example-#{SecureRandom.hex(24)}"
# Creating a bucket
bucket = resource.create_bucket(bucket: bucket_name)
puts "*" * 50
puts("Created bucket named #{bucket.name}.")
puts "*" * 50
# Listing buckets
puts "Listing buckets"
puts "*" * 50
resource.buckets.each do |b|
puts " - #{b.name}"
end
puts "*" * 50
# Upload a file
File.open("document.txt", "w+") { |f| f.write("This is a text document.\n") }
bucket = resource.bucket(bucket_name)
the_object = bucket.object("document.txt")
the_object.upload_file("document.txt")
puts("Uploaded file document.txt into bucket #{bucket.name} with key #{the_object.key}.")
puts "*" * 50
# Download an object from the bucket
puts "Downloading object from bucket #{bucket.name}"
file_name = "a-local-file.txt"
the_object.download_file(file_name)
puts("Object #{the_object.key} successfully downloaded to #{file_name}.")
puts("Contents of #{file_name}: #{File.read(file_name).inspect}")
puts "*" * 50
# Creating a presigned URL to upload a file
object_key = "important-document.txt"
uri = URI("https://api.telnyx.com/v2/storage/buckets/#{bucket_name}/#{object_key}/presigned_url")
response = Net::HTTP.post(
uri,
{ ttl: 30 }.to_json,
"Authorization" => "Bearer #{telnyx_api_key}"
)
raise "Bad response creating presigned URL" unless response.code == "200"
parsed_response = JSON.parse(response.body)
presigned_url = parsed_response["data"]["presigned_url"]
puts "Created presigned upload URL:"
puts "*" * 50
puts presigned_url
puts "*" * 50
# Upload a file using the Telnyx presigned URL
uri = URI(presigned_url)
request = Net::HTTP.new(uri.host)
response = request.put(uri, "This is an important text document.\n")
raise "Couldn't upload file using presigned URL: #{response.inspect}" unless response.code == "200"
puts "Uploaded file using presigned URL: #{response.inspect}"
# Listing objects in the bucket
puts "Listing objects in bucket #{bucket.name}"
puts "*" * 50
bucket.objects.each do |o|
puts " * #{o.key}"
end
puts "*" * 50
# Creating a presigned URL to download the file
uri = URI("https://api.telnyx.com/v2/storage/buckets/#{bucket_name}/#{object_key}/presigned_url")
response = Net::HTTP.post(
uri,
{ ttl: 30 }.to_json,
"Authorization" => "Bearer #{telnyx_api_key}"
)
raise "Bad response creating presigned URL" unless response.code == "200"
parsed_response = JSON.parse(response.body)
presigned_url = parsed_response["data"]["presigned_url"]
puts "Created presigned download URL:"
puts "*" * 50
puts presigned_url
puts "*" * 50
# Download the object using the presigned URL
uri = URI(presigned_url)
response = Net::HTTP.get(uri)
puts("Downloaded object contents from presigned URL: #{response.inspect}")
puts "*" * 50
# Cleaning up
File.delete("document.txt")
File.delete("a-local-file.txt")
# Deleting the objects
the_object.delete
puts("Deleted object #{the_object.key}.")
important_object = bucket.object(object_key)
important_object.delete
puts("Deleted object #{important_object.key}.")
# Deleting the bucket
bucket.delete
puts("Deleted bucket #{bucket.name}.")
puts("Done.")
AWS Ruby SDK Example
Use the AWS SDK for Ruby with Telnyx Cloud Storage. Configure the S3-compatible endpoint and upload, download, and list objects from Ruby applications.
The following example shows how AWS Ruby SDK can be used to interact with Telnyx Cloud Storage.
Was this page helpful?