Share via


Azure File Share-kodexempel med Java version 8-klientbibliotek

Den här artikeln visar kodexempel som använder version 8 av Azure File Share-klientbiblioteket för Java.

Den 31 mars 2023 drog vi tillbaka stödet för Azure SDK-bibliotek som inte följer de aktuella riktlinjerna för Azure SDK. De nya Azure SDK-biblioteken uppdateras regelbundet för att skapa konsekventa upplevelser och stärka din säkerhetsstatus. Vi rekommenderar att du övergår till de nya Azure SDK-biblioteken för att dra nytta av de nya funktionerna och viktiga säkerhetsuppdateringar.

Även om de äldre biblioteken fortfarande kan användas efter den 31 mars 2023 får de inte längre officiell support och uppdateringar från Microsoft. Mer information finns i meddelandet om supportavgångar.

Förutsättningar

Om du vill använda Azure File Share-klientbiblioteket lägger du till följande import direktiv:

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

Få åtkomst till en Azure-filresurs

Relaterad artikel: Utveckla för Azure Files med Java

För att komma åt ditt lagringskonto använder du objektet CloudStorageAccount och skickar anslutningssträngen till dess parsningsmetod .

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

CloudStorageAccount.parse genererar en InvalidKeyException så du måste placera den i ett try/catch-block.

Skapa en filresurs

Relaterad artikel: Utveckla för Azure Files med Java

Alla filer och kataloger i Azure Files lagras i en container som kallas resurs.

Om du vill få åtkomst till en resurs och dess innehåll skapar du en Azure Files-klient. Följande kodexempel visar hur du skapar en filresurs:

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

Med hjälp av Azure Files-klienten kan du sedan hämta en referens till en resurs.

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

Om du vill skapa resursen använder du metoden createIfNotExists för CloudFileShare-objektet .

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

I det här läget innehåller resursen en referens till en resurs med namnet exempelresurs.

Ta bort en filresurs

Relaterad artikel: Utveckla för Azure Files med Java

Följande exempelkod tar bort en filresurs.

Ta bort en resurs genom att anropa metoden deleteIfExists på ett CloudFileShare-objekt .

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();
}

Skapa en katalog

Relaterad artikel: Utveckla för Azure Files med Java

Du kan organisera lagringen genom att placera filer i underkataloger i stället för att ha alla i rotkatalogen.

Följande kod skapar en underkatalog med namnet sampledir under rotkatalogen:

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

Ta bort en katalog

Relaterad artikel: Utveckla för Azure Files med Java

Följande kodexempel visar hur du tar bort en katalog. Du kan inte ta bort en katalog som fortfarande innehåller filer eller underkataloger.

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

Räkna upp filer och kataloger i en Azure-filresurs

Relaterad artikel: Utveckla för Azure Files med Java

Hämta en lista över filer och kataloger genom att anropa listFilesAndDirectories på en CloudFileDirectory-referens . Metoden returnerar en lista över ListFileItem-objekt som du kan iterera på.

Följande kod visar filer och kataloger i rotkatalogen:

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

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

Ladda upp en fil

Relaterad artikel: Utveckla för Azure Files med Java

Hämta en referens till katalogen där filen ska laddas upp genom att anropa metoden getRootDirectoryReference på resursobjektet.

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

Nu när du har en referens till resursens rotkatalog kan du ladda upp en fil till den med hjälp av följande kod:

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

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

Ladda ned en fil

Relaterad artikel: Utveckla för Azure Files med Java

I följande exempel hämtas SampleFile.txt och dess innehåll visas:

//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());

Ta bort en fil

Relaterad artikel: Utveckla för Azure Files med Java

Följande kod tar bort en fil med namnet SampleFile.txt lagras i en katalog med namnet 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");
}