-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
from google.oauth2 import service_account | ||
from googleapiclient.discovery import build | ||
from googleapiclient.http import MediaInMemoryUpload | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous implementation was already trying to find |
||
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() |
There was a problem hiding this comment.
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.