我有一个 CloudBlobContainer,其中包含一个文件 (csv),我想将其移动到 Azure 文件共享。在当前方法中,我将容器中存储的文件下载到临时文件,然后将相同的临时文件上传到文件共享。
CloudBlockBlobsourceBlob = container.getBlockBlobReference(sourceFilePath);
CloudFile destFile = cloudDir.getFileReference(destFileName);
File tmpCsvFile = Files.createTempFile(destFileName, "")
.toFile();
sourceBlob.downloadToFile(tmpCsvFile.getAbsolutePath().substring(0,
tmpCsvFile.getAbsolutePath().lastIndexOf(File.separator)) + File.separator + destFileName);
destFile.uploadFromFile(tmpCsvFile.getAbsolutePath().substring(0,
tmpCsvFile.getAbsolutePath().lastIndexOf(File.separator)) + File.separator + destFileName);
我想知道是否可以直接移动/复制文件而无需下载它,因为对于较大的文件,这部分代码可能会花费很多时间。
我遇到了 .startCopy 并尝试按如下方式使用它。存储详细信息、文件路径/名称都与之前的方法相同。
CloudBlockBlob sourceBlob = container.getBlockBlobReference(sourceFilePath);
CloudFile destFile = cloudDir.getFileReference(destFileName);
if (!destFile.exists()) {
destFile.create(1024);
}
destFile.startCopy(sourceBlob);
通过这些更改,我得到以下异常。
com.microsoft.azure.storage.StorageException: The specified resource does not exist. " at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:89) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.core.StorageRequest.materializeException(StorageRequest.java:305) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:175) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.file.CloudFile.startCopy(CloudFile.java:472) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.file.CloudFile.startCopy(CloudFile.java:362) ~[azure-storage-5.0.0.jar!/:?]" " at com.microsoft.azure.storage.file.CloudFile.startCopy(CloudFile.java:320) ~[azure-storage-5.0.0.jar!/:?]"
在 Spring Boot 应用程序中将文件从 Azure Blob 容器移动到 Azure 文件共享
您可以使用下面的代码通过 Java 将文件从 Azure Blob 存储复制到 Azure 文件共享。
代码:
public class App {
public static void main(String[] args) {
String connectionString = "xxxxx";
String sourceContainerName = "test";
String sourceBlobName = "titanic.csv";
String destinationShareName = "share1";
String destinationDirectoryName = "sample";
String destinationFileName = "test.csv";
// Create a BlobServiceClient object to connect to your Azure Storage account
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.buildClient();
BlobContainerClient sourceContainerClient = blobServiceClient.getBlobContainerClient(sourceContainerName);
ShareFileClient fileClient = new ShareFileClientBuilder().connectionString(connectionString).shareName(destinationShareName).resourcePath(destinationDirectoryName + "/" + destinationFileName).buildFileClient();
BlobClient sourceBlobClient = sourceContainerClient.getBlobClient(sourceBlobName);
String sourceBlobUrl = sourceBlobClient.getBlobUrl();
SyncPoller<ShareFileCopyInfo, Void> poller = fileClient.beginCopy(sourceBlobUrl, (Map<String, String>) null, Duration.ofSeconds(2));
PollResponse<ShareFileCopyInfo> pollResponse = poller.poll();
while (!pollResponse.getStatus().isComplete()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
return;
}
}
if (pollResponse.getStatus().isComplete() && pollResponse.getStatus() != null) {
ShareFileCopyInfo value = pollResponse.getValue();
System.out.printf("Copy source: %s. Status: %s.%n", value.getCopySourceUrl(), value.getCopyStatus());
} else {
System.out.println("Copy operation failed.");
}
}
}
依赖:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-file-share</artifactId>
<version>12.21.4</version>
</dependency>
输出:
Copy source: https://venkat123.blob.core.windows.net/test%2Ftitanic.csv. Status: success.
将文件从 Azure Blob 复制到文件共享后,您可以删除源 Blob。
传送门: