All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.crazywalker.fsf.oss.aliyun.AliyunOSSClient Maven / Gradle / Ivy

The newest version!
package cn.crazywalker.fsf.oss.aliyun;

import cn.crazywalker.fsf.oss.OSSClient;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.MessageFormat;

/**
 * Aliyun OOS客户端
 * @author CrazyWalker
 * @since 周日, 08/21 2022 18:46 GMT+8
 */
public class AliyunOSSClient extends OSSClient implements DisposableBean {

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

    private final OSS client;

    private final String bucketName;

    private final String oosUrlPattern;


    public AliyunOSSClient(AliyunOSSProperties aliyunOSSProperties) {
        String endpoint = aliyunOSSProperties.getEndpoint();
        this.client = new OSSClientBuilder().build(endpoint, aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
        this.bucketName = aliyunOSSProperties.getBucketName();
        this.oosUrlPattern = "https://" + this.bucketName + "." + endpoint + "/{0}";
        LOGGER.info("完成aliyun oos client初始化: endpoint: [{}]", endpoint);
    }

    @Override
    public boolean simpleUpload(String name, byte[] fileBytes) {
        this.client.putObject(bucketName, name, new ByteArrayInputStream(fileBytes));
        return true;
    }

    @Override
    public String getPathByOosKey(String oosKey) {
        return MessageFormat.format(this.oosUrlPattern, oosKey);
    }

    @Override
    public void destroy() throws Exception {
        client.shutdown();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy