def delete(self, path): if self.read_only: raise TransportException("READ ONLY TRANSPORT: Method not allowed") key = self.normalize(path) blob_client = self.service_client.get_blob_client( self.blob_container, key) self.with_retries(blob_client.delete_blob)
def get(self, path): path = self.normalize(path) resp = self.session.get(path, auth=self.auth, cert=self.pki, verify=self.verify) if resp.ok: return resp.content else: raise TransportException("[%s] %s: %s" % (resp.status_code, resp.reason, path))
def upload(self, src_path, dst_path): if self.read_only: raise TransportException("READ ONLY TRANSPORT: Method not allowed") key = self.normalize(dst_path) # if file exists already, it will be overwritten with open(src_path, "rb") as data: blob_client = self.service_client.get_blob_client( self.blob_container, key) try: self.with_retries(blob_client.upload_blob, data) except ResourceExistsError: pass
def put(self, dst_path, content): if self.read_only: raise TransportException("READ ONLY TRANSPORT: Method not allowed") key = self.normalize(dst_path) if isinstance(content, str): content = content.encode('utf-8') with BytesIO(content) as file_io: blob_client = self.service_client.get_blob_client( self.blob_container, key) try: self.with_retries(blob_client.upload_blob, file_io) except ResourceExistsError: pass
def download(self, src_path, dst_path): dir_path = os.path.dirname(dst_path) if not os.path.exists(dir_path): os.makedirs(dir_path) with open(dst_path, 'wb') as localfile: src_path = self.normalize(src_path) resp = self.session.get(src_path, auth=self.auth, cert=self.pki, verify=self.verify) if resp.ok: for chunk in resp.iter_content(chunk_size=1024): if chunk: localfile.write(chunk) else: raise TransportException( "[%s] %s: %s" % (resp.status_code, resp.reason, src_path))
def makedirs(self, path): raise TransportException("READ ONLY TRANSPORT: Method not implemented")
def put(self, dst_path, content): raise TransportException("READ ONLY TRANSPORT: Method not implemented")
def upload_batch(self, local_remote_tuples): raise TransportException("READ ONLY TRANSPORT: Method not implemented")
def upload(self, src_path, dst_path): raise TransportException("READ ONLY TRANSPORT: Method not implemented")
def new_func(self, *args, **kwargs): max_retry = 3 try_count = 0 while try_count < max_retry: try: if not self.ftp: if self.use_tls: self.ftp = ftplib.FTP_TLS() self.ftp.connect(self.host, port=self.port) self.ftp.auth() self.ftp.prot_p() else: self.ftp = ftplib.FTP() self.ftp.connect(self.host, port=self.port) self.ftp.login(self.user, self.password) self.ftp.sendcmd("TYPE I") try: self.ftp.voidcmd('site umask 002') except ftplib.Error: pass return func(self, *args, **kwargs) except ftplib.error_perm as e: if str(e).startswith('550'): raise msg = str(e) or "Unknown permission error" except ftplib.error_temp as e: msg = str(e) or "Unknown temporary error" except ftplib.error_reply as e: msg = str(e) or "Unknown reply error" except ftplib.Error as e: msg = str(e) or "Unknown FTP Error" except IOError as e: # May need to ignore other errors as well if e.errno not in (errno.EPIPE, errno.ECONNRESET): raise msg = "IOError #%s" % e.errno # Prevent any stale connection errors to show up in the warnings # as these error can happen often if the ftp connection is not used # enough. if msg.startswith('421') or msg.startswith( '425') or msg == "IOError #32" or msg == "IOError #104": self.log.info("FTP [%s]: %s" % (self.host, msg)) else: self.log.warning("FTP [%s]: %s" % (self.host, msg)) # The previous attempt at calling original func failed. # Reset the connection and try again. try: if self.ftp: self.ftp.close() # Just best effort. except ftplib.Error: pass time.sleep((2**try_count) / (2.00**(max_retry * 2))) self.ftp = None try_count += 1 raise TransportException("Max retries reach for function: %s(%s)" % (func.__name__, ", ".join(map(repr, args))))