-
azure-12 storage 1 - blob storage 2 - python클라우드/azure 2023. 5. 6. 13:46
1. 컨테이너 생성
!pip install azure-storage-blob azure-identity Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting azure-storage-blob Downloading azure_storage_blob-12.16.0-py3-none-any.whl (387 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 388.0/388.0 kB 6.8 MB/s eta 0:00:00 Collecting azure-identity Downloading azure_identity-1.12.0-py3-none-any.whl (135 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 135.5/135.5 kB 8.4 MB/s eta 0:00:00 Collecting isodate>=0.6.1 Downloading isodate-0.6.1-py2.py3-none-any.whl (41 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 41.7/41.7 kB 3.3 MB/s eta 0:00:00 Requirement already satisfied: cryptography>=2.1.4 in /usr/local/lib/python3.9/dist-packages (from azure-storage-blob) (40.0.2) Requirement already satisfied: typing-extensions>=4.0.1 in /usr/local/lib/python3.9/dist-packages (from azure-storage-blob) (4.5.0) Collecting azure-core<2.0.0,>=1.26.0 Downloading azure_core-1.26.4-py3-none-any.whl (173 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 173.9/173.9 kB 15.9 MB/s eta 0:00:00 Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.9/dist-packages (from azure-identity) (1.16.0) Collecting msal-extensions<2.0.0,>=0.3.0 Downloading msal_extensions-1.0.0-py2.py3-none-any.whl (19 kB) Collecting msal<2.0.0,>=1.12.0 Downloading msal-1.22.0-py2.py3-none-any.whl (90 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.0/90.0 kB 5.6 MB/s eta 0:00:00 Requirement already satisfied: requests>=2.18.4 in /usr/local/lib/python3.9/dist-packages (from azure-core<2.0.0,>=1.26.0->azure-storage-blob) (2.27.1) Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.9/dist-packages (from cryptography>=2.1.4->azure-storage-blob) (1.15.1) Collecting PyJWT[crypto]<3,>=1.0.0 Downloading PyJWT-2.6.0-py3-none-any.whl (20 kB) Collecting portalocker<3,>=1.0 Downloading portalocker-2.7.0-py2.py3-none-any.whl (15 kB) Requirement already satisfied: pycparser in /usr/local/lib/python3.9/dist-packages (from cffi>=1.12->cryptography>=2.1.4->azure-storage-blob) (2.21) Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests>=2.18.4->azure-core<2.0.0,>=1.26.0->azure-storage-blob) (2.0.12) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests>=2.18.4->azure-core<2.0.0,>=1.26.0->azure-storage-blob) (2022.12.7) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests>=2.18.4->azure-core<2.0.0,>=1.26.0->azure-storage-blob) (1.26.15) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests>=2.18.4->azure-core<2.0.0,>=1.26.0->azure-storage-blob) (3.4) Installing collected packages: PyJWT, portalocker, isodate, azure-core, azure-storage-blob, msal, msal-extensions, azure-identity Successfully installed PyJWT-2.6.0 azure-core-1.26.4 azure-identity-1.12.0 azure-storage-blob-12.16.0 isodate-0.6.1 msal-1.22.0 msal-extensions-1.0.0 portalocker-2.7.0
blob storage 모듈과 인증 모듈을 불러온다.
import os, uuid from azure.identity import DefaultAzureCredential from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
필요한 모듈들을 불러온다.
connect_str = 'DefaultEndpointsProtocol=https;AccountName=;AccountKey=' blob_service_client = BlobServiceClient.from_connection_string(connect_str)
액세스 키에서 연결 문자열을 복사하여 변수를 생성한다.
container_name = 'testcontainer' container_client = blob_service_client.create_container(container_name)
컨테이너 이름을 지정하고 컨테이너를 생성한다.
컨테이너 탭에서 컨테이너가 지정된 이름으로 생성이 된 것을 확인할 수 있다.
2. 파일 업로드
local_path = './data' # 로컬 경로를 생성 os.mkdir(local_path) # 디렉토리를 생성 local_file_name = 'welcome' + '.txt' # 로컬 파일 생성 upload_file_path = os.path.join(local_path, local_file_name) # 로컬 파일 업로드 file = open(upload_file_path, mode='w') # 쓰기 모드로 파일 열기 file.write('Welcome to Python') # 내용 작성 file.close() # 파일 닫기
blob_client = blob_service_client.get_blob_client( container=container_name, blob=local_file_name) with open(file=upload_file_path, mode='rb') as data: blob_client.upload_blob(data)
rb는 binary 모드로 읽기이고 text로 올릴 시 파일이 깨질 수 있기 때문에 binary모드로 업로드한다.
3. 파일 다운로드
print('\nListing blobs') blob_list = container_client.list_blobs() for blob in blob_list: print('\t' + blob.name) Listing blobs welcome.txt
blob의 리스트를 확인한다.
download_file_path = os.path.join(local_path, str.replace(local_file_name,'.txt','_DOWNLOAD.txt')) print(download_file_path) container_client = blob_service_client.get_container_client(container=container_name) with open(file=download_file_path, mode='wb') as download_file: download_file.write(container_client.download_blob(blob.name).readall()) ./data/welcome_DOWNLOAD.txt
blob의 파일을 _DOWNLOAD.txt를 붙여서 다운로드한다.
4. 컨테이너 삭제
print('\nPress the Enter Key to begin clean up') if input() == 'yes': print('Deleting blob container...') container_client.delete_container() print('Deleteing the local source and downloaded files....') os.remove(upload_file_path) os.remove(download_file_path) os.rmdir(local_path) print('Done') Press the Enter Key to begin clean up yes Deleting blob container... Deleteing the local source and downloaded files.... Done
yes를 입력면 컨테이너를 삭제하고 업로드, 다운로드, 로컬 위치를 삭제한다.
컨테이너가 삭제된 것을 확인할 수 있다.
'클라우드 > azure' 카테고리의 다른 글
azure-15 storage 3 - queue (0) 2023.05.06 azure-14 storage 2 - file storage 2 - python (0) 2023.05.06 azure-10 sql database 4 - sql server (0) 2023.05.05 azure-09 sql database 3 - vscode, python (1) 2023.05.05