Class: BucketSdk::Client
- Inherits:
-
Object
- Object
- BucketSdk::Client
- Defined in:
- lib/bucket_sdk/client.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#initialize(base_url:, timeout: 60) ⇒ Client
constructor
A new instance of Client.
-
#list_objects(recurse: false) ⇒ Models::ListResponse
List objects in the bucket.
-
#upload_object(file:, destination:) ⇒ Models::LoadResponse
Upload an object to the bucket.
Constructor Details
#initialize(base_url:, timeout: 60) ⇒ Client
Returns a new instance of Client.
12 13 14 15 |
# File 'lib/bucket_sdk/client.rb', line 12 def initialize(base_url:, timeout: 60) @base_url = base_url @timeout = timeout end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
10 11 12 |
# File 'lib/bucket_sdk/client.rb', line 10 def base_url @base_url end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
10 11 12 |
# File 'lib/bucket_sdk/client.rb', line 10 def timeout @timeout end |
Instance Method Details
#list_objects(recurse: false) ⇒ Models::ListResponse
List objects in the bucket
47 48 49 50 51 52 53 |
# File 'lib/bucket_sdk/client.rb', line 47 def list_objects(recurse: false) response = connection.get("/api/v2/objects") do |req| req.params[:recurse] = recurse end handle_response(response, BucketSdk::Models::ListResponse) end |
#upload_object(file:, destination:) ⇒ Models::LoadResponse
Upload an object to the bucket
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bucket_sdk/client.rb', line 21 def upload_object(file:, destination:) response = connection.post("/api/v2/objects") do |req| req..timeout = timeout # Set up multipart form data payload = {} if file.is_a?(File) || file.is_a?(Tempfile) payload[:file] = Faraday::Multipart::FilePart.new(file, "application/octet-stream") elsif file.is_a?(String) && File.exist?(file) # If a string is provided and it's a valid file path, open the file payload[:file] = Faraday::Multipart::FilePart.new(File.open(file, "r"), "application/octet-stream") else raise ArgumentError, "File must be a File object or a valid file path" end req.params[:destination] = destination req.body = payload end handle_response(response, BucketSdk::Models::LoadResponse) end |