Reputation: 517
I have a folder on the google drive with just .jpg images and I want to download all of the images in the folder to my computer using the folder's shared link.
So far, the only thing that I have found that works is the code below but I can only get it to work for specific shared files and not an entire folder.
from google_drive_downloader import GoogleDriveDownloader as gdd
gdd.download_file_from_google_drive(file_id='1viW3guJTZuEFcx1-ivCL2aypDNLckEMh',
dest_path='./data/mnist.zip',
unzip=True)
Is there a way to modify this to work with google folders or is there another way to download google drive folders?
Upvotes: 6
Views: 8080
Reputation: 943
You can use the gdrive tool. Basically it is a tool to access a google drive account from command-line. Follow this example for a Linux machine to set this up:
gdrive
. Now change the permissions of the file by executing the command
chmod +x gdrive
../gdrive about
and you will get a URL asking you to enter for a verification code.
As instructed in the prompt copy the link and go to the URL on your browser, then log into your Google Drive account and grant permission. You will get some verification code at last. Copy it.Now once done with the above process you can navigate the files on your drive using the commands mentioned below.
./gdrive list # List all files' information in your account
./gdrive list -q "name contains 'University'" # serch files by name
./gdrive download fileID # Download some file. You can find the fileID from the 'gdrive list' result.
./gdrive upload filename # Upload a local file to your google drive account.
./gdrive mkdir # Create new folder
Hopefully, this helps.
Upvotes: 0
Reputation: 15377
If you use the Python Quickstart Tutorial for Google Drive API which you can find here, you'll be able to set up your authentication with the API. You can then loop through your Drive and only download jpg
files by specifying the MIMEType of your search to be image/jpeg
.
First, you want to loop through the files on your drive using files: list
:
# Note: folder_id can be parsed from the shared link
def listFiles(service, folder_id):
listOfFiles = []
query = f"'{folder_id}' in parents and mimeType='image/jpeg'"
# Get list of jpg files in shared folder
page_token = None
while True:
response = service.files().list(
q=query,
fields="nextPageToken, files(id, name)",
pageToken=page_token,
includeItemsFromAllDrives=True,
supportsAllDrives=True
).execute()
for file in response.get('files', []):
listOfFiles.append(file)
page_token = response.get('nextPageToken', None)
if page_token is None:
break
return listOfFiles
Then you can download them by using the files: get_media()
method:
import io
from googleapiclient.http import MediaIoBaseDownload
def downloadFiles(service, listOfFiles):
# Download all jpegs
for fileID in listOfFiles:
request = service.files().get_media(fileId=fileID['id'])
fh = io.FileIO(fileID['name'], 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Downloading..." + str(fileID['name']))
You can refine your search further by using the q
parameter in listFiles()
as specified here.
Upvotes: 4