以下是问题详细信息:
- 我们能够将文件上传到Azure Blob存储。
- 我们还有一个Azure函数,该函数使用http触发器运行Python(python 3.6)应用程序(engine / init .py)(该函数本质上是获取文件并通过Azure Speaker运行它识别服务)
- 我们可以在本地计算机上运行代码,并且可以运行。
问题是,当我们将代码移至Azure云(配置为Linux计算机)并希望访问Blob存储中的文件时,该应用程序无法正常工作(返回错误500)。我们已经验证了路径是正确的(源和目标),我们可以在blob存储中看到该文件,但是我们无法读取Blob存储中的文件内容(即,我们无法读取.txt文件中的文本),并且我们无法将文件复制或下载到Azure Function目录中。
文件访问详细信息位于一个名为downloadFile.py的文件中(请参见下面的代码),该文件使用BlockBlobService与Azure Blob存储进行连接。
- 我们使用block_blob_service.list_blob查找文件
- 我们使用block_blob_service.get_blob_to_path下载文件
这两个函数在本地计算机上都可以正常工作,因为我们的requirements.txt文件中包含了azure-storage-blob库。
最终,我们的目标是我们需要python App来访问Blob存储中的文件并返回结果。我们对实现这一目标持开放态度。事先感谢您的帮助。
这是requirements.txt文件的内容:
azure-functions
azure-storage-blob == 1.5.0
这是我们的代码(downloadFile.py):
from azure.storage.blob import BlockBlobService,Publicaccess
import os,uuid,sys
def run_sample():
try:
# Create the BlockBlockService that is used to call the Blob service for the storage account
block_blob_service = BlockBlobService(account_name='',account_key='')
# Create a container called 'quickstartblobs'.
#container_name ='quickstartblobs'
container_name ='images'
#block_blob_service.create_container(container_name)
# Set the permission so the blobs are public.
#block_blob_service.set_container_acl(container_name,public_access=Publicaccess.Container)
# Create a file in Documents to test the upload and download.
__location__ = os.path.realpath(
os.path.join(os.getcwd(),os.path.dirname(__file__)))
#local_path = os.path.join(__location__,'SoundsForJay/')
local_path = os.path.join(__location__)
# not able to download file to azure function.
#local_path=os.path.abspath(os.path.curdir)
# List the blobs in the container
print("\nList blobs in the container")
generator = block_blob_service.list_blobs(container_name)
for blob in generator:
print("\t Blob name: " + blob.name)
if ".wav" in blob.name:
local_file_name = blob.name
# Download the blob(s).
# Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
#full_path_to_file2 = os.path.join(local_path,str.replace(local_file_name,'.txt','_DOWNLOADED.txt'))
full_path_to_file2 = os.path.join(local_path,'.wav','_DOWNLOADED.wav'))
print("\nDownloading blob to " + full_path_to_file2)
block_blob_service.get_blob_to_path(container_name,local_file_name,full_path_to_file2)
sys.stdout.write("Sample finished running. When you hit <any key>,the sample will be deleted and the sample "
"application will exit.")
sys.stdout.flush()
#input()
# Clean up resources. This includes the container and the temp files
#block_blob_service.delete_container(container_name)
#os.remove(full_path_to_file)
#os.remove(full_path_to_file2)
except Exception as e:
print(e)
return "run_sample is running."