【问题标题】:Getting the error : "Retry policy did not allow for a retry" while trying to download contents from azure blob storage using the below python code尝试使用以下 python 代码从 azure blob 存储下载内容时出现错误:“重试策略不允许重试”
【发布时间】:2018-10-14 17:16:05
【问题描述】:

我正在使用以下 Python 代码递归下载内容:(例如,内容是 '/demo/test1' 演示是容器)

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,服务器请求 ID=e3660206-301e-002e-1c86-e36e5f000000,HTTP 状态码=404,异常=指定的 blob 没有 存在。错误代码:BlobNotFoundBlobNotFoundThe 指定的 blob 不 存在.RequestId:e3660206-301e-002e-1c86-e36e5f000000Time:2018-05-04T09:01:00.8232375Z.

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

【问题讨论】:

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


    【解决方案1】:

    查看 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 中列出了您遇到的相同错误。我建议添加重试以解决问题。

    【讨论】:

      猜你喜欢
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2020-07-09
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多