def transfer_fileshare_to_blob(config, fileshare_uri, output_model_name): ''' NB -- transfer proceeds via local temporary file! ''' file_service = FileService(config.storage_account_name, config.storage_account_key) blob_service = BlockBlobService(config.storage_account_name, config.storage_account_key) blob_service.create_container(config.container_trained_models) blob_service.create_container(config.predictions_container) uri_core = fileshare_uri.split('.file.core.windows.net/')[1].split('?')[0] fields = uri_core.split('/') fileshare = fields.pop(0) subdirectory = '/'.join(fields[:-1]) file_name = '{}/{}'.format(output_model_name, fields[-1]) with TemporaryFile() as f: file_service.get_file_to_stream(share_name=fileshare, directory_name=subdirectory, file_name=fields[-1], stream=f) f.seek(0) if 'predictions' in fields[-1]: blob_service.create_blob_from_stream( config.predictions_container, '{}_predictions_test_set.csv'.format(output_model_name), f) else: blob_service.create_blob_from_stream( config.container_trained_models, file_name, f) return
def openGFWListFile(self): azureFileService = FileService(account_name=self.azureAccountName, account_key=self.azureAccountKey) # the following snippet creates a file share and a directory # azureFileService.create_share('myshare') # azureFileService.create_directory('myshare', 'GFWListEditor') # this scans the file share for all directories # generator = azureFileService.list_directories_and_files('myshare') # for fileOrDir in generator: # print(fileOrDir.name) # this uploads a file to the target directory # azureFileService.create_file_from_path('myshare', 'GFWListEditor', self.gfwlistFile.rsplit('/', 1)[1], self.gfwlistFile) # this downloads a file to a stream gfwlistFileStream = BytesIO() azureFileService.get_file_to_stream(self.azureFileShareName, self.azureFileShareFileDir, self.azureFileShareFileName, gfwlistFileStream) gfwlistFileStream.seek(0) return TextIOWrapper(gfwlistFileStream)
def get_file(share_name, path_to_file): try: account_name, account_key = get_auth(request.authorization) directory_name, file_name = get_location(path_to_file) file_service = FileService(account_name=account_name, account_key=account_key) f_stream = io.BytesIO() file_service.get_file_to_stream(share_name, directory_name, file_name, f_stream, max_connections=6) f_stream.seek(0) return send_file(f_stream, attachment_filename=file_name, as_attachment=True) except Exception as e: logger.exception(e) return abort(500, e)