package com.dianguang.cloud.ossservice.service;

import com.dianguang.cloud.ossservice.config.CloudStorageConfig;
import com.obs.services.ObsClient;

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

public class HuaweiCloudStorageService extends AbstractCloudStorageService {



    public HuaweiCloudStorageService(CloudStorageConfig config){
        this.config = config;
    }
    @Override
    public String upload(byte[] data, String path) throws Exception {

        return upload(new ByteArrayInputStream(data), path);
    }

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

    @Override
    public String upload(InputStream inputStream, String path) throws Exception {
        ObsClient client = new ObsClient(config.getAccessKey(),config.getSecretKey(),config.getEndPoint());
        try {
            client.putObject(config.getBucketName(), path, inputStream);
        } catch (Exception e){
            throw new Exception("上传失败");
        }
        return config.getUrl() + "/" + path;
    }

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

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