def download_azure_file(self, connection_string, share_name, dir_name, file_name):
try:
# Build the remote path
source_file_path = dir_name + "/" + file_name
# Add a prefix to the filename to
# distinguish it from the uploaded file
dest_file_name = "DOWNLOADED-" + file_name
# Create a ShareFileClient from a connection string
file_client = ShareFileClient.from_connection_string(
connection_string, share_name, source_file_path)
print("Downloading to:", dest_file_name)
# Open a file for writing bytes on the local system
with open(dest_file_name, "wb") as data:
# Download the file from Azure into a stream
stream = file_client.download_file()
# Write the stream to the local file
data.write(stream.readall())
except ResourceNotFoundError as ex:
print("ResourceNotFoundError:", ex.message)
共有スナップショットを作成する
ファイル共有全体の特定の時点のコピーを作成できます。
def create_snapshot(self, connection_string, share_name):
try:
# Create a ShareClient from a connection string
share_client = ShareClient.from_connection_string(
connection_string, share_name)
# Create a snapshot
snapshot = share_client.create_snapshot()
print("Created snapshot:", snapshot["snapshot"])
# Return the snapshot time so
# it can be accessed later
return snapshot["snapshot"]
except ResourceNotFoundError as ex:
print("ResourceNotFoundError:", ex.message)
共有とスナップショットの一覧表示
特定の共有のすべてのスナップショットを一覧表示できます。
def list_shares_snapshots(self, connection_string):
try:
# Create a ShareServiceClient from a connection string
service_client = ShareServiceClient.from_connection_string(connection_string)
# List the shares in the file service
shares = list(service_client.list_shares(include_snapshots=True))
for share in shares:
if (share["snapshot"]):
print("Share:", share["name"], "Snapshot:", share["snapshot"])
else:
print("Share:", share["name"])
except ResourceNotFoundError as ex:
print("ResourceNotFoundError:", ex.message)
共有スナップショットの参照
各共有スナップショットを参照して、その時点のファイルとディレクトリを取得できます。
def browse_snapshot_dir(self, connection_string, share_name, snapshot_time, dir_name):
try:
# Create a ShareClient from a connection string
snapshot = ShareClient.from_connection_string(
conn_str=connection_string, share_name=share_name, snapshot=snapshot_time)
print("Snapshot:", snapshot_time)
for item in list(snapshot.list_directories_and_files(dir_name)):
if item["is_directory"]:
print("Directory:", item["name"])
else:
print("File:", dir_name + "/" + item["name"])
except ResourceNotFoundError as ex:
print("ResourceNotFoundError:", ex.message)
def download_snapshot_file(self, connection_string, share_name, snapshot_time, dir_name, file_name):
try:
# Build the remote path
source_file_path = dir_name + "/" + file_name
# Add a prefix to the local filename to
# indicate it's a file from a snapshot
dest_file_name = "SNAPSHOT-" + file_name
# Create a ShareFileClient from a connection string
snapshot_file_client = ShareFileClient.from_connection_string(
conn_str=connection_string, share_name=share_name,
file_path=source_file_path, snapshot=snapshot_time)
print("Downloading to:", dest_file_name)
# Open a file for writing bytes on the local system
with open(dest_file_name, "wb") as data:
# Download the file from Azure into a stream
stream = snapshot_file_client.download_file()
# Write the stream to the local file
data.write(stream.readall())
except ResourceNotFoundError as ex:
print("ResourceNotFoundError:", ex.message)
単一の共有スナップショットの削除
単一の共有スナップショットを削除できます。
def delete_snapshot(self, connection_string, share_name, snapshot_time):
try:
# Create a ShareClient for a snapshot
snapshot_client = ShareClient.from_connection_string(conn_str=connection_string, share_name=share_name, snapshot=snapshot_time)
print("Deleting snapshot:", snapshot_time)
# Delete the snapshot
snapshot_client.delete_share()
except ResourceNotFoundError as ex:
print("ResourceNotFoundError:", ex.message)