【发布时间】: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 没有 存在。错误代码:BlobNotFound
BlobNotFoundThe 指定的 blob 不 存在.RequestId:e3660206-301e-002e-1c86-e36e5f000000Time:2018-05-04T09:01:00.8232375Z.
如果我使用本机 python 并调用此方法,我没有看到任何异常。 有人可以帮我避免这个异常,以便我可以继续执行其余的代码吗?
【问题讨论】:
标签: python python-3.x azure azure-storage azure-blob-storage