以下是问题详细信息:
问题是,当我们将代码移至Azure云(配置为Linux计算机)并希望访问Blob存储中的文件时,该应用程序无法正常工作(返回错误500)。我们已经验证了路径是正确的(源和目标),我们可以在blob存储中看到该文件,但是我们无法读取Blob存储中的文件内容(即,我们无法读取.txt文件中的文本),并且我们无法将文件复制或下载到Azure Function目录中。
文件访问详细信息位于一个名为downloadFile.py的文件中(请参见下面的代码),该文件使用BlockBlobService与Azure Blob存储进行连接。
这两个函数在本地计算机上都可以正常工作,因为我们的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, str.replace(local_file_name ,'.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."
答案 0 :(得分:0)
现在,Python Azure功能不允许写入文件,它是只读模式,因此无法更改。因此,您无法使用get_blob_to_path,因为您可以将文件写入磁盘。
因此,如果您只想阅读文本文件的内容,则可以使用以下代码
filename = "test.txt"
account_name = "storage account"
account_key = "account key"
input_container_name="test"
block_blob_service = BlockBlobService(account_name, account_key)
blobstring = block_blob_service.get_blob_to_text(input_container_name, filename).content
您还可以使用函数blob binding读取内容,将inputblob绑定为流。
def main(req: func.HttpRequest,inputblob: func.InputStream) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(inputblob.read(size=-1))
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)