簡體   English   中英

如何使用 java 文件系統 sdk 從 azure 存儲中重命名或復制文件

[英]How to rename or copy file from azure storage using java file system sdk

如何使用 java 文件系統 sdk 從 azure 存儲中重命名或復制文件 有沒有辦法從 azurestorage.jar for java 重命名或復制存儲在 azurestorage 中的文件,如果有請幫助我們。

假設文件在系統掛載的文件共享上,可以使用Files.copy(...)復制文件

Path sourcePath = new File("path/to/source/file").toPath();
Path targetPath = new File("path/to/target/file").toPath();
Files.copy(sourcePath, targetPath);

請注意,該代碼會將源文件下載到本地主機,然后上傳到 azure 存儲服務。

如果要避免下載和上傳,請使用azure storage rest api復制文件。 如果不想直接處理 rest api,請使用azure-sdk-for-java或類似的用於 python 和 C# 的 SDK。

https://stackoverflow.com/a/66774796/12066108展示了如何使用 azure-sdk-for-java 庫復制文件。

您可以使用CloudFile.startCopy(source)重命名和復制 blob 文件。 這是完整的代碼。

package nau.edu.cn.steven;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.file.CopyStatus;
import com.microsoft.azure.storage.file.CloudFile;
import com.microsoft.azure.storage.file.CloudFileClient;
import com.microsoft.azure.storage.file.CloudFileDirectory;
import com.microsoft.azure.storage.file.CloudFileShare;

public class AzureCopyFile {

        // Connection string
        public static final String storageConnectionString =
    "DefaultEndpointsProtocol=http;"
    + "AccountName=your_account_name;"
    + "AccountKey= your_account_key";

        public static void main( String[] args )
        {
            try {

                CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
                CloudFileClient fileClient = account.createCloudFileClient();

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

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

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

                // Old file
                CloudFile oldCloudFile = rootDir.getFileReference("Readme.txt");
                // New file
                CloudFile newCloudFile = rootDir.getFileReference("Readme2.txt");
                // Start copy
                newCloudFile.startCopy(oldCloudFile.getUri());

                // Exit until copy finished
                while(true) {
                    if(newCloudFile.getCopyState().getStatus() != CopyStatus.PENDING) {
                        break;
                    }

                    // Sleep for a second maybe
                    Thread.sleep(1000);
                }
            }
            catch(Exception e) {
                System.out.print("Exception encountered: ");
                System.out.println(e.getMessage());
                System.exit(-1);
            }
        }
}

根據 Azure 文件存儲的CloudFile類的 javadocs,文件存儲本身不支持重命名操作,即使是 Blob 存儲也是如此。

如果要進行重命名操作,則需要執行 2 個步驟,包括復制具有新名稱的文件和刪除具有舊名稱的文件。

下面有兩個線程與 SO & MSDN 分開。

  1. 使用 File (not Blob) Storage 以編程方式 (.NET) 重命名 Azure 文件或目錄,這與使用 Java 相同。
  2. https://social.msdn.microsoft.com/Forums/azure/en-US/04c415fb-cc1a-4270-986b-03a68b05aa81/renaming-files-in-blobs-storage?forum=windowsazuredata

正如@Steven 所說,通過函數startCopy支持復制操作startCopy獲取新文件引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM