Skip to content

Commit

Permalink
Python MultipartEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
laoshanxi committed Jun 25, 2022
1 parent bf2fdfb commit 1b8d553
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/sdk/python/appmesh_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ def delete_role(self, role_name) -> bool:
########################################
# Tag management API
########################################
def add_tag(self, tag_name, tag_value):
def add_tag(self, tag_name, tag_value) -> bool:
"""
Add a tag(label) for current logon node
Expand All @@ -672,7 +672,7 @@ def add_tag(self, tag_name, tag_value):
query={"value": tag_value},
path="/appmesh/label/{0}".format(tag_name),
)
return (resp.status_code == HTTPStatus.OK), resp.json()[REST_TEXT_MESSAGE_JSON_KEY]
return resp.status_code == HTTPStatus.OK

def remove_tag(self, tag_name) -> bool:
"""
Expand Down Expand Up @@ -743,14 +743,21 @@ def download(self, file_path, local_file) -> bool:
if resp.headers.__contains__("File-User") and resp.headers.__contains__("File-Group"):
file_uid = int(resp.headers["File-User"])
file_gid = int(resp.headers["File-Group"])
os.chown(path=local_file, uid=file_uid, gid=file_gid)
try:
os.chown(path=local_file, uid=file_uid, gid=file_gid)
except Exception as ex:
print(ex)
return True
return False

def upload(self, file_path, local_file):
"""
Upload a local file to remote, remote file will have the same permission with local file
Dependency:
----------
sudo apt install python3-pip
pip install requests-toolbelt
Parameters
----------
file_path : str
Expand All @@ -760,19 +767,23 @@ def upload(self, file_path, local_file):
-------
Success : bool
"""
from requests_toolbelt import MultipartEncoder

with open(file=local_file, mode="rb") as fp:
encoder = MultipartEncoder(fields={"filename": os.path.basename(file_path), "file": ("filename", fp, "application/octet-stream")})
file_stat = os.stat(local_file)
header = {}
header["File-Path"] = file_path
header["File-Mode"] = str(file_stat.st_mode)
header["File-User"] = str(file_stat.st_uid)
header["File-Group"] = str(file_stat.st_gid)
header["Content-Type"] = encoder.content_type
# https://stackoverflow.com/questions/22567306/python-requests-file-upload
resp = self.__request_http(
AppMeshClient.Method.POST_STREAM,
path="/appmesh/file/upload",
header=header,
body=fp,
body=encoder,
)
if resp.status_code == HTTPStatus.OK:
return True, ""
Expand Down
2 changes: 1 addition & 1 deletion test/python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
print(client.set_log_level("DEBUG"))
# file
print(client.download("/opt/appmesh/log/server.log", "1.log"))
print(client.upload(local_file="/opt/appmesh/log/server.log", file_path="/tmp/2.log"))
print(client.upload(local_file="1.log", file_path="/tmp/2.log"))
# cloud
print(json.dumps(client.get_cloud_apps(), indent=2))
print(
Expand Down

0 comments on commit 1b8d553

Please sign in to comment.