|
12 | 12 | from googleapiclient.discovery import build |
13 | 13 | from googleapiclient.http import MediaIoBaseDownload |
14 | 14 | from google.auth import default |
| 15 | +from googleapiclient.errors import HttpError |
| 16 | + |
| 17 | + |
| 18 | +def test_file_accessibility(service, file_id: str) -> bool: |
| 19 | + """ |
| 20 | + Test different methods to access a file and provide detailed diagnostics. |
| 21 | + |
| 22 | + Args: |
| 23 | + service: Google Drive API service object |
| 24 | + file_id: The file ID to test |
| 25 | + |
| 26 | + Returns: |
| 27 | + True if file is accessible, False otherwise |
| 28 | + """ |
| 29 | + print(f"Testing accessibility for file ID: {file_id}") |
| 30 | + |
| 31 | + # Method 1: Try to get file metadata |
| 32 | + try: |
| 33 | + print("Method 1: Getting file metadata...") |
| 34 | + file_metadata = service.files().get(fileId=file_id).execute() |
| 35 | + print(f"✅ File metadata accessible: {file_metadata.get('name', 'Unknown')}") |
| 36 | + return True |
| 37 | + except HttpError as e: |
| 38 | + print(f"❌ Method 1 failed: {e}") |
| 39 | + if e.resp.status == 404: |
| 40 | + print(" → File not found (404)") |
| 41 | + elif e.resp.status == 403: |
| 42 | + print(" → Access forbidden (403)") |
| 43 | + else: |
| 44 | + print(f" → HTTP {e.resp.status}") |
| 45 | + |
| 46 | + # Method 2: Try to list files and search for this specific ID |
| 47 | + try: |
| 48 | + print("Method 2: Searching for file in accessible files...") |
| 49 | + results = service.files().list( |
| 50 | + q=f"id='{file_id}'", |
| 51 | + fields="files(id, name, mimeType, owners, permissions)" |
| 52 | + ).execute() |
| 53 | + files = results.get('files', []) |
| 54 | + if files: |
| 55 | + print(f"✅ File found in search: {files[0].get('name', 'Unknown')}") |
| 56 | + return True |
| 57 | + else: |
| 58 | + print("❌ File not found in search results") |
| 59 | + except Exception as e: |
| 60 | + print(f"❌ Method 2 failed: {e}") |
| 61 | + |
| 62 | + # Method 3: Try to get file permissions |
| 63 | + try: |
| 64 | + print("Method 3: Checking file permissions...") |
| 65 | + permissions = service.permissions().list(fileId=file_id).execute() |
| 66 | + print(f"✅ Permissions accessible: {len(permissions.get('permissions', []))} permission(s)") |
| 67 | + return True |
| 68 | + except HttpError as e: |
| 69 | + print(f"❌ Method 3 failed: {e}") |
| 70 | + if e.resp.status == 404: |
| 71 | + print(" → File not found (404)") |
| 72 | + elif e.resp.status == 403: |
| 73 | + print(" → Access forbidden (403)") |
| 74 | + |
| 75 | + print("❌ All access methods failed") |
| 76 | + return False |
15 | 77 |
|
16 | 78 |
|
17 | 79 | def download_file_from_drive(file_id: str, output_filename: str, service_account_key_file: str = None) -> bool: |
@@ -74,19 +136,35 @@ def download_file_from_drive(file_id: str, output_filename: str, service_account |
74 | 136 | print(f"Error listing files: {e}") |
75 | 137 | print("This might indicate insufficient permissions or API scope issues") |
76 | 138 |
|
77 | | - # 4. Get file metadata first |
78 | | - print(f"Getting file metadata for ID: {file_id}") |
| 139 | + # 4. Test file accessibility with detailed diagnostics |
| 140 | + if not test_file_accessibility(service, file_id): |
| 141 | + print("File accessibility test failed. Cannot proceed with download.") |
| 142 | + return False |
| 143 | + |
| 144 | + # 5. Get detailed file metadata |
| 145 | + print(f"Getting detailed file metadata for ID: {file_id}") |
79 | 146 | try: |
80 | 147 | file_metadata = service.files().get(fileId=file_id).execute() |
81 | 148 | print(f"File name: {file_metadata.get('name', 'Unknown')}") |
82 | 149 | print(f"File size: {file_metadata.get('size', 'Unknown')} bytes") |
| 150 | + print(f"File MIME type: {file_metadata.get('mimeType', 'Unknown')}") |
| 151 | + print(f"File owners: {[owner.get('emailAddress', 'Unknown') for owner in file_metadata.get('owners', [])]}") |
| 152 | + print(f"File permissions: {len(file_metadata.get('permissions', []))} permission(s)") |
| 153 | + |
| 154 | + # Show permission details |
| 155 | + permissions = file_metadata.get('permissions', []) |
| 156 | + if permissions: |
| 157 | + print("Permission details:") |
| 158 | + for perm in permissions: |
| 159 | + role = perm.get('role', 'Unknown') |
| 160 | + email = perm.get('emailAddress', 'Unknown') |
| 161 | + perm_type = perm.get('type', 'Unknown') |
| 162 | + print(f" - {email} ({perm_type}): {role}") |
| 163 | + else: |
| 164 | + print("No explicit permissions found") |
| 165 | + |
83 | 166 | except Exception as e: |
84 | 167 | print(f"Error getting file metadata: {e}") |
85 | | - print("This usually means:") |
86 | | - print("1. The file ID is incorrect") |
87 | | - print("2. The service account doesn't have access to the file") |
88 | | - print("3. The file has been deleted or moved") |
89 | | - print("4. The file is in a different Google account") |
90 | 168 | return False |
91 | 169 |
|
92 | 170 | # 4. Request file content |
|
0 commit comments