이번 포스트의 주제는 파이썬으로 azure 서버 blob 스토리지 관리 입니다.

azure 에서 blob 스토리지를 생성하고 관리하는 과정은 다음 포스트에서 단계적으로 다룰 예정이며, 이번 포스트에서는 azure blob 스토리지에 있는 파일들을 로컬에서 저장하는 역할을 하는 파이썬 스크립트에 대해 작성하도록 하겠습니다!

 

먼저, 테스팅한 환경 정보입니다!

 

- 우분투 18.04 lts 환경

- 파이썬 패키지(pip)가 설치되어 있고, 파이썬 2.x 활용

 

파이썬 스크립트 작성 전, 확인해야하는 요소들

- account_name ( 여기서 name에 있는 정보들이 account_name 입니다. )

 

 

- access keys ( 여기서 account_name과 동시에 access keys 정보를 확인할 수 있습니다. )

해당 탭은 storage accounts 탭에서 특정 account에 접근 시 활성화되는 관리 창입니다.

( 관리 창이라는 표현이 맞는 지 모르겠습니다ㅠ 혹시 적절한 표현을 아시는 분은 피드백주시면 바로 반영하도록 하겠습니다. )

 

 

 

- container_name

특정 account에 접근하신 다음, 아래와 같이 blobs 탭이 창에 활성화되실텐데, 여기서 blobs에 접근하시면,

아래와 같은 정보들이 보여지게 됩니다. 여기서 name에 있는 정보들이 container_name 입니다.

 

 

 

 

다음은 azure blob 스토리지로부터의 파일 다운로드를 위한 파이썬 스크립트입니다.

from azure.storage.blob import BlockBlobServiceimport os

#connect azure blob storage
#azure blob storage와의 연동
block_blob_service = BlockBlobService(account_name='<account name>', account_key='<access keys>')
 
#set the saved file information
#로컬에서의 파일 저장 경로 설정
local_path = '<file path>'

#azure blob 스토리지 내의 저장할 파일 정보 설정
local_file_name = '<file name>'container_name = '<container_name>'

full_path_to_file = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))print("\nDownloading blob to " + full_path_to_file)block_blob_service.get_blob_to_path(container_name, local_file_name, full_path_to_file)

 

Reference

참고자료

 

Microsoft Azure Storage Library for Python

https://azure-storage.readthedocs.io

https://github.com/Azure/azure-storage-python

 

반응형

+ Recent posts