from googleapiclient.http import MediaIoBaseUpload from googleapiclient.discovery import build from google.oauth2.credentials import Credentials import io API_KEY = 'AIzaSyDovEbWKKYX4JolYJ-sltEIJ_siWOZd51Q' # Initialize the Drive API client credentials = Credentials(None, api_key=API_KEY) service = build('drive', 'v3', credentials=credentials) def upload_blob_to_drive(blob, file_name): """ Uploads a blob to Google Drive. Parameters: blob (bytes): The blob data to upload. file_name (str): The name of the file to be created on Drive. """ # File metadata file_metadata = { 'name': file_name, 'mimeType': 'application/octet-stream' } # Create a media upload object for the blob media = MediaIoBaseUpload(io.BytesIO(blob), mimetype='application/octet-stream', resumable=True) # Create and execute the request file = service.files().create(body=file_metadata, media_body=media, fields='id').execute() print(f"File ID: {file.get('id')}") # Example usage # blob = b'Your blob data here' # upload_blob_to_drive(blob, 'example_file_name')