def test_execute_fails_for_local_to_cloud_copy(self): source_resource = resource_reference.ObjectResource( storage_url.storage_url_from_string('file://o.txt'), metadata=self.source_metadata) destination_resource = resource_reference.UnknownResource( storage_url.storage_url_from_string('gs://b/o.txt')) with self.assertRaises(ValueError): daisy_chain_copy_task.DaisyChainCopyTask(source_resource, destination_resource)
def get_copy_task(source_resource, destination_resource): """Factory method that returns the correct copy task for the arguments. Args: source_resource (resource_reference.Resource): Reference to file to copy. destination_resource (resource_reference.Resource): Reference to destination to copy file to. Returns: Task object that can be executed to perform a copy. Raises: NotImplementedError: Cross-cloud copy. ValueError: Local filesystem copy. """ source_url = source_resource.storage_url destination_url = destination_resource.storage_url if (isinstance(source_url, storage_url.FileUrl) and isinstance(destination_url, storage_url.FileUrl)): raise ValueError( 'Local copies not supported. Gcloud command-line tool is' ' meant for cloud operations.') if (isinstance(source_url, storage_url.CloudUrl) and isinstance(destination_url, storage_url.FileUrl)): return file_download_task.FileDownloadTask(source_resource, destination_resource) if (isinstance(source_url, storage_url.FileUrl) and isinstance(destination_url, storage_url.CloudUrl)): return file_upload_task.FileUploadTask(source_resource, destination_resource) if (isinstance(source_url, storage_url.CloudUrl) and isinstance(destination_url, storage_url.CloudUrl)): if source_url.scheme != destination_url.scheme: return daisy_chain_copy_task.DaisyChainCopyTask( source_resource, destination_resource) return intra_cloud_copy_task.IntraCloudCopyTask( source_resource, destination_resource)
def test_execute_copies_file_between_clouds(self, mock_client): test_stream = io.BytesIO() mock_client.download_object.side_effect = _write_to_stream_side_effect( test_stream) mock_client.upload_object.side_effect = ( self._assert_upload_stream_has_correct_data) with mock.patch.object(io, 'BytesIO') as mock_stream_creator: mock_stream_creator.return_value = test_stream source_resource = resource_reference.ObjectResource( storage_url.storage_url_from_string('gs://b/o.txt'), metadata=self.source_metadata) destination_resource = resource_reference.UnknownResource( storage_url.storage_url_from_string('s3://b/o2.txt')) task = daisy_chain_copy_task.DaisyChainCopyTask( source_resource, destination_resource) task.execute() mock_client.download_object.assert_called_once_with( source_resource.bucket, source_resource.name, test_stream) mock_client.upload_object.assert_called_once_with( test_stream, destination_resource)
def get_copy_task(source_resource, destination_resource, do_not_decompress=False, print_created_message=False, shared_stream=None, user_request_args=None): """Factory method that returns the correct copy task for the arguments. Args: source_resource (resource_reference.Resource): Reference to file to copy. destination_resource (resource_reference.Resource): Reference to destination to copy file to. do_not_decompress (bool): Prevents automatically decompressing downloaded gzips. print_created_message (bool): Print the versioned URL of each successfully copied object. shared_stream (stream): Multiple tasks may reuse this read or write stream. user_request_args (UserRequestArgs|None): Values for RequestConfig. Returns: Task object that can be executed to perform a copy. Raises: NotImplementedError: Cross-cloud copy. ValueError: Local filesystem copy. """ source_url = source_resource.storage_url destination_url = destination_resource.storage_url if (isinstance(source_url, storage_url.FileUrl) and isinstance(destination_url, storage_url.FileUrl)): raise ValueError( 'Local copies not supported. Gcloud command-line tool is' ' meant for cloud operations.') if (isinstance(source_url, storage_url.CloudUrl) and isinstance(destination_url, storage_url.FileUrl)): if destination_url.is_pipe: return streaming_download_task.StreamingDownloadTask( source_resource, shared_stream, print_created_message=print_created_message, user_request_args=user_request_args) return file_download_task.FileDownloadTask( source_resource, destination_resource, do_not_decompress=do_not_decompress, print_created_message=print_created_message, user_request_args=user_request_args) if (isinstance(source_url, storage_url.FileUrl) and isinstance(destination_url, storage_url.CloudUrl)): return file_upload_task.FileUploadTask( source_resource, destination_resource, print_created_message=print_created_message, user_request_args=user_request_args) if (isinstance(source_url, storage_url.CloudUrl) and isinstance(destination_url, storage_url.CloudUrl)): if source_url.scheme != destination_url.scheme: if getattr(user_request_args.resource_args, 'preserve_acl', False): raise ValueError( 'Cannot preserve ACLs while copying between cloud providers.' ) return daisy_chain_copy_task.DaisyChainCopyTask( source_resource, destination_resource, print_created_message=print_created_message, user_request_args=user_request_args) return intra_cloud_copy_task.IntraCloudCopyTask( source_resource, destination_resource, print_created_message=print_created_message, user_request_args=user_request_args)