Deleting files from blob - TypeError: quote_from_bytes() expected bytes
Deleting files from blob - TypeError: quote_from_bytes() expected bytes
我在名为 data:
的容器中有一些文件
folder1/somepath/folder2/output/folder3/my_file1.csv
folder1/somepath/folder2/output/folder3/my_file4.csv
folder1/somepath/folder2/output/folder3/my_file23.csv
我有以下代码:
file_names_prefix = os.path.join('folder1/somepath/','folder2','output','folder3','my_file')
client = BlobServiceClient('https://mystoragename.blob.core.windows.net',credential=ManagedIdentityCredential()).get_container_client('data')
blob_list = client.list_blobs(name_starts_with=file_names_prefix)
file_list = [blob.name for blob in blob_list]
上面的代码产生以下输出:
['folder1/somepath/folder2/output/folder3/my_file1.csv',
'folder1/somepath/folder2/output/folder3/my_file4.csv',
'folder1/somepath/folder2/output/folder3/my_file23.csv']
但是当尝试使用以下方法删除这些文件时:
client.delete_blobs(file_list)
出现错误:
TypeError Traceback (most recent call last)
/tmp/ipykernel_2376/712121654.py in
----> 1 client.delete_blobs(file_list)
/anaconda/envs/azureml_py38/lib/python3.8/site-packages/azure/core/tracing/decorator.py in wrapper_use_tracer(*args, **kwargs)
81 span_impl_type = settings.tracing_implementation()
82 if span_impl_type is None:
---> 83 return func(*args, **kwargs)
84
85 # Merge span is parameter is set, but only if no explicit parent are passed
/anaconda/envs/azureml_py38/lib/python3.8/site-packages/azure/storage/blob/_container_client.py in delete_blobs(self, *blobs, **kwargs)
1298 return iter(list())
1299
-> 1300 reqs, options = self._generate_delete_blobs_options(*blobs, **kwargs)
1301
1302 return self._batch_send(*reqs, **options)
/anaconda/envs/azureml_py38/lib/python3.8/site-packages/azure/storage/blob/_container_client.py in _generate_delete_blobs_options(self, *blobs, **kwargs)
1206 req = HttpRequest(
1207 "DELETE",
-> 1208 "/{}/{}{}".format(quote(container_name), quote(blob_name, safe='/~'), self._query_str),
1209 headers=header_parameters
1210 )
/anaconda/envs/azureml_py38/lib/python3.8/urllib/parse.py in quote(string, safe, encoding, errors)
817 if errors is not None:
818 raise TypeError("quote() doesn't support 'errors' for bytes")
--> 819 return quote_from_bytes(string, safe)
820
821 def quote_plus(string, safe='', encoding=None, errors=None):
/anaconda/envs/azureml_py38/lib/python3.8/urllib/parse.py in quote_from_bytes(bs, safe)
842 """
843 if not isinstance(bs, (bytes, bytearray)):
--> 844 raise TypeError("quote_from_bytes() expected bytes")
845 if not bs:
846 return ''
TypeError: quote_from_bytes() expected bytes
有人可以帮忙吗?
错误是由于权限不足。 Azure 使用 Shared Access Singatures[SAS] 令牌和角色来保护 Azure Blob 存储对象,例如容器和 blob。上面的代码片段使用默认凭据,它具有对正在使用的 Blob 容器的读取和列表访问权限,但是该用户没有删除 Blob 的正确角色。检查 Azure documentation 以了解允许删除 blob 的 RBAC 角色。
为了 delete a blob
,角色需要存在的 RBAC 操作是 Microsoft.Storage/storageAccounts/blobServices/containers/blobs/delete.
有关 RBAC actions
的完整列表,请参阅 Azure 文档
参考这个 SO 。
我尝试了各种方法,但没有任何效果。最终循环删除文件。
for file in file_list:
client.delete_blob(file)
我在名为 data:
的容器中有一些文件folder1/somepath/folder2/output/folder3/my_file1.csv
folder1/somepath/folder2/output/folder3/my_file4.csv
folder1/somepath/folder2/output/folder3/my_file23.csv
我有以下代码:
file_names_prefix = os.path.join('folder1/somepath/','folder2','output','folder3','my_file')
client = BlobServiceClient('https://mystoragename.blob.core.windows.net',credential=ManagedIdentityCredential()).get_container_client('data')
blob_list = client.list_blobs(name_starts_with=file_names_prefix)
file_list = [blob.name for blob in blob_list]
上面的代码产生以下输出:
['folder1/somepath/folder2/output/folder3/my_file1.csv',
'folder1/somepath/folder2/output/folder3/my_file4.csv',
'folder1/somepath/folder2/output/folder3/my_file23.csv']
但是当尝试使用以下方法删除这些文件时:
client.delete_blobs(file_list)
出现错误:
TypeError Traceback (most recent call last) /tmp/ipykernel_2376/712121654.py in ----> 1 client.delete_blobs(file_list)
/anaconda/envs/azureml_py38/lib/python3.8/site-packages/azure/core/tracing/decorator.py in wrapper_use_tracer(*args, **kwargs) 81 span_impl_type = settings.tracing_implementation() 82 if span_impl_type is None: ---> 83 return func(*args, **kwargs) 84 85 # Merge span is parameter is set, but only if no explicit parent are passed
/anaconda/envs/azureml_py38/lib/python3.8/site-packages/azure/storage/blob/_container_client.py in delete_blobs(self, *blobs, **kwargs) 1298 return iter(list()) 1299 -> 1300 reqs, options = self._generate_delete_blobs_options(*blobs, **kwargs) 1301 1302 return self._batch_send(*reqs, **options)
/anaconda/envs/azureml_py38/lib/python3.8/site-packages/azure/storage/blob/_container_client.py in _generate_delete_blobs_options(self, *blobs, **kwargs) 1206 req = HttpRequest( 1207 "DELETE", -> 1208 "/{}/{}{}".format(quote(container_name), quote(blob_name, safe='/~'), self._query_str), 1209 headers=header_parameters 1210 )
/anaconda/envs/azureml_py38/lib/python3.8/urllib/parse.py in quote(string, safe, encoding, errors) 817 if errors is not None: 818 raise TypeError("quote() doesn't support 'errors' for bytes") --> 819 return quote_from_bytes(string, safe) 820 821 def quote_plus(string, safe='', encoding=None, errors=None):
/anaconda/envs/azureml_py38/lib/python3.8/urllib/parse.py in quote_from_bytes(bs, safe) 842 """ 843 if not isinstance(bs, (bytes, bytearray)): --> 844 raise TypeError("quote_from_bytes() expected bytes") 845 if not bs: 846 return ''
TypeError: quote_from_bytes() expected bytes
有人可以帮忙吗?
错误是由于权限不足。 Azure 使用 Shared Access Singatures[SAS] 令牌和角色来保护 Azure Blob 存储对象,例如容器和 blob。上面的代码片段使用默认凭据,它具有对正在使用的 Blob 容器的读取和列表访问权限,但是该用户没有删除 Blob 的正确角色。检查 Azure documentation 以了解允许删除 blob 的 RBAC 角色。
为了 delete a blob
,角色需要存在的 RBAC 操作是 Microsoft.Storage/storageAccounts/blobServices/containers/blobs/delete.
有关 RBAC actions
的完整列表,请参阅 Azure 文档参考这个 SO
我尝试了各种方法,但没有任何效果。最终循环删除文件。
for file in file_list:
client.delete_blob(file)