Skip to content
Snippets Groups Projects
Commit 4adc253d authored by Philipp Goymann's avatar Philipp Goymann
Browse files

Merge branch 'unstabel' into 'master'

Unstabel

See merge request !6
parents cccc2305 24bffb46
No related branches found
No related tags found
1 merge request!6Unstabel
Pipeline #362569 passed
......@@ -3,6 +3,7 @@
import argparse
import sys
import os
#own functions
from loosolab_s3.Utils import Utils
......@@ -41,7 +42,10 @@ def run_s3_functions():
if args.upload:
utils.check_argparser(args, ['secret', "key", "files", "bucketname"])
s3.upload_s3_objects(args.bucketname, args.files)
if args.download:
utils.check_argparser(args, ['secret', "key", "files", "bucketname"])
s3.download_s3_objects( args.bucketname, args.files, destination=os.getcwd())
#--------------------------------------------------------------------------------------------------------#
# parse command line arguments:
def argparsefunc():
......@@ -54,6 +58,7 @@ def argparsefunc():
#actions
parser.add_argument("--upload", action='store_true', help="upload files to bucket")
parser.add_argument("--download", action='store_true', help="download files to bucket")
parser.add_argument("--bucket_exists", action='store_true', help="check if bucket exists")
#values
......@@ -66,6 +71,7 @@ def argparsefunc():
if args.show_help:
parser.print_help(sys.stderr)
sys.exit(1)
if len(sys.argv)==1:
parser.print_help(sys.stderr)
sys.exit(1)
......
......@@ -26,19 +26,24 @@ class Loosolab_s3:
def __init__(self, credentials, multipart_upload=True, logger=True):
if logger:
level = logging.INFO
level = 20
else:
level = logging.CRITICAL
self.logger = logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=level)
level = 50
self.logger = logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.logger = logging.getLogger('Loosolab_s3_logger')
self.logger.setLevel(level)
#self.logger = logging.getLogger().setLevel(level)
# having a global session (needed for all boto action)
self.create_s3_session(credentials)
if not multipart_upload:
self.create_s3_transfer(credentials)
self.multipart_upload = multipart_upload
def __exception_log__(self, information, exception_e=""):
''' raise exception and logg as ERROR
exception: e
......@@ -452,7 +457,7 @@ class Loosolab_s3:
return True
#--------------------------------------------------------------------------------------------------------#
def download_s3_objects(self, bucket_name, file_list, destination='.', compare=True):
def download_s3_objects(self, bucket_name, file_list, destination=os.getcwd(), compare=True):
""" Download files from bucket
Parameter:
----------
......@@ -466,6 +471,20 @@ class Loosolab_s3:
bucket = self.session.Bucket(bucket_name)
modBool = False
# check if exists
for local_file in file_list:
file_name = os.path.basename(local_file)
file_path = os.path.join(destination, local_file)
if compare:
modBool = self.compare_s3_etag(bucket_name, file_name, file_path)
if self.check_s3_object_ex(bucket_name, file_name) and not modBool:
if not os.path.exists(os.path.dirname(file_path)):
print(file_path)
os.makedirs(os.path.dirname(file_path))
self.logger.info('Created directory: ' + os.path.dirname(file_path))
if self.multipart_upload:
bucket.download_file(file_name, file_path)
else:
bucket.download_file(file_name, file_path, Config=self.transfer)
try:
for local_file in file_list:
file_name = os.path.basename(local_file)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment