Lưu trữ Azure blob với TensorFlow

Xem trên TensorFlow.org Chạy trong Google Colab Xem nguồn trên GitHub Tải xuống sổ ghi chép

Tổng quat

Đây hướng dẫn chương trình làm thế nào để sử dụng đọc và ghi các tập tin trên Azure Blob lưu trữ với TensorFlow, thông qua hội nhập hệ thống tập tin Azure TensorFlow IO.

Cần có tài khoản lưu trữ Azure để đọc và ghi tệp trên Azure Blob Storage. Khoá lưu trữ Azure phải được cung cấp thông qua biến môi trường:

os.environ['TF_AZURE_STORAGE_KEY'] = '<key>'

Tên tài khoản lưu trữ và tên vùng chứa là một phần của tên tệp uri:

azfs://<storage-account-name>/<container-name>/<path>

Trong hướng dẫn này, cho bản demo mục đích bạn có thể tùy chọn thiết lập azurite mà là một giả lập Azure lưu trữ. Với trình giả lập Azurite, có thể đọc và ghi tệp thông qua giao diện lưu trữ Azure blob với TensorFlow.

Thiết lập và sử dụng

Cài đặt các gói bắt buộc và khởi động lại thời gian chạy

try:
  %tensorflow_version 2.x 
except Exception:
  pass

!pip install tensorflow-io

Cài đặt và thiết lập Azurite (tùy chọn)

Trong trường hợp không có Tài khoản lưu trữ Azure, bạn cần thực hiện những điều sau để cài đặt và thiết lập Azurite mô phỏng giao diện Lưu trữ Azure:

npm install azurite@2.7.0
[K[?25hnpm WARN deprecated request@2.87.0: request has been deprecated, see https://github.com/request/request/issues/3142
[K[?25hnpm 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

Đọc và ghi tệp vào Azure Storage với TensorFlow

Sau đây là một ví dụ về việc đọc và ghi tệp vào Azure Storage với API của TensorFlow.

Nó hoạt động giống như các hệ thống khác tập tin (ví dụ, POSIX hoặc GCS) trong TensorFlow lần tensorflow-io gói được nhập khẩu, như tensorflow-io sẽ tự động đăng ký azfs chương trình để sử dụng.

Các Azure lưu trữ chủ chốt cần được cung cấp thông qua TF_AZURE_STORAGE_KEY biến môi trường. Nếu không TF_AZURE_USE_DEV_STORAGE có thể được thiết lập để True sử dụng giả lập azurite thay vì:

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!

Cấu hình

Các cấu hình của Azure Blob Storage trong TensorFlow luôn được thực hiện thông qua các biến môi trường. Dưới đây là danh sách đầy đủ các cấu hình có sẵn:

  • TF_AZURE_USE_DEV_STORAGE : Set 1 để sử dụng giả lập lưu trữ phát triển địa phương cho các kết nối như 'az: //devstoreaccount1/container/file.txt'. Điều này sẽ đưa precendence khắp các thiết lập khác để unset để sử dụng bất kỳ kết nối khác
  • TF_AZURE_STORAGE_KEY chìa khóa tài khoản cho các tài khoản lưu trữ được sử dụng:
  • TF_AZURE_STORAGE_USE_HTTP : Đặt bất kỳ giá trị nếu bạn không muốn sử dụng chuyển https. unset để sử dụng mặc định của https
  • TF_AZURE_STORAGE_BLOB_ENDPOINT : Đặt cho thiết bị đầu cuối của lưu trữ blob - mặc định là .core.windows.net .