Skip to content

Instantly share code, notes, and snippets.

@tseaver
Created January 30, 2019 17:54
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 tseaver/eebb9f8605f6e606b4a985e7b8ea8f85 to your computer and use it in GitHub Desktop.
Save tseaver/eebb9f8605f6e606b4a985e7b8ea8f85 to your computer and use it in GitHub Desktop.
GCS example with pipes
import os
import threading
from google.cloud.storage import Client
PAYLOAD = b'DEADBEEF' * 100000
def reader(in_file):
count = 0
print("reader: start")
while True:
chunk = in_file.read(10000)
print("reader: read one chunk")
if not chunk:
break
count += len(chunk)
print("reader: read {} bytes".format(count))
def main():
client = Client()
bucket = client.bucket('test-gcp-7218')
if not bucket.exists():
bucket.create()
blob = bucket.blob('test.me')
if not blob.exists():
blob.upload_from_string(PAYLOAD)
in_pipe, out_pipe = os.pipe()
in_file = os.fdopen(in_pipe, mode='rb')
out_file = os.fdopen(out_pipe, mode='wb')
worker = threading.Thread(target=reader, args=(in_file,))
worker.start()
blob.download_to_file(out_file)
out_file.close()
worker.join()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment