JAVA使用阿里云Serverless 函数计算将OSS文件打包成Zip批量下载

批量下载OSS文件的几种方法

  1. 单个文件依次下载
    阿里云OSS中批量下载文件使用的就是这种单个文件依次下载的方法,下载时会弹出提示,体验不是很好,也无法下载文件夹保持文件目录结构
    image.png

  2. 将OSS文件下载到自己服务器中,然后自己生成一个Zip文件,完成批量下载

这种方法的缺点 下载比较慢、如果文件过多会消耗服务器CPU资源和带宽

  1. 使用阿里云函数计算打包OSS文件下载

阿里云函数计算官方Demo中有一个python的例子,可惜python用的不多,所以使用Java实现一个类似的功能
image.png

使用JAVA 打包OSS文件

  1. 创建Maven项目,添加阿里云函数计算依赖

<dependencies>
    <dependency>
        <groupId>com.aliyun.fc.runtime</groupId>
        <artifactId>fc-java-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun.fc.runtime</groupId>
        <artifactId>fc-java-common</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>
</dependencies>
<build>
    <finalName>zip-oss</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${
    
    project.build.directory}/classes/lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 实现Http触发接口
package example;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.Credentials;
import com.aliyun.fc.runtime.HttpRequestHandler;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class App implements HttpRequestHandler {
    
    

    String endpoint = "oss-cn-hangzhou.aliyuncs.com";


    public void handleRequest(HttpServletRequest request, HttpServletResponse response, Context context)
            throws IOException {
    
    
        String bucket = request.getParameter("bucket");
        String objectKey= request.getParameter("objectKey");
        Credentials creds = context.getExecutionCredentials();
        OSSClient client = new OSSClient(endpoint, creds.getAccessKeyId(), creds.getAccessKeySecret(), creds.getSecurityToken());
        response.setContentType("application/x-zip-compressed");
        ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
        String [] objectKeys=objectKey.split(",");
        for (String key : objectKeys) {
    
    
            OSSObject ossObject = client.getObject(bucket, key);
            ZipEntry zipEntry = new ZipEntry(key);
            zipOutputStream.putNextEntry(zipEntry);
            IOUtils.copy(ossObject.getObjectContent(), zipOutputStream);
            ossObject.close();
        }
        zipOutputStream.closeEntry();
        zipOutputStream.finish();
    }
}

3.打包工程

mvn clean package

将Jar包部署到阿里云函数计算

1.新建函数

image.png

2.部署jar包

image.png

3.创建Http触发器
image.png

4.测试下载
浏览器访问触发HTTP地址,增加bucket参数和要下载的文件名即可打包下载OSS文件了

http://XXX.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/app_test.LATEST/java_zhaochao/?bucket=XXX-public&objectKey=1.jpg,1.mp4

image.png

猜你喜欢

转载自blog.csdn.net/whzhaochao/article/details/119823374