package com.xjrsoft.module.oss.factory;

import com.obs.services.ObsClient;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.config.OSSConfig;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @Author: tzx
 * @Date:2022/3/8 10:52
 */
public class HwCloudStorageService extends CloudStorageService {
    private ObsClient client;

    public HwCloudStorageService(OSSConfig config){
        this.config = config;

        //初始化
        init();
    }

    private void init(){
        client = new ObsClient(config.getAccessKey(),
                config.getSecretKey(),config.getEndpoint());
    }

    @Override
    public String upload(byte[] data, String path) {
        return upload(new ByteArrayInputStream(data), path);
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        try {
            client.putObject(config.getBucketName(), path, inputStream);
        } catch (Exception e){
            throw new MyException("上传文件失败，请检查配置信息", e);
        }

        return config.getEndpoint() + "/" + path;
    }

    @Override
    public String uploadSuffix(byte[] data, String suffix) {
        return upload(data, getPath(config.getPrefix(), suffix));
    }

    @Override
    public String uploadSuffix(InputStream inputStream, String suffix) {
        return upload(inputStream, getPath(config.getPrefix(), suffix));
    }

}
