Completed
Pull Request — master (#543)
by
unknown
02:35
created

UploadFileAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 3
1
import os
2
3
from libcloud.storage.types import ContainerDoesNotExistError
4
5
from lib.actions import BaseAction
6
7
__all__ = [
8
    'UploadFileAction'
9
]
10
11
12
class UploadFileAction(BaseAction):
13
    api_type = 'storage'
14
15
    def run(self, credentials, file_path, container_name, object_name=None):
16
        driver = self._get_driver_for_credentials(credentials=credentials)
17
18
        try:
19
            container = driver.get_container(container_name=container_name)
20
        except ContainerDoesNotExistError:
21
            self.logger.debug('Container "%s" doesn\'t exist, creating it...' %
22
                              (container_name))
23
            container = driver.create_container(container_name=container_name)
24
25
        object_name = object_name if object_name else os.path.basename(file_path)
26
        obj = driver.upload_object(file_path=file_path, container=container,
27
                                   object_name=object_name)
28
29
        self.logger.info('Object successfully uploaded: %s' % (obj))
30
        return obj
31