-
azure-14 storage 2 - file storage 2 - python클라우드/azure 2023. 5. 6. 14:57
1. filestorage 생성
!pip install azure-storage-file Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting azure-storage-file Downloading azure_storage_file-2.1.0-py2.py3-none-any.whl (36 kB) Collecting azure-common>=1.1.5 Downloading azure_common-1.1.28-py2.py3-none-any.whl (14 kB) Collecting azure-storage-common~=2.1 Downloading azure_storage_common-2.1.0-py2.py3-none-any.whl (47 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 47.8/47.8 kB 4.0 MB/s eta 0:00:00 Requirement already satisfied: cryptography in /usr/local/lib/python3.9/dist-packages (from azure-storage-common~=2.1->azure-storage-file) (40.0.2) Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (from azure-storage-common~=2.1->azure-storage-file) (2.27.1) Requirement already satisfied: python-dateutil in /usr/local/lib/python3.9/dist-packages (from azure-storage-common~=2.1->azure-storage-file) (2.8.2) Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.9/dist-packages (from cryptography->azure-storage-common~=2.1->azure-storage-file) (1.15.1) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.9/dist-packages (from python-dateutil->azure-storage-common~=2.1->azure-storage-file) (1.16.0) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-file) (2022.12.7) Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-file) (1.26.15) Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-file) (2.0.12) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests->azure-storage-common~=2.1->azure-storage-file) (3.4) Requirement already satisfied: pycparser in /usr/local/lib/python3.9/dist-packages (from cffi>=1.12->cryptography->azure-storage-common~=2.1->azure-storage-file) (2.21) Installing collected packages: azure-common, azure-storage-common, azure-storage-file Successfully installed azure-common-1.1.28 azure-storage-common-2.1.0 azure-storage-file-2.1.0
파일 서비스 모듈을 불러온다.
from azure.storage.file import FileService file_service = FileService(account_name='',account_key='')
액세스 탭으로 이동해서 계정 이름과 키를 복사하여 변수를 생성한다.
2. 디렉토리 생성
file_service.create_share('myshare') True file_service.create_directory('myshare','sampledir') True
myshare의 이름을 가진 파일 공유를 생성하고 sampledir의 이름을 가진 디렉토리를 생성한다.
3. 파일 업로드 및 다운로드
from azure.storage.file import ContentSettings file_service.create_file_from_path( 'myshare', 'sampledir', 'myfile', '10.jfif' )
로컬에 사진을 업로드하고 경로와 이름을 지정해서 파일을 업로드한다.
generator = file_service.list_directories_and_files('myshare') for file_or_dir in generator: print(file_or_dir.name) sampledir
디렉토리의 이름을 확인한다.
ile_service.get_file_to_path('myshare','sampledir','myfile','10download.jfif') <azure.storage.file.models.File at 0x7f74c4347460>
파일을 로컬로 다운로드한다.
4. 스냅샷
metadata = {'foo':'bar'} snapshot = file_service.snapshot_share('myshare',metadata=metadata)
메타 데이터를 샘플로 만들고 myshare의 스냅을 찍는다. 스냅샷에는 myshare의 정보가 찍혀있다.
share = list(file_service.list_shares(include_snapshots=True))
스냅샷이 포함되어 있는 목록을 가져온다.
directories_and_file = list(file_service.list_directories_and_files('myshare',snapshot='2023-05-06T05:41:28.0000000Z')) for file_or_dir in directories_and_file: print(file_or_dir.name) sampledir
스냅샷이 있는 파일을 가져오고 디렉토리의 이름을 찍은 것을 확인할 수 있다.
5. 삭제
file_service.delete_share('myshare',snapshot='2023-05-06T05:41:28.0000000Z') True
특정 스냅샷을 삭제할 수 있다.
file_service.delete_share('myshare') True
폴더의 스냅샷을 모두 삭제하고 나면 폴더를 삭제할 수 있다.
'클라우드 > azure' 카테고리의 다른 글
azure-15 storage 3 - queue (0) 2023.05.06 azure-12 storage 1 - blob 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