Ejemplo n.º 1
0
    def test_multipart_upload_uses_correct_client_calls(self):
        client = mock.Mock()
        uploader = MultipartUploader(client, TransferConfig(),
                                     InMemoryOSLayer({'filename': b'foobar'}),
                                     SequentialExecutor)
        client.create_multipart_upload.return_value = {'UploadId': 'upload_id'}
        client.upload_part.return_value = {'ETag': 'first'}

        uploader.upload_file('filename', 'bucket', 'key', None, {})

        # We need to check both the sequence of calls (create/upload/complete)
        # as well as the params passed between the calls, including
        # 1. The upload_id was plumbed through
        # 2. The collected etags were added to the complete call.
        client.create_multipart_upload.assert_called_with(Bucket='bucket',
                                                          Key='key')
        # Should be two parts.
        client.upload_part.assert_called_with(Body=mock.ANY,
                                              Bucket='bucket',
                                              UploadId='upload_id',
                                              Key='key',
                                              PartNumber=1)
        client.complete_multipart_upload.assert_called_with(
            MultipartUpload={'Parts': [{
                'PartNumber': 1,
                'ETag': 'first'
            }]},
            Bucket='bucket',
            UploadId='upload_id',
            Key='key')
Ejemplo n.º 2
0
    def test_multipart_upload_injects_proper_kwargs(self):
        client = mock.Mock()
        uploader = MultipartUploader(
            client, TransferConfig(),
            InMemoryOSLayer({'filename': b'foobar'}), SequentialExecutor)
        client.create_multipart_upload.return_value = {'UploadId': 'upload_id'}
        client.upload_part.return_value = {'ETag': 'first'}

        extra_args = {
            'SSECustomerKey': 'fakekey',
            'SSECustomerAlgorithm': 'AES256',
            'StorageClass': 'REDUCED_REDUNDANCY'
        }
        uploader.upload_file('filename', 'bucket', 'key', None, extra_args)

        client.create_multipart_upload.assert_called_with(
            Bucket='bucket', Key='key',
            # The initial call should inject all the storage class params.
            SSECustomerKey='fakekey',
            SSECustomerAlgorithm='AES256',
            StorageClass='REDUCED_REDUNDANCY')
        client.upload_part.assert_called_with(
            Body=mock.ANY, Bucket='bucket',
            UploadId='upload_id', Key='key', PartNumber=1,
            # We only have to forward certain **extra_args in subsequent
            # UploadPart calls.
            SSECustomerKey='fakekey',
            SSECustomerAlgorithm='AES256',
        )
        client.complete_multipart_upload.assert_called_with(
            MultipartUpload={'Parts': [{'PartNumber': 1, 'ETag': 'first'}]},
            Bucket='bucket',
            UploadId='upload_id',
            Key='key')
Ejemplo n.º 3
0
    def test_multipart_upload_is_aborted_on_error(self):
        # If the create_multipart_upload succeeds and any upload_part
        # fails, then abort_multipart_upload will be called.
        client = mock.Mock()
        uploader = MultipartUploader(
            client, TransferConfig(),
            InMemoryOSLayer({'filename': b'foobar'}), SequentialExecutor)
        client.create_multipart_upload.return_value = {'UploadId': 'upload_id'}
        client.upload_part.side_effect = Exception(
            "Some kind of error occurred.")

        with self.assertRaises(S3UploadFailedError):
            uploader.upload_file('filename', 'bucket', 'key', None, {})

        client.abort_multipart_upload.assert_called_with(
            Bucket='bucket', Key='key', UploadId='upload_id')