Azure Blob Storage로 파일을 올리고 SAS token을 생성하는 과정을 수행할 일이 생겼다.
예전에 C#으로 작성한 경험이 있는데, 이번에는 Python으로 해야 해서 기록을 남김.
Azure Blob Storage SAS token 생성
SAS token을 생성해 blob에 access 할 수 있도록 보안처리된 접근을 생성할 수 있다.
Grant limited access to data with shared access signatures (SAS) - Azure Storage | Microsoft Docs
URI 형태로 blob 링크를 생성 가능하고, 생성하면 일정 기간 token을 이용해 blob에 접근 가능하다.
Github 리포지토리
다음 작업에 연속적으로 필요할 것 같아 github 리포를 만들었다.
CloudBreadPaPa/blob-sas-token-gen: generate blob sas token (github.com)
def generate_sas_token(file_name): sas = generate_blob_sas(account_name=AZURE_ACC_NAME, account_key=AZURE_PRIMARY_KEY, container_name=AZURE_CONTAINER, blob_name=file_name, permission=BlobSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=2))
부분이 핵심이 되는 부분이다. Stack Overflow 참조
이렇게 SAS token 생성을 자동화 하면, 서비스를 개발할때,
- blob 업로드
- SAS token 생성
- 타 서비스에서 SAS token으로 blob에 접근
형태의 흐름으로 blob에 대한 접근 보안을 구성 가능하다. 이후 포스트에서는 ADX의 데이터를 Python으로 받아 Storage에 Blob으로 업로드 하고, SAS 토큰을 생성 후, Azure SQL Database에서 BULK INSERT 하는 과정을 진행 예정이다.
참고자료 :
Grant limited access to data with shared access signatures (SAS) - Azure Storage | Microsoft Docs
CloudBreadPaPa/blob-sas-token-gen: generate blob sas token (github.com)
How do you generate the signature for an Azure Blob storage SAS token in Python? - Stack Overflow