Aracılığıyla paylaş


Java sürüm 8 istemci kitaplıklarını kullanan Azure Dosya Paylaşımı kod örnekleri

Bu makalede, Java için Azure Dosya Paylaşımı istemci kitaplığının 8. sürümünü kullanan kod örnekleri gösterilmektedir.

31 Mart 2023'te, geçerli Azure SDK yönergelerine uymayan Azure SDK kitaplıkları desteğini kullanımdan kaldırdık. Yeni Azure SDK kitaplıkları, tutarlı deneyimler sağlamak ve güvenlik duruşunuzu güçlendirmek için düzenli olarak güncelleştirilir. Yeni özelliklerden ve kritik güvenlik güncelleştirmelerinden yararlanmak için yeni Azure SDK kitaplıklarına geçiş yapmanızı öneririz.

Eski kitaplıklar 31 Mart 2023'ten sonra da kullanılabilir olsa da artık Microsoft'tan resmi destek ve güncelleştirmeler almayacaktır. Daha fazla bilgi için bkz . destek kullanımdan kaldırma duyurusu.

Önkoşullar

Azure Dosya Paylaşımı istemci kitaplığını kullanmak için aşağıdaki import yönergeleri ekleyin:

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

Azure dosya paylaşımına erişme

İlgili makale: Java ile Azure Dosyalar geliştirme

Depolama hesabınıza erişmek için CloudStorageAccount nesnesini kullanarak bağlantı dizesi ayrıştırma yöntemine geçirin.

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

CloudStorageAccount.parse bir InvalidKeyException oluşturur, bu nedenle bunu bir try/catch bloğunun içine yerleştirmeniz gerekir.

Dosya paylaşımı oluşturma

İlgili makale: Java ile Azure Dosyalar geliştirme

Azure Dosyalar içindeki tüm dosyalar ve dizinler, paylaşım adı verilen bir kapsayıcıda depolanır.

Bir paylaşıma ve içeriğine erişim elde etmek için bir Azure Dosyalar istemcisi oluşturun. Aşağıdaki kod örneği, dosya paylaşımının nasıl oluşturulacağını gösterir:

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

Azure Dosyalar istemcisini kullanarak bir paylaşıma başvuru alabilirsiniz.

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

Paylaşımı gerçekten oluşturmak için CloudFileShare nesnesinin createIfNotExists yöntemini kullanın.

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

Bu noktada paylaşım, örnek paylaşım adlı bir paylaşıma başvuru barındırıyor.

Dosya paylaşımını silme

İlgili makale: Java ile Azure Dosyalar geliştirme

Aşağıdaki örnek kod bir dosya paylaşımını siler.

CloudFileShare nesnesinde deleteIfExists yöntemini çağırarak paylaşımı silin.

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

Dizin oluşturma

İlgili makale: Java ile Azure Dosyalar geliştirme

Dosyaları kök dizine yerleştirmek yerine alt dizinlere yerleştirerek depolamayı düzenleyebilirsiniz.

Aşağıdaki kod, kök dizinin altında sampledir adlı bir alt dizin oluşturur:

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

Bir dizini silme

İlgili makale: Java ile Azure Dosyalar geliştirme

Aşağıdaki kod örneğinde bir dizinin nasıl silineceği gösterilmektedir. Hala dosya veya alt dizin içeren bir dizini silemezsiniz.

// 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 dosya paylaşımındaki dosyaları ve dizinleri listeleme

İlgili makale: Java ile Azure Dosyalar geliştirme

CloudFileDirectory başvurusunda listFilesAndDirectories öğesini çağırarak dosyaların ve dizinlerin listesini alın. yöntemi, üzerinde yineleyebileceğiniz ListFileItem nesnelerinin listesini döndürür.

Aşağıdaki kod, kök dizin içindeki dosyaları ve dizinleri listeler:

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

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

Dosyayı karşıya yükleme

İlgili makale: Java ile Azure Dosyalar geliştirme

Share nesnesinde getRootDirectoryReference yöntemini çağırarak dosyanın karşıya yüklendiği dizine bir başvuru alın.

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

Artık paylaşımın kök dizinine bir başvurunuz olduğuna göre, aşağıdaki kodu kullanarak dosyaya yükleyebilirsiniz:

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

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

Dosya indirme

İlgili makale: Java ile Azure Dosyalar geliştirme

Aşağıdaki örnek SampleFile.txt indirir ve içeriğini görüntüler:

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

Dosya silme

İlgili makale: Java ile Azure Dosyalar geliştirme

Aşağıdaki kod, sampledir adlı bir dizinde depolanan SampleFile.txt adlı bir dosyayı siler:

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