I am trying to copy an image from file storage to blob storage using python but unable to get it to work. I have based it on this node.js example Copy Azure File Share to Blob with node.js. The exception I get when running the python code locally is shown below.
azure.core.exceptions.ResourceNotFoundError: This request is not authorized to perform this operation using this resource type.
Here is a snippet of the python code.
sas_token = generate_account_sas(
account_name=account_name,
account_key=account_key,
resource_types=ResourceTypes(service=True),
permission=AccountSasPermissions(read=True),
expiry=datetime.utcnow() timedelta(hours=1)
)
blob_service_client = BlobServiceClient(account_url=blob_uri, credential=account_key)
container_client = blob_service_client.get_container_client("event")
share_directory_client = ShareDirectoryClient.from_connection_string(conn_str=conn_string,
share_name="test-share",
directory_path="")
dir_list = list(share_directory_client.list_directories_and_files())
logging.info(f"list directories and files : {dir_list}")
for item in dir_list:
if item['is_directory']:
pass
else:
fileClient = share_directory_client.get_file_client(item['name'])
source_url = fileClient.url "?" sas_token
logging.info(f"{source_url}")
container_client.get_blob_client(item['name']).start_copy_from_url(source_url)
The source url created using the SAS token is shown below.
https://<STORAGE_NAME>.file.core.windows.net/test-share/test.jpg?se=2021-10-01T05:46:22Z&sp=r&sv=2020-10-02&ss=f&srt=s&sig=<signature>
CodePudding user response:
The reason you're getting this error is because of incorrect resource type in your SAS token (srt
value).
Since your code is copying files to blob storage, you need to have object
as allowed resource type. Please see this link
to learn more about resource types.
Your code would look something like:
sas_token = generate_account_sas(
account_name=account_name,
account_key=account_key,
resource_types=ResourceTypes(object=True),
permission=AccountSasPermissions(read=True),
expiry=datetime.utcnow() timedelta(hours=1)
)
Your SAS URL should look something like:
https://<STORAGE_NAME>.file.core.windows.net/test-share/test.jpg?se=2021-10-01T05:46:22Z&sp=r&sv=2020-10-02&ss=f&srt=o&sig=<signature>