12345678910111213141516171819202122232425262728293031323334353637383940 |
- def upload_file_to_container(blob_storage_service_client: BlobServiceClient,
- container_name: str, file_path: str) -> batchmodels.ResourceFile:
- """
- Uploads a local file to an Azure Blob storage container.
- :param blob_storage_service_client: A blob service client.
- :param str container_name: The name of the Azure Blob storage container.
- :param str file_path: The local path to the file.
- :return: A ResourceFile initialized with a SAS URL appropriate for Batch
- tasks.
- """
- blob_name = os.path.basename(file_path)
- blob_client = blob_storage_service_client.get_blob_client(container_name, blob_name)
- print(f'Uploading file {file_path} to container [{container_name}]...')
- with open(file_path, "rb") as data:
- blob_client.upload_blob(data, overwrite=True)
- sas_token = generate_blob_sas(
- config.STORAGE_ACCOUNT_NAME,
- container_name,
- blob_name,
- account_key=config.STORAGE_ACCOUNT_KEY,
- permission=BlobSasPermissions(read=True),
- expiry=datetime.datetime.utcnow() + datetime.timedelta(hours=2)
- )
- sas_url = generate_sas_url(
- config.STORAGE_ACCOUNT_NAME,
- config.STORAGE_ACCOUNT_DOMAIN,
- container_name,
- blob_name,
- sas_token
- )
- return batchmodels.ResourceFile(
- http_url=sas_url,
- file_path=blob_name
- )
|