得到错误:"重试策略不允许重试"尝试使用下面的python代码从azure blob存储中下载内容

时间:2018-05-04 09:03:34

标签: python python-3.x azure azure-storage azure-storage-blobs

我使用以下python代码递归下载内容:(例如,内容为'/demo/test1' demo为容器)

def download_contents_azure(self,
                            session,
                            content,
                            dir_name) :
    """downloads the specified contents from Azure cloud
    Args :
        session       (obj)      --      Azure blob session object
        content       (list)     --      Part of the subclient content which has to be downloaded from the cloud
        dir_name      (str)      --      Name of the folder where the specified contents are to be downloaded

    Returns :
        None
    """
    os.mkdir(dir_name)
    for item in content :
        os.chdir(dir_name)
        path_to_file = ("/".join(item.strip("/").split('/')[1:]))
        container_name = Path(item).parts[1]
        generator = session.list_blobs(container_name)
        obj_list = []
        for j in generator :
            obj_list.append(j.name)
        if path_to_file == "" :
            self.download_container_azure(session,container_name)
            os.chdir(self.automation_directory)
        elif path_to_file in obj_list :
            if os.path.exists(container_name) is False:
                os.mkdir(container_name)
            os.chdir(container_name)
            head, tail = os.path.split("{}".format(path_to_file))
            if (os.path.isdir(os.getcwd()+ "/" + head)):
                try :
                    print(item)
                    session.get_blob_to_path(container_name,path_to_file,os.getcwd()+ "/" + head + "/" + tail)
                except azure.common.AzureMissingResourceHttpError:
                    self.log.error("exception")
            else:
                """create the diretcory and download the file to it"""
                os.makedirs(os.getcwd()+ "/" + head, exist_ok=True)
                try :
                    print(item)
                    session.get_blob_to_path(container_name,path_to_file,os.getcwd()+ "/" + head + "/" + tail)
                except azure.common.AzureMissingResourceHttpError:
                    self.log.error("exception")
        else :
            generator = session.list_blobs(container_name,path_to_file+'/',delimiter='/')
            self.log.info("got blobs in gen")
            if os.path.exists(container_name) is False:
                os.mkdir(container_name)
            os.chdir(container_name)
            """code below lists all the blobs in the container and downloads them one after another"""
            for blob in generator:
                """check if the path contains a folder structure, create the folder structure"""
                if "/" in "{}".format(blob.name):
                    """extract the folder path and check if that folder exists locally, and if not create it"""
                    head, tail = os.path.split("{}".format(blob.name))
                    if len(tail) != 0 :
                        if (os.path.isdir(os.getcwd()+ "/" + head)):
                            """download the files to this directory"""
                            try :
                                print(blob.name)
                                session.get_blob_to_path(container_name,blob.name,os.getcwd()+ "/" + head + "/" + tail)
                            except azure.common.AzureMissingResourceHttpError:
                                self.log.error("exception")
                        else:
                            """create the diretcory and download the file to it"""
                            os.makedirs(os.getcwd()+ "/" + head, exist_ok=True)
                            try :
                                print(blob.name)
                                session.get_blob_to_path(container_name,blob.name,os.getcwd()+ "/" + head + "/" + tail)
                            except azure.common.AzureMissingResourceHttpError:
                                self.log.error("exception")
                    else :
                        self.recur(session,container_name,blob.name)

                else:
                    try :
                        print(blob.name)
                        session.get_blob_to_path(container_name,blob.name,blob.name)
                    except azure.common.AzureMissingResourceHttpError:
                        self.log.error("exception")

        os.chdir(self.automation_directory)

我可以正确下载所有内容,但在下载后,我收到以下错误:

  

Client-Request-ID = aaaf7986-4f79-11e8-8e26-00155dbf7128重试政策   不允许重试:Server-Timestamp = Fri,04 May 2018 09:01:00   GMT,Server-Request-ID = e3660206-301e-002e-1c86-e36e5f000000,HTTP   状态代码= 404,异常=指定的blob不会   exist.ErrorCode:BlobNotFound BlobNotFound   指定的blob没有   exist.RequestId:e3660206-301e-002E-1c86-e36e5f000000Time:2018-05-04T09:01:00.8232375Z

如果我使用原生python并调用此方法,我没有看到任何异常。 有人可以帮我避免这个异常,以便我可以继续使用其余的代码吗?

1 个答案:

答案 0 :(得分:0)

查看storageclient.py,您收到该错误的原因是没有重试:https://github.com/Azure/azure-storage-python/blob/master/azure-storage-common/azure/storage/common/storageclient.py

github repo显示以下内容:

 # Determine whether a retry should be performed and if so, how 
            # long to wait before performing retry.
            retry_interval = self.retry(retry_context)
            if retry_interval is not None:
                # Execute the callback
                if self.retry_callback:
                    self.retry_callback(retry_context)

                logger.info(
                    "%s Retry policy is allowing a retry: Retry count=%s, Interval=%s.",
                    client_request_id_prefix,
                    retry_context.count,
                    retry_interval)

                # Sleep for the desired retry interval
                sleep(retry_interval)
            else:
                logger.error("%s Retry policy did not allow for a retry: "
                             "%s, HTTP status code=%s, Exception=%s.",
                             client_request_id_prefix,
                             timestamp_and_request_id,
                             status_code,
                             exception_str_in_one_line)
                raise ex

您获得的相同错误列在 logger.error 中。我建议添加一个应该解决问题的重试。