阿里云OSS上传文件 工具类拿来直接用

准备:找运维创建存储空间Bucket,并获取账号信息endpoint、accessKeyId、secretAccessKey

注意:上传文件的大小不超过5GB

然后引入阿里OSS SDK坐标

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

定义工具类

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.bzcst.iot.provider.config.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.time.LocalDate;
import java.util.UUID;

/**
 * 上传文件到阿里云OSS
 *
 * @author 向振华
 * @date 2021/05/12
 */
@Slf4j
public class AliOssUtils {

    private static final String endpoint = "xxx";
    private static final String accessKeyId = "xxx";
    private static final String secretAccessKey = "xxx";
    private static final String bucketName = "xxx";
    // 文件夹
    public static final String dirDefault = "default/";
    public static final String dirBusiness = "business/";

    public static String upload(MultipartFile file) {
        return uploadOss(dirDefault + getFileName(file), getInputStream(file));
    }

    public static String upload(String dir, MultipartFile file) {
        return uploadOss(dir + getFileName(file), getInputStream(file));
    }

    public static String upload(String dir, String fileName, InputStream is) {
        return uploadOss(dir + fileName, is);
    }

    private static String uploadOss(String key, InputStream is) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secretAccessKey);
        try {
            // 上传文件
            ossClient.putObject(bucketName, key, is);
            // 访问权限(如果不设置,则继承bucket权限)
            ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead);
        } catch (Exception e) {
            log.error("uploadOss Exception", e);
            return null;
        } finally {
            ossClient.shutdown();
        }
        // 返回图片URL
        return String.format("https://%s.%s/%s", bucketName, endpoint, key);
    }

    private static InputStream getInputStream(MultipartFile file) {
        try {
            return file.getInputStream();
        } catch (Exception e) {
            log.error("getInputStream Exception", e);
            throw new BusinessException("文件流获取失败");
        }
    }

    private static String getFileName(MultipartFile file) {
        String fileType = null;
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isNotBlank(originalFilename)) {
            fileType = originalFilename.substring(originalFilename.lastIndexOf('.'));
        }
        return getFileName() + fileType;
    }

    private static String getFileName() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String now = LocalDate.now().toString().replace("-", "");
        return now + uuid;
    }
}

 

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页