-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathfile_services.py
More file actions
507 lines (433 loc) · 17.9 KB
/
file_services.py
File metadata and controls
507 lines (433 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""This module is responsible for exposing the services defined at:
<https://rest-docs.synapse.org/rest/#org.sagebionetworks.repo.web.controller.EntityController>
"""
import json
import mimetypes
import os
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
from synapseclient.api.entity_services import get_upload_destination
from synapseclient.core import utils
from synapseclient.core.constants import concrete_types
from synapseclient.core.exceptions import (
SynapseAuthorizationError,
SynapseFileNotFoundError,
)
if TYPE_CHECKING:
from synapseclient import Synapse
async def post_file_multipart(
upload_request_payload: Dict[str, Any],
force_restart: bool,
endpoint: str,
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, str]:
"""
<https://rest-docs.synapse.org/rest/POST/file/multipart.html>
Arguments:
upload_request_payload: The request matching
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/MultipartRequest.html>
force_restart: Optional parameter. When True, any upload state for the given
file will be cleared and a new upload will be started.
endpoint: Server endpoint to call to.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
The requested multipart upload status matching
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/MultipartUploadStatus.html>
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
return await client.rest_post_async(
f"/file/multipart?forceRestart={str(force_restart).lower()}",
json.dumps(upload_request_payload),
endpoint=endpoint,
)
@dataclass
class AddPartResponse:
"""Result of a part add.
Attributes:
upload_id: The unique identifier of a multi-part request.
part_number: The part number of the add.
add_part_state: The state of this add.
error_message: If the added failed, this will contain the error message of the
cause. Will be None when the add is successful.
"""
upload_id: str
part_number: int
add_part_state: str
error_message: str
async def put_file_multipart_add(
upload_id: str,
part_number: int,
md5_hex: str,
*,
synapse_client: Optional["Synapse"] = None,
) -> AddPartResponse:
"""
<https://rest-docs.synapse.org/rest/PUT/file/multipart/uploadId/add/partNumber.html>
Arguments:
upload_id: The unique identifier of the file upload.
part_number: The part number to add. Must be a number between 1 and 10,000.
md5_hex: The MD5 of the uploaded part represented as a hexadecimal string. If
the provided MD5 does not match the MD5 of the uploaded part, the add
will fail.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
Object matching
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/AddPartResponse.html>
"""
try:
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
response = await client.rest_put_async(
f"/file/multipart/{upload_id}/add/{part_number}?partMD5Hex={md5_hex}",
endpoint=client.fileHandleEndpoint,
)
return AddPartResponse(
upload_id=response.get("uploadId", None),
part_number=response.get("partNumber", None),
add_part_state=response.get("addPartState", None),
error_message=response.get("errorMessage", None),
)
except Exception:
client.logger.exception(
f"Error adding part {part_number} to upload {upload_id} with MD5: {md5_hex}"
)
async def put_file_multipart_complete(
upload_id: str,
endpoint: str,
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, str]:
"""
<https://rest-docs.synapse.org/rest/PUT/file/multipart/uploadId/complete.html>
Arguments:
upload_id: The unique identifier of the file upload.
endpoint: Server endpoint to call to.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
Object matching
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/MultipartUploadStatus.html>
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
return await client.rest_put_async(
f"/file/multipart/{upload_id}/complete",
endpoint=endpoint,
)
async def post_file_multipart_presigned_urls(
upload_id: str,
part_numbers: List[int],
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, Any]:
"""
<https://rest-docs.synapse.org/rest/PUT/file/multipart/uploadId/add/partNumber.html>
Arguments:
upload_id: The unique identifier of the file upload.
part_numbers: The part numbers to get pre-signed URLs for.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
Object matching
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/AddPartResponse.html>
"""
from synapseclient import Synapse
uri = f"/file/multipart/{upload_id}/presigned/url/batch"
body = {
"uploadId": upload_id,
"partNumbers": part_numbers,
}
client = Synapse.get_client(synapse_client=synapse_client)
client.logger.debug(
f"Fetching presigned URLs for {part_numbers} with upload_id {upload_id}"
)
return await client.rest_post_async(
uri,
json.dumps(body),
endpoint=client.fileHandleEndpoint,
)
async def post_external_object_store_filehandle(
s3_file_key: str,
file_path: str,
storage_location_id: int,
mimetype: str = None,
md5: str = None,
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, Union[str, int]]:
"""
Create a new FileHandle representing an external object.
<https://rest-docs.synapse.org/rest/POST/externalFileHandle.html>
Arguments:
s3_file_key: S3 key of the uploaded object
file_path: The local path of the uploaded file
storage_location_id: The optional storage location descriptor
mimetype: The Mimetype of the file, if known.
md5: The file's content MD5, if known.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
A FileHandle for objects that are stored externally.
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/ExternalFileHandleInterface.html>
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
if mimetype is None:
mimetype, _ = mimetypes.guess_type(file_path, strict=False)
file_handle = {
"concreteType": concrete_types.EXTERNAL_OBJECT_STORE_FILE_HANDLE,
"fileKey": s3_file_key,
"fileName": os.path.basename(file_path),
"contentMd5": md5 or utils.md5_for_file(file_path).hexdigest(),
"contentSize": os.stat(file_path).st_size,
"storageLocationId": storage_location_id,
"contentType": mimetype,
}
return await client.rest_post_async(
"/externalFileHandle", json.dumps(file_handle), client.fileHandleEndpoint
)
async def post_external_filehandle(
external_url: str,
mimetype: str = None,
md5: str = None,
file_size: int = None,
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, Union[str, int]]:
"""
Create a new FileHandle representing an external object.
<https://rest-docs.synapse.org/rest/POST/externalFileHandle.html>
Arguments:
externalURL: An external URL
mimetype: The Mimetype of the file, if known.
md5: The file's content MD5.
file_size: The size of the file in bytes.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
A FileHandle for objects that are stored externally.
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/ExternalFileHandleInterface.html>
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
file_name = external_url.split("/")[-1]
external_url = utils.as_url(external_url)
file_handle = {
"concreteType": concrete_types.EXTERNAL_FILE_HANDLE,
"fileName": file_name,
"externalURL": external_url,
"contentMd5": md5,
"contentSize": file_size,
}
if mimetype is None:
mimetype, _ = mimetypes.guess_type(external_url, strict=False)
if mimetype is not None:
file_handle["contentType"] = mimetype
return await client.rest_post_async(
"/externalFileHandle", json.dumps(file_handle), client.fileHandleEndpoint
)
async def post_external_s3_file_handle(
bucket_name: str,
s3_file_key: str,
file_path: str,
parent: str = None,
storage_location_id: str = None,
mimetype: str = None,
md5: str = None,
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, Union[str, int, bool]]:
"""
Create an external S3 file handle for e.g. a file that has been uploaded directly to
an external S3 storage location.
<https://rest-docs.synapse.org/rest/POST/externalFileHandle/s3.html>
Arguments:
bucket_name: Name of the S3 bucket
s3_file_key: S3 key of the uploaded object
file_path: Local path of the uploaded file
parent: Parent entity to create the file handle in, the file handle will be
created in the default storage location of the parent. Mutually exclusive
with storage_location_id
storage_location_id: Explicit storage location id to create the file handle in,
mutually exclusive with parent
mimetype: Mimetype of the file, if known
md5: MD5 of the file, if known
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
The created file handle.
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/S3FileHandle.html>
Raises:
ValueError: If neither parent nor storage_location_id is specified, or if
both are specified.
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
if storage_location_id:
if parent:
raise ValueError("Pass parent or storage_location_id, not both")
elif not parent:
raise ValueError("One of parent or storage_location_id is required")
else:
upload_destination = await get_upload_destination(
entity_id=parent, synapse_client=client
)
storage_location_id = upload_destination["storageLocationId"]
if mimetype is None:
mimetype, _ = mimetypes.guess_type(file_path, strict=False)
file_handle = {
"concreteType": concrete_types.S3_FILE_HANDLE,
"key": s3_file_key,
"bucketName": bucket_name,
"fileName": os.path.basename(file_path),
"contentMd5": md5 or utils.md5_for_file(file_path).hexdigest(),
"contentSize": os.stat(file_path).st_size,
"storageLocationId": storage_location_id,
"contentType": mimetype,
}
return await client.rest_post_async(
"/externalFileHandle/s3",
json.dumps(file_handle),
endpoint=client.fileHandleEndpoint,
)
async def get_file_handle(
file_handle_id: Dict[str, Union[str, int]],
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, Union[str, int]]:
"""
Retrieve a fileHandle from the fileHandle service.
Note: You must be the creator of the filehandle to use this method.
Otherwise, an 403-Forbidden error will be raised.
<https://rest-docs.synapse.org/rest/GET/fileHandle/handleId.html>
Arguments:
file_handle_id: The ID of the file handle to look up.
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Returns:
A file handle retrieved from the file handle service.
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/FileHandle.html>
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
return await client.rest_get_async(
f"/fileHandle/{file_handle_id}", endpoint=client.fileHandleEndpoint
)
async def get_file_handle_for_download_async(
file_handle_id: str,
synapse_id: str,
entity_type: str = None,
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, str]:
"""
Gets the URL and the metadata as filehandle object for a filehandle or fileHandleId
Arguments:
file_handle_id: ID of fileHandle to download
synapse_id: The ID of the object associated with the file e.g. syn234
entity_type: Type of object associated with a file e.g. FileEntity,
TableEntity
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/FileHandleAssociateType.html>
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Raises:
SynapseFileNotFoundError: If the fileHandleId is not found in Synapse.
SynapseError: If the user does not have the permission to access the
fileHandleId.
Returns:
A dictionary with keys: fileHandle, fileHandleId and preSignedURL
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
body = {
"includeFileHandles": True,
"includePreSignedURLs": True,
"requestedFiles": [
{
"fileHandleId": file_handle_id,
"associateObjectId": synapse_id,
"associateObjectType": entity_type or "FileEntity",
}
],
}
response = await client.rest_post_async(
"/fileHandle/batch", body=json.dumps(body), endpoint=client.fileHandleEndpoint
)
result = response["requestedFiles"][0]
failure = result.get("failureCode")
if failure == "NOT_FOUND":
raise SynapseFileNotFoundError(
f"The fileHandleId {file_handle_id} could not be found"
)
elif failure == "UNAUTHORIZED":
raise SynapseAuthorizationError(
f"You are not authorized to access fileHandleId {file_handle_id} "
f"associated with the Synapse {entity_type}: {synapse_id}"
)
return result
def get_file_handle_for_download(
file_handle_id: str,
synapse_id: str,
entity_type: str = None,
*,
synapse_client: Optional["Synapse"] = None,
) -> Dict[str, str]:
"""
Gets the URL and the metadata as filehandle object for a filehandle or fileHandleId
Arguments:
file_handle_id: ID of fileHandle to download
synapse_id: The ID of the object associated with the file e.g. syn234
entity_type: Type of object associated with a file e.g. FileEntity,
TableEntity
<https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/file/FileHandleAssociateType.html>
synapse_client: If not passed in and caching was not disabled by
`Synapse.allow_client_caching(False)` this will use the last created
instance from the Synapse class constructor.
Raises:
SynapseFileNotFoundError: If the fileHandleId is not found in Synapse.
SynapseError: If the user does not have the permission to access the
fileHandleId.
Returns:
A dictionary with keys: fileHandle, fileHandleId and preSignedURL
"""
from synapseclient import Synapse
client = Synapse.get_client(synapse_client=synapse_client)
body = {
"includeFileHandles": True,
"includePreSignedURLs": True,
"requestedFiles": [
{
"fileHandleId": file_handle_id,
"associateObjectId": synapse_id,
"associateObjectType": entity_type or "FileEntity",
}
],
}
response = client.restPOST(
"/fileHandle/batch", body=json.dumps(body), endpoint=client.fileHandleEndpoint
)
result = response["requestedFiles"][0]
failure = result.get("failureCode")
if failure == "NOT_FOUND":
raise SynapseFileNotFoundError(
f"The fileHandleId {file_handle_id} could not be found"
)
elif failure == "UNAUTHORIZED":
raise SynapseAuthorizationError(
f"You are not authorized to access fileHandleId {file_handle_id} "
f"associated with the Synapse {entity_type}: {synapse_id}"
)
return result