阿里云oss拷贝(包含移动的代码)文件并返回下载地址

oss拷贝文件官方地址:
https://help.aliyun.com/zh/oss/developer-reference/java-copy-objects?spm=a2c4g.11186623.0.0.16f76083xr3lKM
在这里插入图片描述

步骤1:oss的Maven依赖

		<!-- OSS -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.12.0</version>
        </dependency>

步骤2:配置文件:application.yml

在这里插入图片描述

步骤3:配置文件对象-OSSProperties.java

package com.hcl.demo.configure.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OSSProperties {

    private String fileUrlPrefix;
    private String bucketName;
    private String diskName;
    private String regionId;
    private String endpoint;
    private String accessKey;
    private String accessSecret;
    private String oss_path;

    @Bean
    public OSS ossClient(){
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKey, accessSecret);
        return new OSSClientBuilder().build(endpoint, accessKey, accessSecret);
    }
}

步骤4:处理文件地址和文件名称,copyFile方法

public String copyFile(String fileUrl) {

        String reUrl = null;
        // 原文件路径加文件名称:例如:xxx/test/testfile.xlsx
        String sourceObjectName = null;
        try {
        	// 处理是图片还是文件,我们系统区分了文件和图片
            String url1 = "https://file.xxx.com/";
            String url2 = "https://pic.xxx.com/";
            if (fileUrl.contains(url1)) {
                sourceObjectName = fileUrl.substring(url1.length());
            }
            if (fileUrl.contains(url2)) {
                sourceObjectName = fileUrl.substring(url2.length());
            }
            int doIndex = fileUrl.lastIndexOf("/");
            // 文件名称带后缀 例如:testfile.xlsx
            String fileName = fileUrl.substring(doIndex + 1);
            reUrl = this.ossCopyFileToNewPath(sourceObjectName, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reUrl;
    }

步骤5:执行拷贝操作,ossCopyFileToNewPath方法返回新地址

private String ossCopyFileToNewPath(String sourceObjectName, String fileName) {

        String sourceBucketName = ossProperties.getBucketName();
//        String sourceObjectName = "xxx/test/testfile.xlsx";
        String destinationBucketName = ossProperties.getBucketName();
        String destinationObjectName = "xxxa/copy/" + fileName;


        // 创建OSS连接
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getAccessSecret());
        // 判断bucket(存储空间)是否存在
        boolean exist = ossClient.doesBucketExist(ossProperties.getBucketName());
        JSONObject json = new JSONObject();
        if (!exist) {
            json.put("result", "存储空间不存在");
        }
        try {
            ossClient.copyObject(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName);
            // 如果是移动文件就可以执行下面步骤
            // ossClient.deleteObject(sourceBucketName, sourceObjectName);//比复制文件多了一步删除
        } catch (Exception e) {
            log.info("报错信息:" + e.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return ossProperties.getFileUrlPrefix() + destinationObjectName;
    }

参数说明:
在这里插入图片描述
断点查看
在这里插入图片描述
补充:移动文件操作,就是在上面拷贝文件后删除原路径的文件即可。

ossClient.copyObject(sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName);
// 如果是移动文件就可以执行下面步骤
ossClient.deleteObject(sourceBucketName, sourceObjectName);//比复制文件多了一步删除

在这里插入图片描述
注意:有些依赖没有的可以自行导入,例如lombok等

		<!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值