Created
January 30, 2019 17:54
-
-
Save tseaver/eebb9f8605f6e606b4a985e7b8ea8f85 to your computer and use it in GitHub Desktop.
GCS example with pipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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