Example #1
0
def upload_file(self,
                Filename,
                Bucket,
                Key,
                ExtraArgs=None,
                Callback=None,
                Config=None):
    """Upload a file to an S3 object.

    Usage::

        import ibm_boto3
        s3 = ibm_boto3.resource('s3')
        s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')

    Similar behavior as S3Transfer's upload_file() method,
    except that parameters are capitalized. Detailed examples can be found at
    :ref:`S3Transfer's Usage <ref_s3transfer_usage>`.
    """
    with S3Transfer(self, Config) as transfer:
        return transfer.upload_file(filename=Filename,
                                    bucket=Bucket,
                                    key=Key,
                                    extra_args=ExtraArgs,
                                    callback=Callback)
Example #2
0
 def test_context_manager(self):
     manager = mock.Mock()
     manager.__exit__ = mock.Mock()
     with S3Transfer(manager=manager):
         pass
     # The underlying transfer manager should have had its __exit__
     # called as well.
     assert manager.__exit__.call_args == mock.call(None, None, None)
Example #3
0
 def test_context_manager_with_errors(self):
     manager = mock.Mock()
     manager.__exit__ = mock.Mock()
     raised_exception = ValueError()
     with pytest.raises(type(raised_exception)):
         with S3Transfer(manager=manager):
             raise raised_exception
     # The underlying transfer manager should have had its __exit__
     # called as well and pass on the error as well.
     assert manager.__exit__.call_args == mock.call(type(raised_exception),
                                                    raised_exception,
                                                    mock.ANY)
Example #4
0
def upload_file(self,
                Filename,
                Bucket,
                Key,
                ExtraArgs=None,
                Callback=None,
                Config=None):
    """Upload a file to an S3 object.

    Usage::

        import ibm_boto3
        s3 = ibm_boto3.resource('s3')
        s3.meta.client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')

    Similar behavior as S3Transfer's upload_file() method,
    except that parameters are capitalized. Detailed examples can be found at
    :ref:`S3Transfer's Usage <ref_ibm_s3transfer_usage>`.

    :type Filename: str
    :param Filename: The path to the file to upload.

    :type Bucket: str
    :param Bucket: The name of the bucket to upload to.

    :type Key: str
    :param Key: The name of the key to upload to.

    :type ExtraArgs: dict
    :param ExtraArgs: Extra arguments that may be passed to the
        client operation. For allowed upload arguments see
        ibm_boto3.s3.transfer.S3Transfer.ALLOWED_UPLOAD_ARGS.

    :type Callback: function
    :param Callback: A method which takes a number of bytes transferred to
        be periodically called during the upload.

    :type Config: ibm_boto3.s3.transfer.TransferConfig
    :param Config: The transfer configuration to be used when performing the
        transfer.
    """
    with S3Transfer(self, Config) as transfer:
        return transfer.upload_file(
            filename=Filename,
            bucket=Bucket,
            key=Key,
            extra_args=ExtraArgs,
            callback=Callback,
        )
Example #5
0
def getCosTransferAgent(credentials):
    endpoints = requests.get(credentials.get('endpoints')).json()
    iam_host = (endpoints['identity-endpoints']['iam-token'])
    cos_host = (endpoints['service-endpoints']['cross-region']['us']['public']
                ['us-geo'])
    api_key = credentials.get('apikey')
    service_instance_id = credentials.get('resource_instance_id')
    auth_endpoint = "https://" + iam_host + "/oidc/token"
    service_endpoint = "https://" + cos_host
    cos = ibm_boto3.client('s3',
                           ibm_api_key_id=api_key,
                           ibm_service_instance_id=service_instance_id,
                           ibm_auth_endpoint=auth_endpoint,
                           config=Config(signature_version='oauth'),
                           endpoint_url=service_endpoint)
    return S3Transfer(cos)
Example #6
0
def getCosTransferAgent(credentials):
    """
    Use IAM credentials to obtain a Cloud Object Storage transfer agent object
    """
    if IBMBOTO_INSTALLED:
        endpoints = requests.get(credentials.get('endpoints')).json()
        iam_host = (endpoints['identity-endpoints']['iam-token'])
        cos_host = (endpoints['service-endpoints']['cross-region']['us']
                    ['public']['us-geo'])
        api_key = credentials.get('apikey')
        service_instance_id = credentials.get('resource_instance_id')
        auth_endpoint = "https://" + iam_host + "/oidc/token"
        service_endpoint = "https://" + cos_host
        cos = ibm_boto3.client('s3',
                               ibm_api_key_id=api_key,
                               ibm_service_instance_id=service_instance_id,
                               ibm_auth_endpoint=auth_endpoint,
                               config=Config(signature_version='oauth'),
                               endpoint_url=service_endpoint)
        return S3Transfer(cos)
    else:
        raise ValueError(
            'Attempting to use IAM credentials to communicate with COS. IBMBOTO is not installed.\
             You make use HMAC credentials and the CosClient instead.')
Example #7
0
 def test_download_requires_string_filename(self):
     transfer = S3Transfer(client=mock.Mock())
     with pytest.raises(ValueError):
         transfer.download_file(bucket='foo', key='bar', filename=object())
Example #8
0
 def test_osutil_and_manager_are_mutually_exclusive(self):
     with pytest.raises(ValueError):
         S3Transfer(osutil=mock.Mock(), manager=self.manager)
Example #9
0
 def test_client_and_manager_are_mutually_exclusive(self):
     with pytest.raises(ValueError):
         S3Transfer(self.client, manager=self.manager)
Example #10
0
 def test_client_or_manager_is_required(self):
     with pytest.raises(ValueError):
         S3Transfer()
Example #11
0
 def test_can_create_with_extra_configurations(self):
     transfer = S3Transfer(client=mock.Mock(),
                           config=TransferConfig(),
                           osutil=OSUtils())
     assert isinstance(transfer, S3Transfer)
Example #12
0
 def test_can_create_with_just_client(self):
     transfer = S3Transfer(client=mock.Mock())
     assert isinstance(transfer, S3Transfer)
Example #13
0
 def setUp(self):
     self.client = mock.Mock()
     self.manager = mock.Mock(TransferManager(self.client))
     self.transfer = S3Transfer(manager=self.manager)
     self.callback = mock.Mock()
 def test_upload_requires_string_filename(self):
     transfer = S3Transfer(client=mock.Mock())
     with self.assertRaises(ValueError):
         transfer.upload_file(filename=object(), bucket='foo', key='bar')
 def test_config_and_manager_are_mutually_exclusive(self):
     with self.assertRaises(ValueError):
         S3Transfer(config=mock.Mock(), manager=self.manager)
 def test_client_or_manager_is_required(self):
     with self.assertRaises(ValueError):
         S3Transfer()