You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Java将Azure存储Blob复制到Linux虚拟机(函数应用场景)

实现方案:Azure函数(Java)将Blob存储文件复制到Linux虚拟机

1. 依赖配置(pom.xml)

需要引入Azure Functions SDK、Azure Storage Blob SDK,以及用于SSH传输的JSch库:

<dependencies>
    <!-- Azure Functions Core -->
    <dependency>
        <groupId>com.microsoft.azure.functions</groupId>
        <artifactId>azure-functions-java-library</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- Azure Storage Blob -->
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-blob</artifactId>
        <version>12.20.0</version>
    </dependency>
    <!-- JSch for SSH file transfer -->
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.55</version>
    </dependency>
</dependencies>

2. Blob触发型函数代码示例

该函数会在Blob存储指定容器有新文件时自动触发,将文件复制到Linux VM:

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.annotation.*;
import com.microsoft.azure.functions.*;
import com.jcraft.jsch.*;

import java.io.*;
import java.util.Properties;

public class BlobToVmFunction {

    // 从环境变量读取配置(推荐在Azure函数应用的配置面板中设置)
    private static final String STORAGE_CONNECTION_STRING = System.getenv("AZURE_STORAGE_CONNECTION_STRING");
    private static final String VM_SSH_HOST = System.getenv("VM_SSH_HOST");
    private static final String VM_SSH_USERNAME = System.getenv("VM_SSH_USERNAME");
    private static final String VM_SSH_PASSWORD = System.getenv("VM_SSH_PASSWORD");
    private static final String VM_TARGET_PATH = System.getenv("VM_TARGET_PATH"); // 例如 /home/user/uploaded-files/

    @FunctionName("BlobToVmCopy")
    public void run(
            @BlobTrigger(
                name = "blob",
                dataType = "binary",
                path = "your-source-container/{name}",
                connection = "AZURE_STORAGE_CONNECTION_STRING") byte[] content,
            @BindingName("name") String fileName,
            final ExecutionContext context) {

        context.getLogger().info("Processing blob file: " + fileName);

        File tempFile = null;
        try {
            // 1. 将Blob内容写入临时文件
            tempFile = File.createTempFile(fileName, null);
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                fos.write(content);
            }

            // 2. 通过SSH连接Linux VM并上传文件
            JSch jsch = new JSch();
            Session session = jsch.getSession(VM_SSH_USERNAME, VM_SSH_HOST, 22);
            session.setPassword(VM_SSH_PASSWORD);

            // 配置SSH会话(生产环境建议开启主机密钥验证)
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();
            context.getLogger().info("SSH connection to VM established");

            // 开启SFTP通道传输文件
            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            channelSftp.put(tempFile.getAbsolutePath(), VM_TARGET_PATH + fileName);
            context.getLogger().info("File " + fileName + " uploaded to VM successfully");

            // 释放资源
            channelSftp.disconnect();
            session.disconnect();

        } catch (Exception e) {
            context.getLogger().severe("Error processing blob: " + e.getMessage());
            throw new RuntimeException(e);
        } finally {
            // 清理临时文件
            if (tempFile != null && tempFile.exists()) {
                tempFile.delete();
            }
        }
    }
}

3. 关键配置说明

  • 环境变量设置:在Azure函数应用的「配置」→「应用程序设置」中添加以下变量:

    • AZURE_STORAGE_CONNECTION_STRING:Blob存储账户的连接字符串
    • VM_SSH_HOST:Linux虚拟机的公网IP或域名
    • VM_SSH_USERNAME:VM的SSH登录用户名
    • VM_SSH_PASSWORD:VM的SSH登录密码(生产环境推荐替换为SSH密钥认证)
    • VM_TARGET_PATH:VM上接收文件的目标目录(需确保目录存在且有写入权限)
  • Blob触发器调整:修改@BlobTrigger注解中的path参数为你要监听的Blob容器名称

  • 生产环境优化建议:

    • 替换密码认证为SSH密钥对认证,避免明文密码泄露
    • 配置Azure Functions的重试策略,提升异常场景下的可靠性
    • 使用Azure Key Vault存储敏感信息,替代直接存储在环境变量中

内容的提问来源于stack exchange,提问作者manish bansal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 05:45:36