Class: BucketSdk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bucket_sdk/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_urlObject (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

#timeoutObject (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

Parameters:

  • recurse (Boolean) (defaults to: false)

    Whether to list objects recursively (default: false)

Returns:



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

Parameters:

  • file (File, String)

    The file to upload (either a File object or a path to a file)

  • destination (String)

    The destination path in the bucket

Returns:



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.options.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