|
19 | 19 | import urllib.parse
|
20 | 20 |
|
21 | 21 | import aiohttp
|
| 22 | +import google.api_core.exceptions |
22 | 23 |
|
23 | 24 | from clusterfuzz._internal.base import concurrency
|
24 |
| -from clusterfuzz._internal.base import retry |
25 | 25 | from clusterfuzz._internal.base import utils
|
26 | 26 | from clusterfuzz._internal.metrics import logs
|
27 | 27 |
|
28 |
| -BATCH_DELETE_URL = 'https://storage.googleapis.com/batch/storage/v1' |
29 |
| - |
30 |
| -MULTIPART_BOUNDARY = 'multi-part-boundary' |
31 |
| - |
32 | 28 |
|
33 | 29 | def download_urls(urls_and_filepaths: List[Tuple[str, str]]) -> List[bool]:
|
34 | 30 | """Downloads multiple urls to filepaths in parallel and asynchronously.
|
@@ -91,37 +87,57 @@ async def _async_download_file(session: aiohttp.ClientSession, url: str,
|
91 | 87 | fp.write(chunk)
|
92 | 88 |
|
93 | 89 |
|
94 |
| -@retry.wrap( |
95 |
| - retries=2, |
96 |
| - delay=1, |
97 |
| - function='system.fast_http.delete_gcs_blobs_batch', |
98 |
| - exception_types=[asyncio.TimeoutError], |
99 |
| - retry_on_false=True) |
100 |
| -async def delete_gcs_blobs_batch(session, bucket, blobs, auth_token): |
101 |
| - """Batch deletes |blobs| asynchronously.""" |
| 90 | +async def delete_blob_async(bucket_name, blob_name, session, auth_token): |
| 91 | + """Asynchronously deletes a GCS blob.""" |
| 92 | + blob_name = urllib.parse.quote(blob_name, safe='') |
| 93 | + url = ( |
| 94 | + f'https://storage.googleapis.com/storage/v1/b/{bucket_name}/o/{blob_name}' |
| 95 | + ) |
102 | 96 | headers = {
|
103 | 97 | 'Authorization': f'Bearer {auth_token}',
|
104 |
| - 'Content-Type': f'multipart/mixed; boundary={MULTIPART_BOUNDARY}' |
105 | 98 | }
|
106 |
| - # Build multipart body |
107 |
| - body = [] |
108 |
| - bucket = urllib.parse.quote(bucket, safe='') |
109 |
| - for idx, blob in enumerate(blobs): |
110 |
| - path = urllib.parse.quote(blob['name'], safe='') |
111 |
| - body.append(f'--{MULTIPART_BOUNDARY}\r\n' |
112 |
| - 'Content-Type: application/http\r\n' |
113 |
| - f'Content-ID: <item{idx+1}>\r\n\r\n' |
114 |
| - f'DELETE /storage/v1/b/{bucket}/o/{path} HTTP/1.1\r\n' |
115 |
| - 'Content-Length: 0\r\n\r\n' |
116 |
| - 'Host: storage.googleapis.com\r\n') |
117 |
| - body.append(f'--{MULTIPART_BOUNDARY}--\r\n') |
118 |
| - body = '\r\n'.join(body) |
119 | 99 |
|
120 | 100 | try:
|
121 |
| - async with session.post( |
122 |
| - BATCH_DELETE_URL, headers=headers, data=body, timeout=25) as response: |
123 |
| - response.raise_for_status() |
124 |
| - return True |
| 101 | + async with session.delete(url, headers=headers) as response: |
| 102 | + if response.status != 204: |
| 103 | + response_text = await response.text() |
| 104 | + logs.error(f'Failed to delete blob {blob_name}. Status code: ' |
| 105 | + f'{response.status} {response_text}') |
| 106 | + except google.api_core.exceptions.NotFound: |
| 107 | + logs.info(f'Not found: {blob_name} {response_text}') |
125 | 108 | except Exception as e:
|
126 |
| - logs.info(f'Failed to batch delete {e}') |
127 |
| - return False |
| 109 | + logs.error(f'Error deleting {blob_name}: {e}') |
| 110 | + |
| 111 | + |
| 112 | +async def list_blobs_async(bucket_name, path, auth_token): |
| 113 | + """Asynchronously lists blobs, yielding dicts containing their size, updated |
| 114 | + time and name.""" |
| 115 | + async with aiohttp.ClientSession() as session: |
| 116 | + url = f'https://storage.googleapis.com/storage/v1/b/{bucket_name}/o' |
| 117 | + params = { |
| 118 | + 'prefix': path, |
| 119 | + 'delimiter': '/', |
| 120 | + # Need token and save space in response. |
| 121 | + 'fields': 'items(name,size,updated),nextPageToken' |
| 122 | + } |
| 123 | + while True: |
| 124 | + async with session.get( |
| 125 | + url, headers={'Authorization': f'Bearer {auth_token}'}, |
| 126 | + params=params) as response: |
| 127 | + if response.status == 200: |
| 128 | + data = await response.json() |
| 129 | + items = data.get('items', []) |
| 130 | + for blob in items: |
| 131 | + yield { |
| 132 | + 'size': int(blob['size']), |
| 133 | + 'updated': blob['updated'], |
| 134 | + 'name': blob['name'], |
| 135 | + } |
| 136 | + |
| 137 | + next_page_token = data.get('nextPageToken') |
| 138 | + if not next_page_token: |
| 139 | + break |
| 140 | + params['pageToken'] = next_page_token |
| 141 | + else: |
| 142 | + logs.error(f'No blobsm, tatus code: {response.status}') |
| 143 | + break |
0 commit comments