Class: S3FileHandler::File::Upload

Inherits:
BaseOperation show all
Defined in:
lib/s3_file_handler/file/upload.rb

Instance Attribute Summary collapse

Attributes inherited from BaseOperation

#client, #errors

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name, file_key, content: nil, local_path: nil, client: S3FileHandler.client) ⇒ Upload

Returns a new instance of Upload.



8
9
10
11
12
13
14
15
# File 'lib/s3_file_handler/file/upload.rb', line 8

def initialize(bucket_name, file_key, content: nil, local_path: nil, client: S3FileHandler.client)
  super(client: client)

  @bucket_name = bucket_name
  @file_key = file_key
  @content = content
  @local_path = local_path
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



6
7
8
# File 'lib/s3_file_handler/file/upload.rb', line 6

def bucket_name
  @bucket_name
end

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/s3_file_handler/file/upload.rb', line 6

def content
  @content
end

#file_keyObject (readonly)

Returns the value of attribute file_key.



6
7
8
# File 'lib/s3_file_handler/file/upload.rb', line 6

def file_key
  @file_key
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



6
7
8
# File 'lib/s3_file_handler/file/upload.rb', line 6

def local_path
  @local_path
end

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/s3_file_handler/file/upload.rb', line 17

def execute
  handle_aws_errors do
    body = content || ::File.open(local_path, 'rb')

    client.put_object(
      bucket: bucket_name,
      key: file_key,
      body: body
    )

    result({ bucket: bucket_name, key: file_key, status: 'uploaded' })
  end
end