I cannot add folders through the Google API, only individual files. I want to access a large number of images (10s of GB) on Google Drive by their path. The intent is for the client to fill out a form with fields like “Pet”, “Dog”, “Rover”, and the server code will assemble a path like “myGoogleDrive/Pet/Dog/Rover.jpg”, and return a modified copy of the image. It is not practical to add each file to the app individually.
I tried using Anvil’s Google API. I got errors when adding files with a vague error “e is undefined”. I added OAuth credentials through the Google Developer API (“Connecting Google to Anvil” article), now that error is resolved, but I still cannot add folders. I can see them descend into them, but not select the folder.
I do not have a code sample or link. I am new to Anvil, still trying to assess if it will meet my needs. Very hopeful, excited that it might.
If you want the user to access your files, then you shouldn’t use the Advanced Settings of Google’s API. This approach allows your users to access their files (not your files)
If I am reading your use case correctly, I think you should use Data Files. You can find more info here. This allows your users to access the files that you have uploaded.
Start by dragging your root folder to the Data Files field
Then you can write a server code that returns the file. You’ll probably be able to write a more efficient and clean code, but it’s just to show the possibility.
@anvil.server.callable
def search_image(folder_name_1, folder_name_2, file_name):
data_files_folder_1 = data_files[folder_name_1]
if data_files_folder_1 is not None:
with os.scandir(data_files_folder_1) as data_files_folder_2:
for directory_2 in data_files_folder_2:
if directory_2.is_dir() and folder_name_2 in directory_2.name:
with os.scandir(directory_2) as data_files_folder_3:
for file_1 in data_files_folder_3:
if file_name in file_1.name and file_1.is_file():
with open(file_1.path, "rb") as f:
file_bytes = f.read()
anvil_media = anvil.BlobMedia("image/jpeg", file_bytes, name=file_1.name)
return anvil_media
return None
Hi APDW, thanks for taking so much time to reply! I can certainly use some of your code for my app. I’m missing a key component: The size of my image database exceeds the Anvil storage limit. That’s why I need to create a path to the folder in my Google Drive, not the app server folder.
I modified your server code to use app_files (imported from anvil.google.drive) instead of data_files, to pull the path from my Google Drive, and then I logged into that Drive through the Google API, and mirrored the “pet” folder structure onto Google Drive. See code snippet below, where I modified the last line of the excerpt. I got an error at the last line of this code excerpt: TypeError: 'AppFilesCollection' object is not subscriptable
The Anvil article below says I can select Google files and folders. I tried, but can only select individual files.
import anvil.files
from anvil.files import data_files
import anvil.google.auth, anvil.google.drive, anvil.google.mail
from anvil.google.drive import app_files
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.server
import os
@anvil.server.callable
def search_image(folder_name_1, folder_name_2, file_name):
data_files_folder_1 = app_files[folder_name_1]