def PutArtifact(self, request_iterator, context=None): # type: (...) -> beam_artifact_api_pb2.PutArtifactResponse first = True for request in request_iterator: if first: first = False metadata = request.metadata.metadata retrieval_token = self.retrieval_token( request.metadata.staging_session_token) artifact_path = self._artifact_path(retrieval_token, metadata.name) temp_path = self._temp_path(artifact_path) fout = self._open(temp_path, 'w') hasher = hashlib.sha256() else: hasher.update(request.data.data) fout.write(request.data.data) fout.close() data_hash = hasher.hexdigest() if metadata.sha256 and metadata.sha256 != data_hash: self._delete(temp_path) raise ValueError('Bad metadata hash: %s vs %s' % (metadata.sha256, data_hash)) self._rename(temp_path, artifact_path) return beam_artifact_api_pb2.PutArtifactResponse()
def PutArtifact(self, request_iterator, context=None): first = True for request in request_iterator: if first: first = False metadata = request.metadata.metadata retrieval_token = self.retrieval_token( request.metadata.staging_session_token) self._mkdir(retrieval_token) temp_path = filesystems.FileSystems.join( self._root, retrieval_token, '%x.tmp' % random.getrandbits(128)) fout = filesystems.FileSystems.create(temp_path) hasher = hashlib.sha256() else: hasher.update(request.data.data) fout.write(request.data.data) fout.close() data_hash = hasher.hexdigest() if metadata.sha256 and metadata.sha256 != data_hash: filesystems.FileSystems.delete([temp_path]) raise ValueError('Bad metadata hash: %s vs %s' % (metadata.metadata.sha256, data_hash)) filesystems.FileSystems.rename( [temp_path], [self._artifact_path(retrieval_token, metadata.name)]) return beam_artifact_api_pb2.PutArtifactResponse()
def PutArtifact(self, request_iterator, context): first = True file_name = None for request in request_iterator: if first: first = False file_name = request.metadata.name else: with open(os.path.join(self.temp_dir, file_name), 'ab') as f: f.write(request.data.data) return beam_artifact_api_pb2.PutArtifactResponse()