I am trying to make a route in flask which zips all the files under file share home directory and sends the zip as response. The code I have has some issue but I am not sure what is wrong here. The response is received by reactJS to download the zip.
@files_blueprint.route('/download/all', methods=['GET'])
def download_all():
print("-------------downloading all files-----------")
parent_dir = ShareDirectoryClient.from_connection_string(conn_str=connection_string, share_name=my_share, directory_path="")
my_list = list(parent_dir.list_directories_and_files())
print(my_list[0])
# Get a reference to the file share
share_client = service.get_share_client(file_share_name)
# Create a temporary directory to store the zip file
temp_dir = '/tmp'
os.makedirs(temp_dir, exist_ok=True)
# Create a zip archive containing all the files from the root directory of the share
zip_filename = 'zipped_share.zip'
with ZipFile(os.path.join(temp_dir, zip_filename), 'w') as zipf:
for file in share_client.get_directory_client('').list_directories_and_files():
file_name = file.name
file_path = os.path.join(temp_dir, file_name)
file_client = share_client.get_file_client(file_name)
with open(file_path, 'wb') as local_file:
file_client.download_file().readinto(local_file)
zipf.write(file_path, file_name)
# Send the zip file as a response
response = send_file(os.path.join(temp_dir, zip_filename), as_attachment=True)
# Clean up the temporary directory and files
shutil.rmtree(temp_dir)
return response
You can use the below Python code to route in Flask the list of files & download them as a zip folder and send them reactJS application.
Code:
The above code downloads the list of files from Azure file share saves it as a zip sends it to the react app and deletes the temporary path.
In react app under the src folder the
App.js
is likeApp.js
My flask app output:
Front end output:
Atlast in browser: