![]() |
![]() |
![]() |
![]() |
總覽
本教學課程說明如何透過 TensorFlow IO 的 Azure 檔案系統整合,搭配 TensorFlow 使用 Azure Blob 儲存體上的讀取和寫入檔案功能。
若要在 Azure Blob 儲存體上讀取和寫入檔案,需要 Azure 儲存體帳戶。Azure 儲存體金鑰應透過環境變數提供
os.environ['TF_AZURE_STORAGE_KEY'] = '<key>'
儲存體帳戶名稱和容器名稱是檔案名稱 URI 的一部分
azfs://<storage-account-name>/<container-name>/<path>
在本教學課程中,為了示範用途,您可以選擇性設定 Azurite,這是一個 Azure 儲存體模擬器。透過 Azurite 模擬器,可以使用 TensorFlow 的 Azure Blob 儲存體介面讀取和寫入檔案。
設定與使用方式
安裝必要的套件,並重新啟動執行階段
try:
%tensorflow_version 2.x
except Exception:
pass
!pip install tensorflow-io
安裝與設定 Azurite (選用)
如果沒有 Azure 儲存體帳戶,則需要執行下列步驟來安裝和設定 Azurite,以模擬 Azure 儲存體介面
npm install azurite@2.7.0
npm WARN deprecated request@2.87.0: request has been deprecated, see https://github.com/request/request/issues/3142 npm WARN saveError ENOENT: no such file or directory, open '/content/package.json' npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN enoent ENOENT: no such file or directory, open '/content/package.json' npm WARN content No description npm WARN content No repository field. npm WARN content No README data npm WARN content No license field. + azurite@2.7.0 added 116 packages from 141 contributors in 6.591s
# The path for npm might not be exposed in PATH env,
# you can find it out through 'npm bin' command
npm_bin_path = get_ipython().getoutput('npm bin')[0]
print('npm bin path: ', npm_bin_path)
# Run `azurite-blob -s` as a background process.
# IPython doesn't recognize `&` in inline bash cells.
get_ipython().system_raw(npm_bin_path + '/' + 'azurite-blob -s &')
npm bin path: /content/node_modules/.bin
使用 TensorFlow 讀取和寫入 Azure 儲存體檔案
以下範例說明如何使用 TensorFlow API 讀取和寫入 Azure 儲存體檔案。
一旦匯入 tensorflow-io
套件,它的行為方式就與 TensorFlow 中的其他檔案系統 (例如 POSIX 或 GCS) 相同,因為 tensorflow-io
會自動註冊 azfs
配置以供使用。
Azure 儲存體金鑰應透過 TF_AZURE_STORAGE_KEY
環境變數提供。否則,可以將 TF_AZURE_USE_DEV_STORAGE
設定為 True
以改用 Azurite 模擬器
import os
import tensorflow as tf
import tensorflow_io as tfio
# Switch to False to use Azure Storage instead:
use_emulator = True
if use_emulator:
os.environ['TF_AZURE_USE_DEV_STORAGE'] = '1'
account_name = 'devstoreaccount1'
else:
# Replace <key> with Azure Storage Key, and <account> with Azure Storage Account
os.environ['TF_AZURE_STORAGE_KEY'] = '<key>'
account_name = '<account>'
# Alternatively, you can use a shared access signature (SAS) to authenticate with the Azure Storage Account
os.environ['TF_AZURE_STORAGE_SAS'] = '<your sas>'
account_name = '<account>'
pathname = 'az://{}/aztest'.format(account_name)
tf.io.gfile.mkdir(pathname)
filename = pathname + '/hello.txt'
with tf.io.gfile.GFile(filename, mode='w') as w:
w.write("Hello, world!")
with tf.io.gfile.GFile(filename, mode='r') as r:
print(r.read())
Hello, world!
組態
TensorFlow 中 Azure Blob 儲存體的組態一律透過環境變數完成。以下是可用組態的完整清單
TF_AZURE_USE_DEV_STORAGE
:設為 1 可針對類似 'az://devstoreaccount1/container/file.txt' 的連線使用本機開發儲存體模擬器。這會優先於所有其他設定,因此unset
即可使用任何其他連線TF_AZURE_STORAGE_KEY
:使用中儲存體帳戶的帳戶金鑰TF_AZURE_STORAGE_USE_HTTP
:如果您不想使用 HTTPS 傳輸,請設為任何值。unset
即可使用預設的 HTTPSTF_AZURE_STORAGE_BLOB_ENDPOINT
:設定為 Blob 儲存體的端點 - 預設值為.core.windows.net
。