package org.springblade.modules.resource.builder.oss;

import com.obs.services.ObsClient;
import com.obs.services.model.AccessControlList;
import com.obs.services.model.CreateBucketRequest;
import com.obs.services.model.ObsBucket;
import org.springblade.core.oss.HuaweiObsTemplate;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.props.OssProperties;
import org.springblade.core.oss.rule.OssRule;
import org.springblade.modules.resource.entity.Oss;

import java.io.ByteArrayInputStream;

/**
 * Author pangqijun
 * Date 2023/5/9
 * Description
 */
public class HuaWeiOssBuilder {

    public static OssTemplate template(Oss oss, OssRule ossRule) {
        // 创建配置类
        OssProperties ossProperties = new OssProperties();
        ossProperties.setEndpoint(oss.getEndpoint());
        ossProperties.setAccessKey(oss.getAccessKey());
        ossProperties.setSecretKey(oss.getSecretKey());
        ossProperties.setBucketName(oss.getBucketName());
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(oss.getAccessKey(), oss.getSecretKey(), oss.getEndpoint());
        return new HuaweiObsTemplate(obsClient, ossProperties, ossRule);
    }

    public static void main(String[] args) {
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient("W9SDMFJI3CDMSYBJYEFW", "EsdI4vvuqkEMdtikbnZCt8hVwXRj0UfSTJXISuJi",
                "obs.cn-southwest-2.myhuaweicloud.com");
//        String bucketLocation = obsClient.getBucketLocation("yj-bucket");
//        System.out.println(bucketLocation);
//        ObsBucket bucket = obsClient.createBucket("yj-bucket");

//        bucket.setLocation("cn-east-2");


//        CreateBucketRequest request = new CreateBucketRequest();
//        request.setBucketName("devbucket");
//        request.setLocation("cn-southwest-2");
//        request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
//        ObsBucket bucket = obsClient.createBucket(request);

        obsClient.putObject("devbucket", "aa", new ByteArrayInputStream("Hello OBS".getBytes()));
    }
}
