Authentication methods

  • OAuth 2.0

Access to Google Drive Python SDK

Dealing with files over REST API can be challenging. Therefore, for the Google Drive connector, we make it easy to use the published Google Drive Python SDK.

To access the service object necessary to make the calls, simply add a Strada action and give it a name. That strada action will have a .service attribute.

From there, you can invoke any of the Python SDK methods.

Uploading a file

Once you have added a Google Drive action and given it a name of DriveAction, you can use DriveAction.service to upload files.

Files should be managed in-memory. Therefore use io.BytesIO and MediaIoBaseUpload when applicable.

import io
from googleapiclient.http import MediaIoBaseUpload

# File contents in memory
csv_content = """column1,column2,column3
value1,value2,value3
value4,value5,value6"""

# Create a BytesIO object from the CSV content
file_io = io.BytesIO(csv_content.encode('utf-8'))

# Create a new file metadata
file_metadata = {
    'name': 'my_uploaded_file.csv',
    'mimeType': 'text/csv'
}

# Upload the file using MediaIoBaseUpload
media = MediaIoBaseUpload(file_io, mimetype='text/csv')

file = DriveAction.service.files().create(body=file_metadata, media_body=media, fields='id').execute()

print(f"File ID: {file.get('id')}")

Listing files

Once you have added a Google Drive action and given it a name of DriveAction, you can use DriveAction.service to upload files.

# Call the Drive v3 API
results = (
    DriveAction.service.files()
    .list(pageSize=10, fields="nextPageToken, files(id, name)")
    .execute()
)
items = results.get("files", [])

if not items:
  print("No files found.")
else:
  print("Files:")
  for item in items:
    print(f"{item['name']} ({item['id']})")