Skip to content

Commit afc8254

Browse files
author
Hamed Taheri
committed
CLI Azure app
1 parent 8ef1b21 commit afc8254

File tree

12 files changed

+268
-0
lines changed

12 files changed

+268
-0
lines changed

README.MD

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Installation
2+
3+
### Step1:
4+
5+
> Make sure python >= 3.6 has been installed
6+
7+
Install following packages using pip in terminal:
8+
(this package is `vital` to work this cli-app)
9+
10+
* pip install azure-storage-file
11+
12+
### Step2:
13+
14+
> Edit config.json file and put your Azure StorageAccount name, AccessKey->Key1 and FileShare(SMB) name in it.
15+
16+
```json
17+
{
18+
"configs": {
19+
"account_name": "YourAzureStorageAccountName",
20+
"account_key": "Key1 from AccessKey related to your StorageAccount",
21+
"file_share": "Name of your FileShare(Container) in StorageAccount"
22+
}
23+
}
24+
25+
```
26+
27+
Tutorial Link:
28+
[https://docs.microsoft.com/en-us/azure/storage/files/storage-how-to-create-file-share](https://docs.microsoft.com/en-us/azure/storage/files/storage-how-to-create-file-share)
29+
Azure Portal:
30+
[https://portal.azure.com/](https://portal.azure.com/)
31+
32+
33+
# Usage:
34+
35+
Open your OS Terminal ( like CMD in windows ) inside `Project directory (CLI directory)`.
36+
37+
Type below command for more info and help:
38+
39+
> python updwn.py --help
40+
41+
Type below command to upload `sample_imgs/lake.jpg` to `image/data` path in azure:
42+
43+
> python updwn.py --upload image/data sample_imgs/lake.jpg
44+
45+
Type below command to download `image/data/lake.jpg` in your computer to the current directory:
46+
47+
> python updwn.py --download image/data/lake.jpg
48+
49+
Type below command to see all files list:
50+
51+
> python updwn.py --list
52+
53+
----------------------------------------------
54+
55+
## Screenshot
56+
57+
![screenshot](Usage-prove.png)
58+
59+
60+
Full Stack Developer

Usage-prove.png

111 KB
Loading

azureFileManager.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
import os
3+
import re
4+
from azure.storage.file import FileService
5+
from config import Configuration
6+
7+
8+
class AzureFileManager():
9+
def __init__(self):
10+
# fetch config data
11+
conf = Configuration()
12+
# create Azure File share service
13+
self.file_service = FileService(
14+
account_name=conf.account_name, account_key=conf.account_key)
15+
# set azure share file name (container)
16+
self.file_share = conf.file_share
17+
18+
def upload_file(self, upload_path, file_path):
19+
if not os.path.isfile(file_path):
20+
print("Your file is not exists, check your file path and try again.")
21+
return
22+
filename = os.path.basename(file_path)
23+
# remove ' or " from path, if path was empty like "" or '' set upload_path=None, this make upload file to root directory
24+
upload_path = upload_path.strip().replace("'", '').replace('"', '')
25+
# remove start and end / or \
26+
if upload_path.endswith('/') or upload_path.endswith('\\'):
27+
upload_path = upload_path[:-1]
28+
if upload_path.startswith('/') or upload_path.startswith('\\'):
29+
upload_path = upload_path[1:]
30+
# sanity check
31+
upload_path = upload_path if len(upload_path) >= 1 else None
32+
33+
print("Start uploading...")
34+
try:
35+
# create sub directories
36+
self.create_sub_directories(upload_path)
37+
# upload
38+
self.file_service.create_file_from_path(
39+
share_name=self.file_share, # file_share name in azure
40+
directory_name=upload_path, # server directories address. None => root directory
41+
file_name=filename, # Name of file to create in azure
42+
local_file_path=file_path)
43+
print("'{0}' has been successfully uploaded".format(filename))
44+
except:
45+
print("Failed to upload '{0}', please try again".format(filename))
46+
47+
def download_file(self, file_path):
48+
""" download file from azure, enter file path in azure """
49+
# check file path was not empty
50+
file_path = file_path.strip().replace("'", '').replace('"', '')
51+
if len(file_path) == 0:
52+
print("Please enter a file path")
53+
return
54+
filename = os.path.basename(file_path)
55+
dir_path = os.path.dirname(file_path)
56+
# if parent path was not available, use None => root directory
57+
dir_path = dir_path if dir_path else None
58+
59+
print("Downloading...")
60+
try:
61+
self.file_service.get_file_to_path(
62+
share_name=self.file_share,
63+
directory_name=dir_path, # The path to the directory in azure
64+
file_name=filename, # Name of existing file in azure
65+
# Path of file to write to local machine
66+
file_path="{0}".format(filename))
67+
print(
68+
"'{0}' has been successfully downloaded and saved in current directory.".format(filename))
69+
except:
70+
print("Failed to download '{0}', either file doesn't exist or you are offline.".format(
71+
filename))
72+
73+
def get_list_of_files(self, dir_name=None):
74+
""" show list of all files and all directories in azure"""
75+
generator = self.file_service.list_directories_and_files(
76+
share_name=self.file_share,
77+
directory_name=dir_name)
78+
parent = "" if dir_name == None else dir_name
79+
for file_or_dir in generator:
80+
if not re.match(r"(.[a-z]*[A-Z]*[0-9]*)$", file_or_dir.name):
81+
# file
82+
if len(parent) == 0:
83+
print(file_or_dir.name)
84+
else:
85+
print("{0}/{1}".format(parent, file_or_dir.name))
86+
else:
87+
# dir
88+
if len(parent) == 0:
89+
self.get_list_of_files(file_or_dir.name)
90+
else:
91+
self.get_list_of_files(
92+
"{0}/{1}".format(parent, file_or_dir.name))
93+
94+
def create_sub_directories(self, path):
95+
""" create sub directories in Azure """
96+
if path is None:
97+
return
98+
dirs = os.path.normpath(path).split(os.path.sep)
99+
parent = ''
100+
for dir in dirs:
101+
parent += dir if len(parent) == 0 else '/'+dir
102+
self.file_service.create_directory(self.file_share, parent)

config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"configs": {
3+
"account_name": "YourAzureStorageAccountName",
4+
"account_key": "Key1 from AccessKey related to your StorageAccount",
5+
"file_share": "Name of your FileShare(Container) in StorageAccount"
6+
}
7+
}

config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import json
3+
4+
5+
class Configuration:
6+
def __init__(self):
7+
config = self.read_config('config.json')
8+
self.account_name = config["configs"]["account_name"]
9+
self.account_key = config["configs"]["account_key"]
10+
self.file_share = config["configs"]["file_share"]
11+
12+
def read_config(self, config_file):
13+
""" Read config.json and pass it as dictionary """
14+
data = {}
15+
with open(config_file, encoding='utf-8') as f:
16+
data = json.load(f)
17+
return data

lake.jpg

378 KB
Loading

sample_imgs/lake.jpg

378 KB
Loading

sample_imgs/mnt.jpg

66.1 KB
Loading

sample_imgs/rain.jpg

96 KB
Loading

sample_imgs/winter.jpg

122 KB
Loading

0 commit comments

Comments
 (0)