Using Object Storage with Ruby

Hey there! If you want to test Object Storage in Ruby, I built this lightweight demo script to help people get started. This uses the existing AWS S3 SDK, with a slight configuration tweak to connect to Object Storage.

When creating the client object, I needed to add force_path_style: true to the params.

But, that one-line adjustment is the only change required to do to make everything work! :magic_wand:

require 'aws-sdk-s3'

# Define testing variables for bucket and object creation
bucketName = "my-new-bucket"
objectKey = "test-key"
content = "test-body"

# Create an S3 client

@object_store = Aws::S3::Client.new(
  access_key_id: "#{KEY}",
  secret_access_key: "#{SECRET}",
  
  # This example assumes you're working out of Fastly's US West region.
  # Change to another supported region as desired.
  endpoint: "https://us-west.object.fastlystorage.app",
  region: "us-west",
  
  # This param instructs the client to connect to Fastly Object Storage
  # via the endpoint path defined above.
  force_path_style: true
)

#####################
# Test Interactions #
#####################

# Create a bucket
puts @object_store.create_bucket(bucket: bucketName)

# List all buckets
puts @object_store.list_buckets

# Get the location for the newly-created bucket
puts @object_store.get_bucket_location(bucket: bucketName)

# Write a test object to the newly-create bucket
puts @object_store.put_object(bucket: bucketName, key: objectKey, body: content)

# List the first 5 objects in the newly-created bucket
puts @object_store.list_objects(bucket: bucketName, max_keys: 5)
1 Like