5
5
import sys
6
6
from threading import Thread
7
7
from concurrent .futures import ThreadPoolExecutor
8
+ from typing import Callable
8
9
9
10
10
11
class DiffgramDatasetIterator :
@@ -15,13 +16,15 @@ class DiffgramDatasetIterator:
15
16
file_cache : dict
16
17
_internal_file_list : list
17
18
current_file_index : int
19
+ custom_signer_fn : Callable
18
20
19
21
def __init__ (self ,
20
22
project ,
21
23
diffgram_file_id_list ,
22
24
validate_ids = True ,
23
25
max_size_cache = 1073741824 ,
24
- max_num_concurrent_fetches = 25 ):
26
+ max_num_concurrent_fetches = 25 ,
27
+ custom_signer_fn = None ):
25
28
"""
26
29
27
30
:param project (sdk.core.core.Project): A Project object from the Diffgram SDK
@@ -30,6 +33,7 @@ def __init__(self,
30
33
self .diffgram_file_id_list = []
31
34
self .max_size_cache = 1073741824
32
35
self .pool = None
36
+ self .custom_signer_fn = custom_signer_fn
33
37
self .file_cache = {}
34
38
self ._internal_file_list = []
35
39
self .current_file_index = 0
@@ -118,16 +122,24 @@ def __validate_file_ids(self):
118
122
raise Exception (
119
123
'Some file IDs do not belong to the project. Please provide only files from the same project.' )
120
124
125
+ def set_custom_url_signer (self , signer_fn : Callable ):
126
+ self .custom_signer_fn = signer_fn
127
+
121
128
def get_image_data (self , diffgram_file ):
122
129
MAX_RETRIES = 10
123
130
image = None
124
131
if hasattr (diffgram_file , 'image' ):
125
132
for i in range (0 , MAX_RETRIES ):
126
133
try :
134
+ url = None
127
135
if diffgram_file .image :
128
136
url = diffgram_file .image .get ('url_signed' )
129
- if url :
130
- image = imread (diffgram_file .image .get ('url_signed' ))
137
+ if diffgram_file .image and self .custom_signer_fn is not None :
138
+ blob_path = diffgram_file .image ['url_signed_blob_path' ]
139
+ bucket_name = diffgram_file .image ['bucket_name' ]
140
+ url = self .custom_signer_fn (blob_path , bucket_name )
141
+ if url :
142
+ image = imread (url )
131
143
break
132
144
except Exception as e :
133
145
if i < MAX_RETRIES - 1 :
0 commit comments