Exemple #1
0
 def get_object(self, container_name, key, stream=False, extra_get_args={}):
     """
     Get object from Swift with a key. Throws StorageNoSuchKeyError if the given key does not exist.
     :param key: key of the object
     :return: Data of the object
     :rtype: str/bytes
     """
     if not container_name:
         container_name = self.storage_container
     url = os.path.join(self.endpoint, container_name, key)
     headers = {'X-Auth-Token': self.token}
     headers.update(extra_get_args)
     try:
         res = self.session.get(url, headers=headers, stream=stream)
         if res.status_code == 200 or res.status_code == 206:
             if stream:
                 data = res.raw
             else:
                 data = res.content
             return data
         elif res.status_code == 404:
             raise StorageNoSuchKeyError(key)
         else:
             raise Exception('{} - {}'.format(res.status_code, key))
     except StorageNoSuchKeyError:
         raise StorageNoSuchKeyError(key)
     except Exception as e:
         print(e)
         raise StorageNoSuchKeyError(key)
Exemple #2
0
 def bucket_exists(self, container_name):
     """
     Head container from Swift with a name. Throws StorageNoSuchKeyError if the given container does not exist.
     :param container_name: name of the container
     :return: Data of the bucket
     :rtype: str/bytes
     """
     url = os.path.join(self.endpoint, container_name)
     try:
         res = self.session.head(url)
         if res.status_code == 204:
             return res.headers
         elif res.status_code == 404:
             raise StorageNoSuchKeyError(container_name)
         else:
             raise Exception('{} - {}'.format(res.status_code))
     except:
         raise StorageNoSuchKeyError(container_name)
Exemple #3
0
 def head_object(self, container_name, key):
     """
     Head object from Swift with a key. Throws StorageNoSuchKeyError if the given key does not exist.
     :param key: key of the object
     :return: Data of the object
     :rtype: str/bytes
     """
     url = os.path.join(self.endpoint, container_name, key)
     try:
         res = self.session.head(url)
         if res.status_code == 200:
             return res.headers
         elif res.status_code == 404:
             raise StorageNoSuchKeyError(key)
         else:
             raise Exception('{} - {}'.format(res.status_code, key))
     except:
         raise StorageNoSuchKeyError(key)
Exemple #4
0
 def bucket_exists(self, bucket_name):
     """
     Head bucket from COS with a name. Throws StorageNoSuchKeyError if the given bucket does not exist.
     :param bucket_name: name of the bucket
     :return: Data of the object
     :rtype: str/bytes
     """
     try:
         self.cos_client.head_bucket(Bucket=bucket_name)
     except ibm_botocore.exceptions.ClientError as e:
         if e.response['Error']['Code'] == '404':
             raise StorageNoSuchKeyError(bucket_name)
         else:
             raise e
Exemple #5
0
 def head_object(self, bucket_name, key):
     """
     Head object from COS with a key. Throws StorageNoSuchKeyError if the given key does not exist.
     :param key: key of the object
     :return: Data of the object
     :rtype: str/bytes
     """
     try:
         metadata = self.cos_client.head_object(Bucket=bucket_name, Key=key)
         return metadata['ResponseMetadata']['HTTPHeaders']
     except ibm_botocore.exceptions.ClientError as e:
         if e.response['Error']['Code'] == '404':
             raise StorageNoSuchKeyError(key)
         else:
             raise e
Exemple #6
0
 def get_object(self, bucket_name, key, stream=False, extra_get_args={}):
     """
     Get object from COS with a key. Throws StorageNoSuchKeyError if the given key does not exist.
     :param key: key of the object
     :return: Data of the object
     :rtype: str/bytes
     """
     try:
         r = self.cos_client.get_object(Bucket=bucket_name, Key=key, **extra_get_args)
         if stream:
             data = r['Body']
         else:
             data = r['Body'].read()
         return data
     except ibm_botocore.exceptions.ClientError as e:
         if e.response['Error']['Code'] == "NoSuchKey":
             raise StorageNoSuchKeyError(key)
         else:
             raise e
Exemple #7
0
    def list_objects(self, bucket_name, prefix=None):
        paginator = self.cos_client.get_paginator('list_objects_v2')
        try:
            if (prefix is not None):
                page_iterator = paginator.paginate(Bucket=bucket_name, Prefix=prefix)
            else:
                page_iterator = paginator.paginate(Bucket=bucket_name)

            object_list = []
            for page in page_iterator:
                if 'Contents' in page:
                    for item in page['Contents']:
                        object_list.append(item)
            return object_list
        except ibm_botocore.exceptions.ClientError as e:
            if e.response['Error']['Code'] == '404':
                raise StorageNoSuchKeyError(bucket_name)
            else:
                raise e
Exemple #8
0
 def put_object(self, bucket_name, key, data):
     """
     Put an object in COS. Override the object if the key already exists.
     :param key: key of the object.
     :param data: data of the object
     :type data: str/bytes
     :return: None
     """
     try:
         res = self.cos_client.put_object(Bucket=bucket_name, Key=key, Body=data)
         status = 'OK' if res['ResponseMetadata']['HTTPStatusCode'] == 200 else 'Error'
         try:
             logger.debug('PUT Object {} - Size: {} - {}'.format(key, sizeof_fmt(len(data)), status))
         except:
             logger.debug('PUT Object {} {}'.format(key, status))
     except ibm_botocore.exceptions.ClientError as e:
         if e.response['Error']['Code'] == "NoSuchKey":
             raise StorageNoSuchKeyError(key)
         else:
             raise e