From 98b060d19d645481ce23bac817c47e2e61afe1da Mon Sep 17 00:00:00 2001 From: Tom Gross Date: Fri, 21 Feb 2020 18:06:18 +0100 Subject: [PATCH] Fix upload of multiple files from paths --- CHANGES.rst | 5 ++++- README.rst | 15 +++++++++++++++ src/pcloud/api.py | 9 ++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d5c68da..685bd09 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,8 +5,11 @@ Changelog 1.0a8 (unreleased) ------------------ -- Nothing changed yet. +- Fix upload of multiple files from paths + [tomgross] +- Document uploading of files + [tomgross] 1.0a7 (2020-02-20) ------------------ diff --git a/README.rst b/README.rst index 3d14bc2..ea3e6b2 100644 --- a/README.rst +++ b/README.rst @@ -34,6 +34,21 @@ Usage of PyFilesystem with opener >>> opener.open_fs('pcloud://email%40example.com:SecretPassword@/') +Uploading files + +a) from filenames: + + >>> pc.uploadfile(files=['/full/path/to/image1.jpg', '/Users/tom/another/image.png'] + +b) from data: + + >>> import io + >>> from PIL import Image + >>> img = Image.open('image.jpg', 'r') + >>> bio = io.BytesIO() + >>> img.save(bio, format='jpeg') + >>> pc.uploadfile(data=bio.getvalue(), filename="image.jpg") + Documentation ------------- diff --git a/src/pcloud/api.py b/src/pcloud/api.py index 2a2f9b7..36e24e4 100644 --- a/src/pcloud/api.py +++ b/src/pcloud/api.py @@ -1,6 +1,5 @@ from hashlib import sha1 from io import BytesIO -from os.path import basename from pcloud.validate import RequiredParameterCheck import argparse @@ -125,12 +124,8 @@ def uploadfile(self, **kwargs): data='Hello pCloud', filename='foo.txt' """ if "files" in kwargs: - files = {} - upload_files = kwargs.pop("files") - for f in upload_files: - filename = basename(f) - files = {filename: open(f, "rb")} - # kwargs['filename'] = filename + upload_files = kwargs.pop("files", []) + files = [("file", open(f, "rb")) for f in upload_files] else: # 'data' in kwargs: files = { "f": (kwargs.pop("filename", "data-upload.bin"), kwargs.pop("data"))