oss上传和删除文件

import java.io.ByteArrayInputStream;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

@Component
public class UploadFileUtil {

    @Value("${oss.end.point}")
    private String endPoint;
    @Value("${oss.access.key.id}")
    private String accessKeyId;
    @Value("${oss.access.key.secret}")
    private String accessKeySecret;
    @Value("${oss.bucket.name}")
    private String bucketName;
    @Value("${oss.dir}")
    private String dir;

    private static final Logger LOGGER = LoggerFactory.getLogger(UploadFileUtil.class);

    public String uploadImage(String base64) {
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        String imageType = "jpg";
        if (base64.startsWith("data:image")) {
            imageType = base64.substring(11, base64.indexOf(";"));
        }
        if (base64.startsWith("data:img")) {
            imageType = base64.substring(9, base64.indexOf(";"));
        }
        String fileName = dir + "/" + UUID.randomUUID() + "." + imageType;
        if (StringUtils.indexOf(base64, ",") > 0) {
            base64 = StringUtils.substringAfterLast(base64, ",");
        }
        ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(Base64.getDecoder().decode(base64)));
        ossClient.shutdown();
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString();
        LOGGER.info("oss图片链接,{}", url);
        return url;
    }

    public String uploadFile(MultipartFile file) throws Exception {
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = dir + "/" + UUID.randomUUID() + type;
        ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(file.getBytes()));
        ossClient.shutdown();
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        String url = ossClient.generatePresignedUrl(bucketName, fileName, expiration).toString();
        LOGGER.info("oss文件链接,{}", url);
        return url;
    }

    public boolean deleteFile(String filePath) {
        OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
        boolean exist = ossClient.doesObjectExist(bucketName, filePath);
        if (!exist) {
            LOGGER.error("文件不存在,filePath={}", filePath);
            return false;
        }
        LOGGER.info("删除文件,filePath={}", filePath);
        ossClient.deleteObject(bucketName, filePath);
        ossClient.shutdown();
        return true;
    }

}

 

你可能感兴趣的:(oss)