Python minio.Minio() Examples

The following are 30 code examples of minio.Minio(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module minio , or try the search function .
Example #1
Source File: bucket_exist_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_bucket_exists_works(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('HEAD',
                         'https://localhost:9000/hello/',
                         {'User-Agent': _DEFAULT_USER_AGENT},
                         200)
        )
        client = Minio('localhost:9000')
        result = client.bucket_exists('hello')
        eq_(True, result)
        mock_server.mock_add_request(
            MockResponse('HEAD',
                         'https://localhost:9000/goodbye/',
                         {'User-Agent': _DEFAULT_USER_AGENT},
                         404)
        )
        false_result = client.bucket_exists('goodbye')
        eq_(False, false_result) 
Example #2
Source File: set_bucket_notification_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_notification_config_filterspec_is_valid_2(self):
        client = Minio('localhost:9000')
        client.set_bucket_notification(
            'my-test-bucket',
            {
                'QueueConfigurations': [
                    {
                        'Id': '1',
                        'Arn': 'arn1',
                        'Events': ['s3:ObjectCreated:*'],
                        'Filter': {
                            'S3Key': {
                            }
                        }
                    }
                ]
            }
        ) 
Example #3
Source File: DocumentService.py    From tfrs with Apache License 2.0 6 votes vote down vote up
def delete_attachments(document_id, attachment_ids):
        """
        Sends a request to the minio instance to delete the files associated
        to the DocumentFile records related to the provided parameters
        """
        minio = Minio(MINIO['ENDPOINT'],
                      access_key=MINIO['ACCESS_KEY'],
                      secret_key=MINIO['SECRET_KEY'],
                      secure=MINIO['USE_SSL'])

        attachments = DocumentFileAttachment.objects.filter(
            document_id=document_id,  # additional security
            id__in=attachment_ids
        )

        object_names = map(DocumentService.get_filename, attachments)
        for error in minio.remove_objects(MINIO['BUCKET_NAME'], object_names):
            logging.error(error)

        attachments.update(
            is_removed=True
        ) 
Example #4
Source File: _loader.py    From tfrs with Apache License 2.0 6 votes vote down vote up
def load_from_minio(self, bucket_name, object_name):
        access_key = os.getenv('MINIO_ACCESS_KEY', None)
        secret_key = os.getenv('MINIO_SECRET_KEY', None)
        endpoint = os.getenv('MINIO_ENDPOINT', None)

        minio = Minio(endpoint, access_key=access_key, secret_key=secret_key, secure=False)
        data = minio.get_object(bucket_name, object_name)

        chunks = []

        for buf in data.stream():
            chunks.append(buf)

        source = b''.join(chunks).decode('utf-8')
        mod = self._load_from_string(source)
        return mod['script_class'], source 
Example #5
Source File: Document.py    From tfrs with Apache License 2.0 6 votes vote down vote up
def upload_url(self, request):
        """
        Generates the presigned URL for uploading and retrieving
        the file
        """
        minio = Minio(MINIO['ENDPOINT'],
                      access_key=MINIO['ACCESS_KEY'],
                      secret_key=MINIO['SECRET_KEY'],
                      secure=MINIO['USE_SSL'])

        object_name = uuid.uuid4().hex
        put_url = minio.presigned_put_object(
            bucket_name=MINIO['BUCKET_NAME'],
            object_name=object_name,
            expires=MINIO['EXPIRY'])

        get_url = minio.presigned_get_object(
            bucket_name=MINIO['BUCKET_NAME'],
            object_name=object_name,
            expires=MINIO['EXPIRY'])

        return Response({
            'put': put_url,
            'get': get_url
        }) 
Example #6
Source File: list_objects_v2_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_empty_list_objects_works(self, mock_connection):
        mock_data = '''<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Name>bucket</Name>
  <Prefix></Prefix>
  <KeyCount>0</KeyCount>
  <MaxKeys>1000</MaxKeys>
  <Delimiter></Delimiter>
  <IsTruncated>false</IsTruncated>
</ListBucketResult>'''
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse(
                'GET',
                'https://localhost:9000/bucket/?list-type=2&prefix=&'
                'start-after=',
                {'User-Agent': _DEFAULT_USER_AGENT}, 200, content=mock_data)
        )
        client = Minio('localhost:9000')
        object_iter = client.list_objects_v2('bucket', recursive=True)
        objects = []
        for obj in object_iter:
            objects.append(obj)
        eq_(0, len(objects)) 
Example #7
Source File: list_buckets_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_empty_list_buckets_works(self, mock_connection):
        mock_data = ('<ListAllMyBucketsResult '
                     'xmlns="http://s3.amazonaws.com/doc/2006-03-01/">'
                     '<Buckets></Buckets><Owner><ID>minio</ID><DisplayName>'
                     'minio</DisplayName></Owner></ListAllMyBucketsResult>')
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('GET', 'https://localhost:9000/',
                         {'User-Agent': _DEFAULT_USER_AGENT},
                         200, content=mock_data)
        )
        client = Minio('localhost:9000')
        buckets = client.list_buckets()
        count = 0
        for bucket in buckets:
            count += 1
        eq_(0, count) 
Example #8
Source File: set_bucket_notification_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_notification_config_filterspec_is_valid_3(self):
        client = Minio('localhost:9000')
        client.set_bucket_notification(
            'my-test-bucket',
            {
                'QueueConfigurations': [
                    {
                        'Id': '1',
                        'Arn': 'arn1',
                        'Events': ['s3:ObjectCreated:*'],
                        'Filter': {
                            'Key': {
                            }
                        }
                    }
                ]
            }
        ) 
Example #9
Source File: set_bucket_notification_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_notification_config_filterspec_is_valid_6(self):
        client = Minio('localhost:9000')
        client.set_bucket_notification(
            'my-test-bucket',
            {
                'QueueConfigurations': [
                    {
                        'Id': '1',
                        'Arn': 'arn1',
                        'Events': ['s3:ObjectCreated:*'],
                        'Filter': {
                            'Key': {
                                'FilterRules': [
                                    {
                                        'Name': 'ab',
                                        'Value': 'abc'
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        ) 
Example #10
Source File: stat_object_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_stat_object_works(self, mock_connection):
        mock_headers = {
            'content-type': 'application/octet-stream',
            'last-modified': 'Fri, 26 Jun 2015 19:05:37 GMT',
            'content-length': 11,
            'etag': '5eb63bbbe01eeed093cb22bb8f5acdc3'
        }
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('HEAD',
                         'https://localhost:9000/hello/world',
                         {'User-Agent': _DEFAULT_USER_AGENT}, 200,
                         response_headers=mock_headers)
        )
        client = Minio('localhost:9000')
        client.stat_object('hello', 'world') 
Example #11
Source File: set_bucket_notification_test.py    From minio-py with Apache License 2.0 6 votes vote down vote up
def test_notification_config_filterspec_is_valid_4(self):
        client = Minio('localhost:9000')
        client.set_bucket_notification(
            'my-test-bucket',
            {
                'QueueConfigurations': [
                    {
                        'Id': '1',
                        'Arn': 'arn1',
                        'Events': ['s3:ObjectCreated:*'],
                        'Filter': {
                            'Key': {
                                'FilterRules': []
                            }
                        }
                    }
                ]
            }
        ) 
Example #12
Source File: remove_objects_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_object_is_iterator(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('POST',
                         'https://localhost:9000/hello/?delete=',
                         {'Content-Length': 95,
                          'User-Agent': _DEFAULT_USER_AGENT,
                          'Content-Md5': u'5Tg5SmU9Or43L4+iIyfPrQ=='}, 200,
                         content='<Delete/>')
        )
        client = Minio('localhost:9000')
        it = itertools.chain(('Ab', 'c'))
        for err in client.remove_objects('hello', it):
            print(err) 
Example #13
Source File: make_bucket_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_is_string(self):
        client = Minio('localhost:9000')
        client.make_bucket(1234) 
Example #14
Source File: remove_bucket_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_remove_bucket_works(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('DELETE',
                         'https://localhost:9000/hello/',
                         {'User-Agent': _DEFAULT_USER_AGENT}, 204)
        )
        client = Minio('localhost:9000')
        client.remove_bucket('hello') 
Example #15
Source File: remove_objects_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_invalid_name(self):
        client = Minio('localhost:9000')
        for err in client.remove_objects('AB&CD', 'world'):
            print(err) 
Example #16
Source File: remove_objects_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_object_is_list(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('POST',
                         'https://localhost:9000/hello/?delete=',
                         {'Content-Length': 95,
                          'User-Agent': _DEFAULT_USER_AGENT,
                          'Content-Md5': u'5Tg5SmU9Or43L4+iIyfPrQ=='}, 200,
                         content='<Delete/>')
        )
        client = Minio('localhost:9000')
        for err in client.remove_objects('hello', ["Ab", "c"]):
            print(err) 
Example #17
Source File: remove_objects_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_object_is_tuple(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('POST',
                         'https://localhost:9000/hello/?delete=',
                         {'Content-Length': 95,
                          'User-Agent': _DEFAULT_USER_AGENT,
                          'Content-Md5': u'5Tg5SmU9Or43L4+iIyfPrQ=='}, 200,
                         content='<Delete/>')
        )
        client = Minio('localhost:9000')
        for err in client.remove_objects('hello', ('Ab', 'c')):
            print(err) 
Example #18
Source File: ocr.py    From functions with Apache License 2.0 5 votes vote down vote up
def handler(event, context):
  if event['data']['EventType'] == "s3:ObjectCreated:Put" : 
        tf = tempfile.NamedTemporaryFile(delete=False)
        bucket = event['data']['Key'].split('/')[0]
        filename = event['data']['Key'].split('/')[1]
      
        # Fetching source file from Minio
        try:
            print('Fetching file')
            client.fget_object(bucket, filename, tf.name)
        except ResponseError as err:
            print('Error fetching file')
            print err

        # OCR text extraction performed by Tika
        print 'Sending file to Tika'
        parsed = parser.from_file(tf.name, 'http://tika-tika-server:80/tika')
        ocrdata = json.dumps(parsed, ensure_ascii=True)

        # MongoDB document insertion 
        db = mongo['ocr']
        result = db.processed.insert_one(parsed)
        print 'Document Saved!'
        print('Document proccessed: {0}'.format(result.inserted_id))

        # move OCRd file to done bucket 
        try:
            # Copy from input bucket to done bucket
            fullpath = 'input/' + filename 
            client.copy_object('done', filename, fullpath)
            # Remove from input bucket
            client.remove_object('input', filename)
        except ResponseError as err:
            print err
  else:
       print "Minio file deletion event"
      
  return "OCR Finished" 
Example #19
Source File: bucket_exist_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_exists_bad_request(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('HEAD',
                         'https://localhost:9000/hello/',
                         {'User-Agent': _DEFAULT_USER_AGENT},
                         400)
        )
        client = Minio('localhost:9000')
        client.bucket_exists('hello') 
Example #20
Source File: bucket_exist_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_exists_invalid_name(self):
        client = Minio('localhost:9000')
        client.bucket_exists('AB*CD') 
Example #21
Source File: bucket_exist_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_is_string(self):
        client = Minio('localhost:9000')
        client.bucket_exists(1234) 
Example #22
Source File: make_bucket_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_is_not_empty_string(self):
        client = Minio('localhost:9000')
        client.make_bucket('  \t \n  ') 
Example #23
Source File: remove_bucket_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_remove_bucket_invalid_name(self):
        client = Minio('localhost:9000')
        client.remove_bucket('AB*CD') 
Example #24
Source File: remove_bucket_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_is_not_empty_string(self):
        client = Minio('localhost:9000')
        client.remove_bucket('  \t \n  ') 
Example #25
Source File: remove_bucket_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_bucket_is_string(self):
        client = Minio('localhost:9000')
        client.remove_bucket(1234) 
Example #26
Source File: list_uploaded_parts_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_empty_list_parts_works(self, mock_connection):
        mock_data = '''<?xml version="1.0"?>
<ListPartsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Bucket>bucket</Bucket>
  <Key>go1.4.2</Key>
  <UploadId>ntWSjzBytPT2xKLaMRonzXncsO10EH4Fc-Iq2-4hG-ulRYB</UploadId>
  <Initiator>
    <ID>minio</ID>
    <DisplayName>minio</DisplayName>
  </Initiator>
  <Owner>
    <ID>minio</ID>
    <DisplayName>minio</DisplayName>
  </Owner>
  <StorageClass>STANDARD</StorageClass>
  <PartNumberMarker>0</PartNumberMarker>
  <NextPartNumberMarker>0</NextPartNumberMarker>
  <MaxParts>1000</MaxParts>
  <IsTruncated>false</IsTruncated>
</ListPartsResult>'''
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse(
                'GET',
                'https://localhost:9000/bucket/key?uploadId=upload_id',
                {'User-Agent': _DEFAULT_USER_AGENT}, 200, content=mock_data)
        )

        client = Minio('localhost:9000')
        part_iter = client._list_object_parts('bucket', 'key', 'upload_id')
        parts = []
        for part in part_iter:
            parts.append(part)
        eq_(0, len(parts)) 
Example #27
Source File: list_objects_v2_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_list_objects_works(self, mock_connection):
        mock_data = '''<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Name>bucket</Name>
  <Prefix></Prefix>
  <KeyCount>2</KeyCount>
  <MaxKeys>1000</MaxKeys>
  <IsTruncated>false</IsTruncated>
  <Contents>
    <Key>6/f/9/6f9898076bb08572403f95dbb86c5b9c85e1e1b3</Key>
    <LastModified>2016-11-27T07:55:53.000Z</LastModified>
    <ETag>&quot;5d5512301b6b6e247b8aec334b2cf7ea&quot;</ETag>
    <Size>493</Size>
    <StorageClass>REDUCED_REDUNDANCY</StorageClass>
  </Contents>
  <Contents>
    <Key>b/d/7/bd7f6410cced55228902d881c2954ebc826d7464</Key>
    <LastModified>2016-11-27T07:10:27.000Z</LastModified>
    <ETag>&quot;f00483d523ffc8b7f2883ae896769d85&quot;</ETag>
    <Size>493</Size>
    <StorageClass>REDUCED_REDUNDANCY</StorageClass>
  </Contents>
</ListBucketResult>'''
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse(
                'GET',
                'https://localhost:9000/bucket/?delimiter=%2F&list-type=2&'
                'prefix=&start-after=',
                {'User-Agent': _DEFAULT_USER_AGENT}, 200,
                content=mock_data
            )
        )
        client = Minio('localhost:9000')
        objects_iter = client.list_objects_v2('bucket')
        objects = []
        for obj in objects_iter:
            objects.append(obj)

        eq_(2, len(objects)) 
Example #28
Source File: get_object_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_get_object_throws_fail(self, mock_connection):
        error_xml = generate_error('code', 'message', 'request_id',
                                   'host_id', 'resource', 'bucket',
                                   'object')
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('GET',
                         'https://localhost:9000/hello/key',
                         {'User-Agent': _DEFAULT_USER_AGENT},
                         404, content=error_xml)
        )
        client = Minio('localhost:9000')
        client.get_object('hello', 'key') 
Example #29
Source File: get_object_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_object_is_not_empty_string(self):
        client = Minio('localhost:9000')
        client.get_object('hello', ' \t \n ') 
Example #30
Source File: get_bucket_policy_test.py    From minio-py with Apache License 2.0 5 votes vote down vote up
def test_get_policy_for_non_existent_bucket(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        bucket_name = 'non-existent-bucket'
        mock_server.mock_add_request(
            MockResponse(
                'GET',
                'https://localhost:9000/' + bucket_name + '/?policy=',
                {'User-Agent': _DEFAULT_USER_AGENT},
                404,
            )
        )
        client = Minio('localhost:9000')
        client.get_bucket_policy(bucket_name)