Share via


使用 Python 第 2 版用戶端程式庫的 Azure 檔案共用程式代碼範例

本文說明使用適用於 Python 的 Azure 檔案共用用戶端程式庫第 2 版的程式碼範例。

在 2023 年 3 月 31 日,我們已淘汰不符合目前 Azure SDK 指導方針的 Azure SDK 程式庫支援。 新的 Azure SDK 程式庫會定期更新,以促進一致的體驗並加強您的安全性態勢。 建議您轉換至新的 Azure SDK 程式庫,以利用新功能和重大安全性更新。

雖然較舊的程式庫仍可在 2023 年 3 月 31 日之後使用,但它們將不再收到 Microsoft 的官方支援和更新。 如需詳細資訊,請參閱支援淘汰公告

必要條件

使用 pip install 安裝下列封裝:

pip install azure-storage-file

加入下列 import 陳述式:

from azure.storage.file import FileService

建立 Azure 檔案共用

相關文章:使用 Python 開發 Azure 檔案儲存體

下列程式碼範例使用 FileService 物件來建立共用 (如果共用不存在)。

file_service.create_share('myshare')

建立目錄

相關文章:使用 Python 開發 Azure 檔案儲存體

您可以組織儲存體,方法是將檔案放在子目錄中,而不是將所有檔案都放在根目錄中。

下列程式碼會在根目錄底下建立名為 sampledir 的子目錄。

file_service.create_directory('myshare', 'sampledir')

上傳檔案

相關文章:使用 Python 開發 Azure 檔案儲存體

在此節中,您將了解如何將檔案從本機儲存體上傳至 Azure 檔案儲存體。

Azure 檔案共用至少包含可以放置檔案的根目錄。 若要建立檔案並上傳資料,請使用下列任一種方法:

這些方法可以在資料大小超過 64 MiB 時執行必要的區塊化動作。

create_file_from_path 會從指定的路徑上傳檔案的內容,create_file_from_stream 會從已開啟的檔案/串流上傳內容。 create_file_from_bytes 會上傳位元組陣列,create_file_from_text 會使用指定的編碼 (預設為 UTF-8) 上傳指定的文字值。

下列範例會將 sunset.png 檔案的內容上傳至 myfile 檔案中。

from azure.storage.file import ContentSettings
file_service.create_file_from_path(
    'myshare',
    None,  # We want to create this file in the root directory, so we specify None for the directory_name
    'myfile',
    'sunset.png',
    content_settings=ContentSettings(content_type='image/png'))

列舉 Azure 檔案共用的檔案和目錄

相關文章:使用 Python 開發 Azure 檔案儲存體

若要列出共用中的檔案和目錄,請使用 list_directories_and_files 方法。 這個方法會傳回產生器。 下列程式碼會將共用中每個檔案和目錄的 name 輸出到主控台。

generator = file_service.list_directories_and_files('myshare')
for file_or_dir in generator:
    print(file_or_dir.name)

下載檔案

相關文章:使用 Python 開發 Azure 檔案儲存體

若要從檔案儲存體下載資料,請使用下列任一種方法:

這些方法可以在資料大小超過 64 MiB 時執行必要的區塊化動作。

下列範例示範如何使用 get_file_to_path 下載 myfile 檔案的內容,並將其儲存至 out-sunset.png 檔案。

file_service.get_file_to_path('myshare', None, 'myfile', 'out-sunset.png')

建立共用快照集

相關文章:使用 Python 開發 Azure 檔案儲存體

您可以針對整個檔案共用,建立一個時間點複本。

snapshot = file_service.snapshot_share(share_name)
snapshot_id = snapshot.snapshot

使用中繼資料建立共用快照集

metadata = {"foo": "bar"}
snapshot = file_service.snapshot_share(share_name, metadata=metadata)

列出共用和快照集

相關文章:使用 Python 開發 Azure 檔案儲存體

您可以針對特定的共用,列出所有快照集。

shares = list(file_service.list_shares(include_snapshots=True))

瀏覽共用快照集

相關文章:使用 Python 開發 Azure 檔案儲存體

您可以瀏覽每個共用快照集,以擷取該時間點的檔案和目錄。

directories_and_files = list(
    file_service.list_directories_and_files(share_name, snapshot=snapshot_id))

從共用快照集取得檔案

相關文章:使用 Python 開發 Azure 檔案儲存體

您可以從共用快照集下載檔案。 這可讓您還原檔案的先前版本。

with open(FILE_PATH, 'wb') as stream:
    file = file_service.get_file_to_stream(
        share_name, directory_name, file_name, stream, snapshot=snapshot_id)

刪除單一共用快照集

相關文章:使用 Python 開發 Azure 檔案儲存體

您可以刪除單一共用快照集。

file_service.delete_share(share_name, snapshot=snapshot_id)

刪除檔案

相關文章:使用 Python 開發 Azure 檔案儲存體

若要刪除檔案,請呼叫 delete_file

下列程式碼範例示範如何刪除檔案儲存體:

file_service.delete_file('myshare', None, 'myfile')

共用快照集存在時刪除共用

相關文章:使用 Python 開發 Azure 檔案儲存體

除非先刪除所有快照集,否則無法刪除內含快照集的共用。

下列程式碼範例示範如何刪除檔案共用:

file_service.delete_share(share_name, delete_snapshots=DeleteSnapshot.Include)