代码之家  ›  专栏  ›  技术社区  ›  Sandy

使用Java编写的Azure函数将blob从一个存储帐户复制到另一个

  •  -1
  • Sandy  · 技术社区  · 1 年前

    我正在用java编写一个Azure函数,用于在同一租户内的存储帐户之间复制blob,而无需先将其加载到内存中。我偶然发现 this post 这建议使用AzCopy来做到这一点。我从以下网址下载了AzCopy二进制文件 this location 并在创建docker镜像之前将其添加到函数根目录中。下面是我在java中调用azcopy二进制文件的代码,这在本地运行良好,但在Kubernetes中部署函数时什么也不做。不确定这是二进制文件的问题,还是环境的问题,因为函数部署在Kubernetes而不是函数应用程序中。

    源和目标是带有临时SAS令牌的URL。

    ProcessBuilder processBuilder = new ProcessBuilder(azCopyBinaryLocation, "copy", source, destination);
        Process process = processBuilder.start();
    
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
    
        StringBuilder sb = new StringBuilder();
    
        while ((line = br.readLine()) != null) {
            sb.append("\n");
            sb.append(line);
        }
    
        context.getLogger().info("Logs: "+sb.toString()); //print logs in console
    

    我不确定我是否走在正确的道路上,比如我应该使用azcopy还是尝试使用java-sdk,对于sdk,我不确定blob api是否在复制到另一个存储帐户之前将内容加载到内存中,如果是这样的话,它会很慢,内存密集,我想不惜一切代价避免。我正在寻找一个像azcopy一样使用azure骨干网的选项。

    0 回复  |  直到 1 年前
        1
  •  1
  •   Vivek Vaibhav Shandilya    1 年前

    我用过 copyfromurl 将文件从一个存储复制到另一个存储的方法。

    下面的代码对我有效。

    我使用了相同的名称作为销毁文件名。如果要更改文件名,可以使用任何文件名。

    如果使用 getBlobUrl ,应在中启用允许blob匿名访问 storage account => Configuration 如果没有,需要使用 SAS_url .

    package com.function;
    
    import java.util.*;
    import com.microsoft.azure.functions.annotation.*;
    import com.azure.storage.blob.BlobClient;
    import com.azure.storage.blob.BlobContainerClient;
    import com.azure.storage.blob.BlobServiceClient;
    import com.azure.storage.blob.BlobServiceClientBuilder;
    import com.microsoft.azure.functions.*;
    import com.azure.identity.*;;
    
    public class HttpTriggerJava1 {
        @FunctionName("HttpTriggerJava1")
        public HttpResponseMessage run(
                @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
                final ExecutionContext context) {
            context.getLogger().info("Java HTTP trigger processed a request.");
             
            
            //Source Account
            String sourceContainerName = "source";
            String blobName = "oldfile.txt";
    
    
            BlobServiceClient sourceServiceClient = new BlobServiceClientBuilder().credential(new DefaultAzureCredentialBuilder().build()).endpoint("https://storage29aug.blob.core.windows.net").buildClient();
    
            BlobContainerClient sourceContainerClient = sourceServiceClient.getBlobContainerClient(sourceContainerName);
    
            BlobClient sourceBlobClient = sourceContainerClient.getBlobClient(blobName);
    
    
            //Destination Account
            String destinationContainerName = "destination";
    
            BlobServiceClient destinationServiceClient = new BlobServiceClientBuilder().credential(new DefaultAzureCredentialBuilder().build()).endpoint("https://destinationstorage29aug.blob.core.windows.net").buildClient();
    
            BlobContainerClient destinationContainerClient = destinationServiceClient.getBlobContainerClient(destinationContainerName);
    
            BlobClient destinatiBlobClient = destinationContainerClient.getBlobClient(blobName);
    
            destinatiBlobClient.copyFromUrl(sourceBlobClient.getBlobUrl());
            
            return request.createResponseBuilder(HttpStatus.OK).body("blob copied successfully").build();
        }
    }
    

    pom.xml :

    <dependencies>
            <dependency>
                <groupId>com.microsoft.azure.functions</groupId>
                <artifactId>azure-functions-java-library</artifactId>
                <version>3.1.0</version>
            </dependency>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-storage-blob</artifactId>
                <version>12.27.1</version>
            </dependency>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-identity</artifactId>
                <version>1.13.2</version>
            </dependency>
    </dependencies>
    

    OUTPUT :