Примечание
Для доступа к этой странице требуется авторизация. Вы можете попробовать войти или изменить каталоги.
Для доступа к этой странице требуется авторизация. Вы можете попробовать изменить каталоги.
Learn how to develop Java applications that use Azure Files to store data. Azure Files is a managed file share service in the cloud. It provides fully managed file shares that are accessible via the industry standard Server Message Block (SMB) and Network File System (NFS) protocols. Azure Files also provides a REST API for programmatic access to file shares.
In this article, you learn about the different approaches to developing with Azure Files in Java, and how to choose the approach that best fits the needs of your app. You also learn how to create a basic console app that interacts with Azure Files resources.
Применяется к
Модель управления | Модель выставления счетов | Media tier | Redundancy | SMB | NFS |
---|---|---|---|---|---|
Microsoft.Storage | Provisioned v2 | HDD (стандартный) | Local (LRS) |
![]() |
![]() |
Microsoft.Storage | Provisioned v2 | HDD (стандартный) | Zone (ZRS) |
![]() |
![]() |
Microsoft.Storage | Provisioned v2 | HDD (стандартный) | Гео (GRS) |
![]() |
![]() |
Microsoft.Storage | Provisioned v2 | HDD (стандартный) | GeoZone (GZRS) |
![]() |
![]() |
Microsoft.Storage | Provisioned v1 | SSD (премиум) | Local (LRS) |
![]() |
![]() |
Microsoft.Storage | Provisioned v1 | SSD (премиум) | Zone (ZRS) |
![]() |
![]() |
Microsoft.Storage | Pay-as-you-go | HDD (стандартный) | Local (LRS) |
![]() |
![]() |
Microsoft.Storage | Pay-as-you-go | HDD (стандартный) | Zone (ZRS) |
![]() |
![]() |
Microsoft.Storage | Pay-as-you-go | HDD (стандартный) | Гео (GRS) |
![]() |
![]() |
Microsoft.Storage | Pay-as-you-go | HDD (стандартный) | GeoZone (GZRS) |
![]() |
![]() |
About Java app development with Azure Files
Azure Files offers several ways for Java developers to access data and manage resources in Azure Files. The following table lists the approaches, summarizes how they work, and provides guidance on when to use each approach:
Подход | Принцип работы | Когда следует использовать |
---|---|---|
Standard file I/O libraries | Uses OS-level API calls through Azure file shares mounted using SMB or NFS. When you mount a file share using SMB/NFS, you can use file I/O libraries for a programming language or framework, such as java.io and java.nio for Java. |
You have line-of-business apps with existing code that uses standard file I/O, and you don't want to rewrite code for the app to work with an Azure file share. |
FileREST API | Directly calls HTTPS endpoints to interact with data stored in Azure Files. Provides programmatic control over file share resources. The Azure SDK provides the File Shares client library (com.azure.storage.file.share ) that builds on the FileREST API, allowing you interact with FileREST API operations through familiar Java programming language paradigms. |
You're building value-added cloud services and apps for customers and you want to use advanced features not available through standard file I/O libraries. |
Storage resource provider REST API | Uses Azure Resource Manager (ARM) to manage storage accounts and file shares. Calls REST API endpoints for various resource management operations. | Your app or service needs to perform resource management tasks, such as creating, deleting, or updating storage accounts or file shares. |
For general information about these approaches, see Overview of application development with Azure Files.
This article focuses on working with Azure Files resources using the following approaches:
- Work with Azure Files using Java file I/O libraries: Mount a file share using SMB or NFS and use Java file I/O libraries to work with files and directories in the share.
- Work with Azure Files using the File Shares client library for Java: Use the Azure Storage File Shares client library for Java to work with files and directories in a file share. This client library builds on the FileREST API.
- Manage Azure Files resources using the Azure Storage management libraries: Use the Azure Storage management libraries to manage file shares and other resources in your storage account. The management libraries build on the Azure Storage resource provider REST API.
Предпосылки
- Подписка Azure — создайте бесплатную учетную запись.
- Учетная запись хранения Azure — создайте такую учетную запись.
- Комплект SDK для Java (JDK) версии 8 или более поздней версии.
- Apache Maven используется для управления проектами в этом примере
Настройка среды
Примечание.
В рамках этой статьи для сборки и запуска примера кода используется средство сборки Maven. Для работы с пакетами SDK Azure для Java есть и другие средства сборки, например Gradle.
Используйте Maven для создания нового консольного приложения или открытия существующего проекта. Выполните следующие действия, чтобы установить пакеты и добавить необходимые import
директивы.
Установка пакетов
Откройте файл pom.xml
в текстовом редакторе. Установите пакеты, включив файл BOM или включив прямую зависимость.
Включите BOM-файл
Добавьте azure-sdk-bom , чтобы получить зависимость от последней версии библиотеки. В следующем фрагменте замените заполнитель {bom_version_to_target}
номером версии. Использование azure-sdk-bom избавляет от необходимости указывать версию каждой отдельной зависимости. Чтобы узнать больше о BOM, см. файл README для BOM Azure SDK.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-sdk-bom</artifactId>
<version>{bom_version_to_target}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Добавьте приведенные ниже элементы зависимости в группу зависимостей. Зависимость azure-identity необходима для подключения к службам Azure без использования паролей. Note that the resource manager artifacts are not included in the BOM file, so you need to add them as direct dependencies.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-file-share</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-management</artifactId>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>{package_version_to_target}</version>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-storage</artifactId>
<version>{package_version_to_target}</version>
</dependency>
Include a direct dependency
To take a dependency on a particular version of the library, add the direct dependency to your project:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-file-share</artifactId>
<version>{package_version_to_target}</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>{package_version_to_target}</version>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>{package_version_to_target}</version>
</dependency>
<dependency>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager-storage</artifactId>
<version>{package_version_to_target}</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-management</artifactId>
<version>{package_version_to_target}</version>
</dependency>
Включите директивы импорта
Затем откройте файл кода и добавьте необходимые import
директивы. В этом примере мы добавим в файл App.java следующие директивы:
import com.azure.identity.*;
import com.azure.storage.file.share.*;
import com.azure.resourcemanager.*;
import com.azure.resourcemanager.storage.models.*;
import com.azure.core.management.*;
import com.azure.core.management.profile.*;
If you plan to use the Java file I/O libraries, you also need to add the following import directives:
import java.io.*;
import java.nio.file.*;
Work with Azure Files using Java file I/O libraries
Standard file I/O libraries are the most common way to access and work with Azure Files resources. When you mount a file share using SMB or NFS, your operating system redirects API requests for the local file system. This approach allows you to use standard file I/O libraries, such as java.io
and java.nio
, to interact with files and directories in the share.
Consider using Java file I/O libraries when your app requires:
- App compatibility: Ideal for line-of-business apps with existing code that already uses Java file I/O libraries. You don't need to rewrite code for the app to work with an Azure file share.
- Ease of use: Java file I/O libraries are well known by developers and easy to use. A key value proposition of Azure Files is that it exposes native file system APIs through SMB and NFS.
In this section, you learn how to use Java file I/O libraries to work with Azure Files resources.
For more information and examples, see the following resource:
Mount a file share
To use Java file I/O libraries, you must first mount a file share. See the following resources for guidance on how to mount a file share using SMB or NFS:
- Mount an SMB file share on Windows
- Mount an SMB file share on Linux
- Mount an NFS file share on Linux
In this article, we use the following path to refer to a mounted SMB file share on Windows:
String fileSharePath = "Z:\\file-share";
Example: Connect to a file share and enumerate directories using Java file I/O libraries
The following code example shows how to connect to a file share and list the directories in the share:
import java.io.*;
import java.nio.file.*;
// Add the following code to a new or existing function
String fileSharePath = "Z:\\file-share";
try {
File directory = new File(fileSharePath);
File[] dirs = directory.listFiles(File::isDirectory);
if (dirs != null) {
for (File dir : dirs) {
System.out.println(dir.getName());
}
System.out.println(dirs.length + " directories found.");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
Example: Write to a file in a file share using Java file I/O libraries
The following code example shows how to write and append text to a file:
import java.io.*;
import java.nio.file.*;
import java.util.Arrays;
// Add the following code to a new or existing function
String fileSharePath = "Z:\\file-share";
String fileName = "test.txt";
try {
String textToWrite = "First line" + System.lineSeparator();
Path filePath = Paths.get(fileSharePath, fileName);
// Write initial content to file
Files.write(filePath, textToWrite.getBytes());
System.out.println("Initial text written to file");
// Append additional lines to the file
String[] textToAppend = { "Second line", "Third line" };
Files.write(filePath,
Arrays.asList(textToAppend),
StandardOpenOption.APPEND);
System.out.println("Additional lines appended to file");
} catch (IOException ex) {
System.out.println("Error writing to file: " + ex.getMessage());
}
Example: Lock a file in a file share using Java file I/O libraries
SMB clients that mount file shares can use file system locking mechanisms to manage access to shared files.
The following code example shows how to lock a file in a file share:
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.*;
// Add the following code to a new or existing function
String fileSharePath = "Z:\\file-share";
String fileName = "test.txt";
String filePath = Paths.get(fileSharePath, fileName).toString();
try (
FileOutputStream fos = new FileOutputStream(filePath);
FileChannel fileChannel = fos.getChannel()) {
// Acquire an exclusive lock on this file
FileLock lock = fileChannel.lock();
System.out.println("File is locked.");
// Perform file operations here
// Release the lock
lock.release();
System.out.println("File lock released.");
} catch (Exception e) {
e.printStackTrace();
}
When using both SMB and the FileREST API, keep in mind that the FileREST API uses leases to manage file locks, while SMB uses file system locks managed by the operating system. To learn more about managing file locking interactions between SMB and the FileREST API, see Manage file locks.
Example: Enumerate file ACLs using Java file I/O libraries
The following code example shows how to enumerate access control lists (ACLs) for a file:
import java.nio.file.*;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclFileAttributeView;
import java.util.List;
// Add the following code to a new or existing function
String fileSharePath = "Z:\\file-share";
String fileName = "test.txt";
String filePath = Paths.get(fileSharePath, fileName).toString();
try {
Path path = Paths.get(filePath);
// Get the ACL view for the file
AclFileAttributeView aclView = Files.getFileAttributeView(
path, AclFileAttributeView.class);
// Get the ACL entries
List<AclEntry> aclEntries = aclView.getAcl();
// List all access rules for the file
for (AclEntry entry : aclEntries) {
System.out.println("Identity: " + entry.principal().getName());
System.out.println("Access Control Type: " + entry.type());
System.out.println("File System Rights: " + entry.permissions());
System.out.println();
}
System.out.println(aclEntries.size() + " ACL entries found.");
} catch (Exception ex) {
System.out.println("Error: " + ex.getMessage());
}
Work with Azure Files using the File Shares client library for Java
The FileREST API provides programmatic access to Azure Files. It allows you to call HTTPS endpoints to perform operations on file shares, directories, and files. The FileREST API is designed for high scalability and advanced features that might not be available through native protocols. The Azure SDK provides client libraries, such as the File Shares client library for Java, that build on the FileREST API.
Consider using the FileREST API and the File Share client library if your application requires:
- Advanced features: Access operations and features that aren't available through native protocols.
- Custom cloud integrations: Build custom value-added services, such as backup, antivirus, or data management, that interact directly with Azure Files.
- Performance optimization: Benefit from performance advantages in high-scale scenarios using data plane operations.
The FileREST API models Azure Files as a hierarchy of resources, and is recommended for operations that are performed at the directory or file level. You should prefer the Storage resource provider REST API for operations that are performed at the file service or file share level.
In this section, you learn how to use the File Shares client library for Java to work with Azure Files resources.
For more information and examples, see the following resources:
Авторизация доступа и создание клиента
To connect an app to Azure Files, create a ShareClient
object. This object is your starting point for working with Azure Files resources. The following code examples show how to create a ShareClient
object using different authorization mechanisms.
To authorize with Microsoft Entra ID, you'll need to use a security principal. Which type of security principal you need depends on where your app runs. To learn more about authentication scenarios, see Azure authentication with Java and Azure Identity.
To work with the code examples in this article, assign the Azure RBAC built-in role Storage File Data Privileged Contributor to the security principal. This role provides full read, write, modify ACLs, and delete access on all the data in the shares for all the configured storage accounts regardless of the file/directory level NTFS permissions that are set. For more information, see Access Azure file shares using Microsoft Entra ID with Azure Files OAuth over REST.
Авторизация доступа с помощью DefaultAzureCredential
An easy and secure way to authorize access and connect to Azure Files is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create a ShareClient
object.
The following example creates a ShareClient
object authorized using DefaultAzureCredential
, then creates a ShareDirectoryClient
object to work with a directory in the share:
import com.azure.core.credential.TokenCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.storage.file.share.*;
// Add the following code to a new or existing function
String accountName = "<account-name>";
String shareName = "<share-name>";
TokenCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build();
// Create the ShareClient
ShareClient shareClient = new ShareClientBuilder()
.endpoint(String.format("https://%s.file.core.windows.net", accountName))
.shareName(shareName)
.credential(defaultAzureCredential)
.buildClient();
// Create a client to interact with a directory in the share
ShareDirectoryClient directoryClient = shareClient.getDirectoryClient("sample-directory");
If you know exactly which credential type you use to authenticate users, you can obtain an OAuth token by using other classes in the Azure Identity client library for Java. Эти классы являются производными от класса TokenCredential .
To learn more about each of these authorization mechanisms, see Choose how to authorize access to file data.
Example: Copy files using the File Shares client library
You can copy files within a file share or between file shares by using the following method:
You can copy a file to a destination blob by using the following method from a BlockBlobClient
object:
The following code example shows how to copy a file to a file in another file share:
import java.time.*;
import java.util.*;
import com.azure.core.credential.TokenCredential;
import com.azure.core.util.polling.*;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.storage.file.share.*;
import com.azure.storage.file.share.models.*;
// Add the following code to a new or existing function
String accountName = "<account-name>";
String srcShareName = "src-file-share";
String destShareName = "dest-file-share";
String srcFilePath = "src/path/to/file";
String destFilePath = "dest/path/to/file";
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
ShareFileClient srcShareFileClient = new ShareFileClientBuilder()
.endpoint(String.format("https://%s.file.core.windows.net", accountName))
.shareName(srcShareName)
.shareTokenIntent(ShareTokenIntent.BACKUP)
.resourcePath(srcFilePath)
.credential(defaultAzureCredential)
.buildFileClient();
ShareFileClient destShareFileClient = new ShareFileClientBuilder()
.endpoint(String.format("https://%s.file.core.windows.net", accountName))
.shareName(destShareName)
.shareTokenIntent(ShareTokenIntent.BACKUP)
.resourcePath(destFilePath)
.credential(defaultAzureCredential)
.buildFileClient();
// Copy the file from the source share to the destination share
SyncPoller<ShareFileCopyInfo, Void> poller = destShareFileClient
.beginCopy(srcShareFileClient.getFileUrl(),
Collections.singletonMap("file", "metadata"),
Duration.ofSeconds(2));
final PollResponse<ShareFileCopyInfo> pollResponse = poller.poll();
final ShareFileCopyInfo value = pollResponse.getValue();
System.out.printf("Copy source: %s. Status: %s.%n", value.getCopySourceUrl(), value.getCopyStatus());
Example: Lease a file using the File Shares client library
A lease creates a lock on a file that's managed by Azure via a lease ID. The lease provides a mechanism to coordinate access to files across multiple clients in a distributed system. A lease on a file provides exclusive write and delete access. To learn more about lease states and actions, see Lease File.
The following code example shows how to create a lease client, acquire an infinite duration lease on a file, and release the lease:
import com.azure.core.credential.TokenCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.storage.file.share.*;
import com.azure.storage.file.share.models.*;
import com.azure.storage.file.share.specialized.*;
// Add the following code to a new or existing function
String accountName = "<account-name>";
String shareName = "sample-file-share";
String filePath = "path/to/file";
TokenCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build();
ShareFileClient fileClient = new ShareFileClientBuilder()
.endpoint(String.format("https://%s.file.core.windows.net", accountName))
.shareName(shareName)
.shareTokenIntent(ShareTokenIntent.BACKUP)
.resourcePath(filePath)
.credential(defaultAzureCredential)
.buildFileClient();
// Get a ShareLeaseClient
ShareLeaseClient fileLeaseClient = new ShareLeaseClientBuilder()
.fileClient(fileClient)
.shareTokenIntent(ShareTokenIntent.BACKUP)
.buildClient();
try {
// Acquire a lease on the file with infinite duration
fileLeaseClient.acquireLease();
System.out.println("Lease acquired successfully");
// Do something with the file
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
} finally {
// Release the lease when finished
try {
fileLeaseClient.releaseLease();
System.out.println("Lease released successfully.");
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
When using both SMB and the FileREST API, keep in mind that the FileREST API uses leases to manage file locks, while SMB uses file system locks managed by the operating system. To learn more about managing file locking interactions between SMB and the FileREST API, see Manage file locks.
Example: Create and list share snapshots using the File Shares client library
Share snapshots are read-only copies of a file share at a point in time. You can create a snapshot of a file share, and then use the snapshot to access the data in the share at the time the snapshot was created. You can also list all snapshots in a file share, and delete share snapshots.
The following code example shows how to create a share snapshot, list the snapshots in a file share, and traverse the directory tree in a share snapshot:
import com.azure.storage.file.share.*;
import com.azure.storage.file.share.models.*;
// Add the following code to a new or existing function
public static void main(String[] args) {
String connectionString = "<connection-string>";
// Create a ShareServiceClient from which you can create clients for specific shares
ShareServiceClient shareServiceClient = new ShareServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
// Get a client for a specific share
ShareClient shareClient = shareServiceClient.getShareClient("sample-file-share");
try {
// Create a snapshot
ShareSnapshotInfo snapshotInfo = shareClient.createSnapshot();
System.out.println("Snapshot created: " + snapshotInfo.getSnapshot());
// List snapshots in a share
ListSharesOptions options = new ListSharesOptions()
.setIncludeSnapshots(true);
for (ShareItem shareItem : shareServiceClient.listShares(options, null, null)) {
if (shareItem.getSnapshot() != null) {
System.out.println("Share: " + shareItem.getName() +
" (Snapshot: " + shareItem.getSnapshot() + ")");
}
}
// List directories and files in a share snapshot
String snapshotTimestamp = snapshotInfo.getSnapshot();
ShareClient shareSnapshot = shareClient.getSnapshotClient(snapshotTimestamp);
ShareDirectoryClient rootDir = shareSnapshot.getRootDirectoryClient();
listDirectoryTree(rootDir);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
private static void listDirectoryTree(ShareDirectoryClient directory) {
// List all files and directories in current directory
for (ShareFileItem fileItem : directory.listFilesAndDirectories()) {
if (fileItem.isDirectory()) {
System.out.println("Directory: " + fileItem.getName());
// Recursively list subdirectory contents
listDirectoryTree(directory.getSubdirectoryClient(fileItem.getName()));
} else {
System.out.println("File: " + fileItem.getName());
}
}
}
Примечание.
OAuth tokens, such as those obtained when using DefaultAzureCredential
, aren't allowed for data plane operations at the file share level. To work with share snapshots, the client object must be authorized using the account key. The ShareClient
object created in this code example uses a connection string, which includes the account key.
Storing account keys or connection strings presents a security risk. You should only use them when Microsoft Entra authentication isn't available. To learn more about securely storing account keys in Azure Key Vault, see About Azure Key Vault managed storage account keys.
Manage Azure Files resources using the Azure Storage management libraries
The Azure Storage management libraries are built on the Azure Storage resource provider REST API. The Azure Storage resource provider is a service based on Azure Resource Manager, and supports both declarative (templates) and imperative (direct API call) methods. The Azure Storage resource provider REST API provides programmatic access to Azure Storage resources, including file shares. The Azure SDK provides management libraries that build on the Azure Storage resource provider REST API.
The management libraries are recommended for operations that are performed at the file service or file share level. In this section, you learn how to use the Azure Storage management libraries to manage Azure Files resources.
The Azure Storage management libraries are built on the Azure Storage resource provider REST API. The Azure Storage resource provider is a service based on Azure Resource Manager, and supports both declarative (templates) and imperative (direct API call) methods. The Azure Storage resource provider REST API provides programmatic access to Azure Storage resources, including file shares. The Azure SDK provides management libraries that build on the Azure Storage resource provider REST API.
The management libraries are recommended for operations that are performed at the file service or file share level. In this section, you learn how to use the Azure Storage management libraries to manage Azure Files resources.
Example: Create a file share using the Azure Storage management library
The following code example shows how to create a top-level AzureResourceManager
object, register the Storage resource provider with a subscription, and create a file share using the Azure Storage management library:
import com.azure.identity.*;
import com.azure.resourcemanager.*;
import com.azure.resourcemanager.storage.fluent.*;
import com.azure.resourcemanager.storage.fluent.models.*;
import com.azure.core.credential.TokenCredential;
import com.azure.core.management.*;
import com.azure.core.management.profile.*;
// Add the following code to a new or existing function
String subscriptionID = "<subscription-id>";
String rgName = "<resource-group-name>";
String saName = "<storage-account-name>";
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
AzureResourceManager armClient = AzureResourceManager
.configure()
.authenticate(credential, profile)
.withSubscription(subscriptionID);
// Check the registration state of the resource provider and register, if needed
if (armClient.providers().getByName("Microsoft.Storage").registrationState() == "NotRegistered")
armClient.providers().register("Microsoft.Storage");
// Create a new file share
StorageManagementClient storageManagementClient = armClient.storageAccounts().manager().serviceClient();
FileSharesClient fileShare = storageManagementClient.getFileShares();
String shareName = "sample-file-share";
int quotaInGB = 1;
// Create the file share
fileShare.create(
rgName,
saName,
shareName,
new FileShareInner()
.withShareQuota(quotaInGB)
);
You can configure the file share properties using the FileShareInner class. The previous example shows how to set the share quota when creating the file share. To update an existing file share, call fileShare.update()
and pass in the FileShareInner
object with the properties you want to update.
Примечание.
To perform the register operation, you need permissions for the following Azure RBAC action: Microsoft.Storage/register/action. This permission is included in the Contributor and Owner built-in roles.
Example: List file shares and snapshots using the Azure Storage management library
The following code example shows how to list file shares and snapshots in a storage account:
import com.azure.identity.*;
import com.azure.resourcemanager.*;
import com.azure.resourcemanager.storage.fluent.*;
import com.azure.resourcemanager.storage.fluent.models.*;
import com.azure.core.credential.TokenCredential;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.management.*;
import com.azure.core.management.profile.*;
import com.azure.core.util.Context;
// Add the following code to a new or existing function
String subscriptionID = "<subscription-id>";
String rgName = "<resource-group-name>";
String saName = "<storage-account-name>";
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
AzureResourceManager armClient = AzureResourceManager
.configure()
.authenticate(credential, profile)
.withSubscription(subscriptionID);
// Check the registration state of the resource provider and register, if needed
if (armClient.providers().getByName("Microsoft.Storage").registrationState() == "NotRegistered")
armClient.providers().register("Microsoft.Storage");
StorageManagementClient storageManagementClient = armClient.storageAccounts().manager().serviceClient();
FileSharesClient fileShare = storageManagementClient.getFileShares();
// List all file shares and include snapshots
PagedIterable<FileShareItemInner> fileShares = fileShare.list(
rgName, // resource group name
saName, // storage account name
null, // maxpagesize
null, // filter
"snapshots", // expand to include snapshots
Context.NONE); // context
for (FileShareItemInner fileShareItem : fileShares) {
System.out.println("File share name: " + fileShareItem.name());
System.out.println("File share quota: " + fileShareItem.shareQuota());
}
Следующие шаги
For more information about developing with Azure Files, see the following resources: