高效从AzureDevops仓库获取文件并上传到Azure Blob存储容器的方法:

huangapple go评论79阅读模式
英文:

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:

  1. Cloning of whole repo in local FS and then uploading files one by one to blob.
  2. 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)。

高效从AzureDevops仓库获取文件并上传到Azure Blob存储容器的方法:

你可以查看这篇文档获取更多细节。

英文:

> 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).

高效从AzureDevops仓库获取文件并上传到Azure Blob存储容器的方法:

You could check this document for some more details.

huangapple
  • 本文由 发表于 2020年9月18日 01:34:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63943393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定