共用方式為


使用 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 物件,將連接字串傳遞至其剖析方法。

// 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");
}

目前,共用會將參考保留至名為 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 檔案儲存體

下列程式碼會刪除儲存在名為 sampledir之目錄內名為 SampleFile.txt 的檔案:

// 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");
}