为简单起见,此示例使用任意.pkl而不是模型文件。
对象创建代码
import pickle
# Create a pickle
small_object = list(range(0,1000))
name = 'small_file.pkl'
with open(name, 'wb') as f:
pickle.dump(small_object, f)
#### Verify integrity ###
# Loading from disk
with open(name, 'rb') as f:
opened_object_one = pickle.load(f)
print(opened_object_one) # prints expected list
# Loading from byte stream
with open('small_object.pkl', 'rb') as f:
opened_object_two = f.read()
print(pickle.loads(opened_object_two)) # print expected list
振动功能代码
import logging
import pickle
app = func.FunctionApp()
@app.route(route="http_trigger", auth_level=func.AuthLevel.FUNCTION)
@app.blob_input(
arg_name = 'client',
path = '<container>/small_file.pkl',
connection ='AzureWebJobsStorage'
)
def http_trigger(req: func.HttpRequest, client) -> func.HttpResponse:
# `client` is of type azure.functions.blob.InputStream
package_bytes = client.read() # return bytes
client.close()
small_object = pickle.loads(package_bytes)
i使用
source .venv/scripts/activate
GitBash
)。然后,我在本地运行func host start
Exception: UnpicklingError: invalid load key, '\xef'
。
我尝试使用
joblib
,dill
和pickle
在读写阶段。我尝试将泡菜加载到blob上,并用Azure CLI
和Firefox
加载泡菜。我总是得到相同的结果。 我使用python 3.10.5.我在Windows 10上 所有的帮助。
我确实同意并收到类似的错误,作为替代方案,您可以使用以下代码:
function_app.py:
import logging as rilg
import pickle
from azure.storage.blob import BlobServiceClient as bsc
import azure.functions as func
rith_con = "DefaultEndpointsProtocol=https;AccountName=rithtest4647350688;AccountKey=FmrithikchotubJuUg==;EndpointSuffix=core.windows.net"
ri_con_nm = "rithwik"
rit_blb_nm = "small_file.pkl"
rithapp = func.FunctionApp()
@rithapp.route(route="http_trigger", auth_level=func.AuthLevel.FUNCTION)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
rilg.info("Hello Rithwik, reading the pkl file")
ri_bsc = bsc.from_connection_string(rith_con)
ri_cc = ri_bsc.get_container_client(ri_con_nm)
ri_bc = ri_cc.get_blob_client(rit_blb_nm)
ds = ri_bc.download_blob()
test_pk_byts = ds.readall()
rith_obj = pickle.loads(test_pk_byts)
return func.HttpResponse(f"Hello Rithwik Bojja, Successfully loaded: {rith_obj[:10]}", status_code=200)
Quirlements.txt:
azure-functions
azure-storage-blob
Azure存储帐户中的PKL文件: