使用 azure.storage.blob for python 2.7 將檔案上傳到 azure blob 存盤容器時出現問題。(我知道我應該使用更新的 python,但它是大型 ROS 應用程式的一部分,因此不僅僅是升級它。)
from azure.storage.blob import BlobServiceClient
...
container_name = "operationinput"
self.back_up_root = "~/backup/sql/lp/"
self.back_up_root = os.path.expanduser(self.back_up_root)
file = 'test.sql'
try:
client = BlobServiceClient.from_connection_string(conn_str=connection_string)
blob = client.get_blob_client(container='container_name', blob='datafile')
except Exception as err:
print(str(err))
with open(self.back_up_root file, "rb") as data:
blob.upload_blob(data)
我收到以下錯誤:
azure.core.exceptions.HttpResponseError: The specifed resource name contains invalid characters.
RequestId:3fcb6c26-101e-007e-596d-1c7d61000000
Time:2022-02-07T21:58:17.1308670Z
ErrorCode:InvalidResourceName
我發現的所有帖子都是指使用大寫字母左右的人,但我有: operationinput datafile
一切都應在規格范圍內。
有任何想法嗎?
uj5u.com熱心網友回復:
我們嘗試使用下面的示例代碼使用 SAS 令牌將檔案上傳到 Azure blob 存盤(容器),并且能夠成功實作。
代碼示例:-
from azure.storage.blob import BlobClient
upload_file_path="C:\\Users\\Desktop\\filename"
sas_url="https://xxx.blob.core.windows.nethttps://cloudsh3D?sastoken"
client = BlobClient.from_blob_url(sas_url)
with open(upload_file_path,'rb') as data:
client.upload_blob(data)
print("**file uploaded**")

要生成SAS url,connection string我們選擇如下:-


有關詳細資訊,請參閱此Microsoft 檔案:允許或禁止對存盤帳戶進行公共讀取訪問
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/428053.html
