Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

166 add functionality to upload log text report files to google drive #170

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ frontend/package-lock.json
desktop.ini

# Ignore Google Cloud service account key
key.json
key.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don’t need to add this to prevent pushing the key.json file, as it was already included in the .gitignore on the line above.

backend/key.json
4 changes: 2 additions & 2 deletions backend/PythonClient/multirotor/airsim_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

from PythonClient import airsim
from PythonClient.multirotor.storage import GCSStorageService, GoogleDriveStorageService

class AirSimApplication:
# Parent class for all airsim client side mission and monitors
def __init__(self):
# implementation of the GCS service
self.storage_service = GCSStorageService()
#self.storage_service = GCSStorageService()
self.storage_service = GoogleDriveStorageService()

self.circular_mission_names = {"FlyInCircle"}
self.polygon_mission_names = {"FlyToPoints", "FlyToPointsGeo"}
Expand Down
46 changes: 44 additions & 2 deletions backend/PythonClient/multirotor/storage/gd_storage_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from PythonClient.multirotor.storage.abstract.storage_service import StorageServiceInterface
from abstract.storage_service import StorageServiceInterface
# backend.PythonClient.multirotor.storage.abstract.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to use relative imports, you just need to use a single period (.), which refers to the current package.

from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaInMemoryUpload
Expand All @@ -18,6 +19,47 @@ def __init__(self, folder_id='1zZ06TaCzqdPJlQ9Uc4VgJaawBZm3eTSS'):
"""
SCOPES = ['https://www.googleapis.com/auth/drive']
self.credentials = service_account.Credentials.from_service_account_file(
'key.json', scopes=SCOPES)
'backend/key.json', scopes=SCOPES)
self.service = build('drive', 'v3', credentials=self.credentials)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation was already trying to find key.json in the backend directory. Your implementation causes an error: FileNotFoundError: [Errno 2] No such file or directory: 'backend/key.json'.

self.folder_id = folder_id # The ID of the shared root folder

def upload_to_service(self, file_name, content, content_type='text/plain'):
"""
Uploads a file to the Google Drive.
"""

with self._lock: # Prevent race conditions
try:
# Prepare file metadata and content
media = MediaInMemoryUpload(content.encode('utf-8'), mimetype=content_type)
metadata = {'name': file_name, 'parents': [self.folder_id]}

# Upload the file
file_id = self.service.files().create(body=metadata, media_body=media, fields='id').execute().get('id')
print(f"Uploaded '{file_name}' with ID: {file_id}")
return file_id

except Exception as e:
print(f"Upload error: {e}")
return None


def main():
# Initialize Google Drive storage service with default folder ID and credentials
drive_service = GoogleDriveStorageService()

# Sample file details
file_name = 'test_log.txt'
content = 'This is a test log file content with relative path.'

# Upload the file
file_id = drive_service.upload_to_service(file_name, content)

# Verify upload
if file_id:
print(f"File uploaded successfully with ID: {file_id}")
else:
print("File upload failed.")

if __name__ == "__main__":
main()
Loading