英文:
Efficient way to get some files from AzureDevops repository and upload them to Azure Blob Store's container
问题
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class AzureBlobUploader {
public static void main(String[] args) {
String repositoryPath = "path/to/repository";
String storageConnectionString = "your_storage_connection_string";
try {
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(storageConnectionString)
.buildClient();
File repoDirectory = new File(repositoryPath);
File[] folders = repoDirectory.listFiles(File::isDirectory);
if (folders != null) {
for (File folder : folders) {
String containerName = folder.getName();
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
containerClient.createIfNotExists();
File[] jsonFiles = folder.listFiles((dir, name) -> name.endsWith(".json"));
if (jsonFiles != null) {
for (File jsonFile : jsonFiles) {
String blobName = jsonFile.getName();
BlobClient blobClient = containerClient.getBlobClient(blobName);
byte[] data = Files.readAllBytes(jsonFile.toPath());
blobClient.upload(data, data.length, true);
System.out.println("Uploaded: " + blobName);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Note: Remember to replace "path/to/repository"
with the actual path to your repository, and "your_storage_connection_string"
with the connection string of your Azure Storage Account.
Make sure you have the necessary Azure SDK dependencies in your project for this code to work.
Also, ensure that you handle exceptions and edge cases appropriately for production use.
英文:
Suppose I have a repository in AzureDevops in which the structure is like some folders are having the json files inside them.
Ex:
Folder A:
File1.json
File2.json
Folder B:
File3.json
Folder C:
File4.json
I need to fetch these files from the repository and place it in the existing Azure Storage Account containers named: Folder A, Folder B, Folder C.
Please provide an optimal solution using java. I tried several ways which includes:
- Cloning of whole repo in local FS and then uploading files one by one to blob.
- Used Azure Devops rest APIs to fetch the contents of the file one by one and the uploaded each on the blob in there respective blob name. However this leads to hitting of REST APIs number of files +1 times.
答案1
得分: 1
以下是翻译好的内容:
从AzureDevops仓库获取文件并上传到Azure Blob Store容器的高效方法
有一个任务叫做Azure文件复制,它可以用来将文件复制到Microsoft Azure存储容器或虚拟机(VMs)。
你可以查看这篇文档获取更多细节。
英文:
> Efficient way to get some files from AzureDevops repository and upload them to Azure Blob Store's container
There this a task Azure File Copy, which could be used to copy files to Microsoft Azure storage blobs or virtual machines (VMs).
You could check this document for some more details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论