Skip to content

Instantly share code, notes, and snippets.

@wfng92
Created September 8, 2022 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wfng92/62844916180ff2a39658d703c5a806fc to your computer and use it in GitHub Desktop.
Save wfng92/62844916180ff2a39658d703c5a806fc to your computer and use it in GitHub Desktop.
def read_iterfile(filepath, chunk_size=1024):
split_data = os.path.splitext(filepath)
filename = split_data[0]
extension = split_data[1]
metadata = hello_pb2.MetaData(filename=filename, extension=extension)
yield hello_pb2.UploadFileRequest(metadata=metadata)
with open(filepath, mode="rb") as f:
while True:
chunk = f.read(chunk_size)
if chunk:
entry_request = hello_pb2.UploadFileRequest(chunk_data=chunk)
yield entry_request
else: # The chunk was empty, which means we're at the end of the file
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment