使用 Java 版本 8 客户端库的 Azure 文件共享代码示例

本文介绍使用适用于 Java 的 Azure 文件共享客户端库版本 8 的代码示例。

2023 年 3 月 31 日,我们停用了对不符合当前 Azure SDK 指南的 Azure SDK 库的支持。 新的 Azure SDK 库会定期更新,以推动一致的体验并增强安全态势。 建议转换到新的 Azure SDK 库,以利用新功能和关键安全更新。

尽管 2023 年 3 月 31 日之后仍然可以使用较旧的库,但它们将不再从 Microsoft 获得官方支持和更新。 有关详细信息,请参阅支持停用公告

先决条件

要使用 Azure 文件共享客户端库,请添加以下 import 指令:

// Include the following imports to use Azure Files APIs v11
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.file.*;

访问 Azure 文件共享

相关文章:使用 Java 进行 Azure 文件存储开发

若要访问存储帐户,请使用 CloudStorageAccount 对象,将连接字符串传递到其 parse 方法。

// Use the CloudStorageAccount object to connect to your storage account
try {
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
} catch (InvalidKeyException invalidKey) {
    // Handle the exception
}

CloudStorageAccount.parse 会引发 InvalidKeyException,因此需将其置于 try/catch 块内。

创建文件共享

相关文章:使用 Java 进行 Azure 文件存储开发

Azure 文件存储中的所有文件和目录都存储在名为共享的容器内。

若要获取对共享及其内容的访问权限,请创建 Azure 文件存储客户端。 以下代码示例演示了如何创建文件共享:

// Create the Azure Files client.
CloudFileClient fileClient = storageAccount.createCloudFileClient();

使用 Azure 文件客户端可以获取对共享的引用。

// Get a reference to the file share
CloudFileShare share = fileClient.getShareReference("sampleshare");

实际创建共享时,请使用 CloudFileShare 对象的 createIfNotExists 方法。

if (share.createIfNotExists()) {
    System.out.println("New share created");
}

而在目前,share 保留对名为 sample share 的共享的引用。

删除文件共享

相关文章:使用 Java 进行 Azure 文件存储开发

以下示例代码将删除文件共享。

通过对 CloudFileShare 对象调用 deleteIfExists 方法来删除共享。

try
{
    // Retrieve storage account from connection-string.
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

    // Create the file client.
   CloudFileClient fileClient = storageAccount.createCloudFileClient();

   // Get a reference to the file share
   CloudFileShare share = fileClient.getShareReference("sampleshare");

   if (share.deleteIfExists()) {
       System.out.println("sampleshare deleted");
   }
} catch (Exception e) {
    e.printStackTrace();
}

创建目录

相关文章:使用 Java 进行 Azure 文件存储开发

可以将文件置于子目录中,不必将其全部置于根目录中,以便对存储进行有效的组织。

以下代码会在根目录下创建名为 sampledir 的子目录:

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

//Get a reference to the sampledir directory
CloudFileDirectory sampleDir = rootDir.getDirectoryReference("sampledir");

if (sampleDir.createIfNotExists()) {
    System.out.println("sampledir created");
} else {
    System.out.println("sampledir already exists");
}

删除目录

相关文章:使用 Java 进行 Azure 文件存储开发

以下代码示例说明了如何删除目录。 你无法删除仍包含文件或子目录的目录。

// Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

// Get a reference to the directory you want to delete
CloudFileDirectory containerDir = rootDir.getDirectoryReference("sampledir");

// Delete the directory
if ( containerDir.deleteIfExists() ) {
    System.out.println("Directory deleted");
}

枚举 Azure 文件共享中的文件和目录

相关文章:使用 Java 进行 Azure 文件存储开发

通过对 CloudFileDirectory 引用调用 listFilesAndDirectories,获取文件和目录的列表。 该方法会返回可循环访问的 ListFileItem 对象的列表。

以下代码将列出根目录中的文件和目录:

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

for ( ListFileItem fileItem : rootDir.listFilesAndDirectories() ) {
    System.out.println(fileItem.getUri());
}

上传文件

相关文章:使用 Java 进行 Azure 文件存储开发

通过对共享对象调用 getRootDirectoryReference 方法,获取对文件上传目录的引用。

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

现在,你已经具有对共享根目录的引用,可以使用以下代码将文件上传到该位置:

// Define the path to a local file.
final String filePath = "C:\\temp\\Readme.txt";

CloudFile cloudFile = rootDir.getFileReference("Readme.txt");
cloudFile.uploadFromFile(filePath);

下载文件

相关文章:使用 Java 进行 Azure 文件存储开发

以下示例将下载 SampleFile.txt 并显示其内容:

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

//Get a reference to the directory that contains the file
CloudFileDirectory sampleDir = rootDir.getDirectoryReference("sampledir");

//Get a reference to the file you want to download
CloudFile file = sampleDir.getFileReference("SampleFile.txt");

//Write the contents of the file to the console.
System.out.println(file.downloadText());

删除文件

相关文章:使用 Java 进行 Azure 文件存储开发

下面的代码会删除名为 SampleFile.txt 的文件,该文件存储在名为 sampledir 的目录中:

// Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

// Get a reference to the directory where the file to be deleted is in
CloudFileDirectory containerDir = rootDir.getDirectoryReference("sampledir");

String filename = "SampleFile.txt"
CloudFile file;

file = containerDir.getFileReference(filename)
if ( file.deleteIfExists() ) {
    System.out.println(filename + " was deleted");
}