Skip to content

Commit

Permalink
Add stream file example
Browse files Browse the repository at this point in the history
  • Loading branch information
NabilMostafa committed Mar 1, 2022
1 parent 095d2d8 commit 0a27589
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aiogoogle/sessions/aiohttp_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def resolve_response(request, response):

if pipe_to:
async for line in response.content.iter_chunked(chunk_size):
pipe_to.write(line)
await pipe_to.write(line)

if download_file:
async with aiofiles.open(download_file, "wb+") as f:
Expand Down
63 changes: 63 additions & 0 deletions examples/stream_drive_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/python3.7

'''
Scopes Required:
* https://www.googleapis.com/auth/drive
* https://www.googleapis.com/auth/drive.file
API explorer link:
* https://developers.google.com/apis-explorer/#p/drive/v3/drive.files.get
'''

import asyncio
import sys

from helpers import Aiogoogle, user_creds, client_creds

usage = """
Usage:
argv1: ID of the file you want to stream (see hint)
Hint:
run list_drive_files.py to list the names of the files you own alongside their IDs
Example:
List the id of "my_old_archive.tar.gz" by running:
./list_drive_files | grep my_old_archive.tar.gz
0Bw9MwYF2OXbSc0d0ZmNlRjVhMmM: myold_archive.tar.gz
Now run:
./stream_drive_file.py 0Bw9MwYF2OXbSc0d0ZmNlRjVhMmM
"""


class MyFile:
@staticmethod
async def write(data: bytes):
print(data)


async def stream_file(file_id):
async with Aiogoogle(user_creds=user_creds, client_creds=client_creds) as aiogoogle:
drive_v3 = await aiogoogle.discover("drive", "v3")

# Stream the file
await aiogoogle.as_user(
drive_v3.files.get(fileId=file_id, pipe_to=MyFile(), alt="media")
)


if __name__ == "__main__":
try:
file_id = sys.argv[1]
except IndexError:
print(usage)
sys.exit(1)
asyncio.run(stream_file(file_id))

0 comments on commit 0a27589

Please sign in to comment.