obs工具类:
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.HeaderResponse;
import com.ruoyi.common.utils.DictUtils;
import java.io.File;
/**
* 华为obs对象存储api
*/
public class ObsTemplate {
public static String endPoint = "https://your-endpoint";
public static String ak = "*** Provide your Access Key ***";
public static String sk = "*** Provide your Secret Key ***";
public static String bucketName="";
public static ObsTemplate obsTemplate;
public static ObsClient obsClient = null;// 创建ObsClient实例
public static ObsTemplate getInstance()
{
if(obsTemplate == null)
{
init();
obsTemplate = new ObsTemplate();
}
return obsTemplate;
}
public static void init(){
endPoint= DictUtils.getDictLabel("obs","endpoint");
ak=DictUtils.getDictLabel("obs","accesskey");
sk=DictUtils.getDictLabel("obs","secretKey");
bucketName=DictUtils.getDictLabel("obs","bucketName");
obsClient = new ObsClient(ak, sk, endPoint);// 创建ObsClient实例
}
public String putObs(String filePath,String objectName){
// 您的工程中可以只保留一个全局的ObsClient实例
// ObsClient是线程安全的,可在并发场景下使用
try
{
// 调用接口进行操作,例如上传对象
HeaderResponse response = obsClient.putObject(bucketName, objectName,
new File(filePath)); // localfile为待上传的本地文件路径,需要指定到具体的文件名
System.out.println(response);
return null;
}
catch (ObsException e)
{
System.out.println("HTTP Code: " + e.getResponseCode());
System.out.println("Error Code:" + e.getErrorCode());
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Request ID:" + e.getErrorRequestId());
System.out.println("Host ID:" + e.getErrorHostId());
return e.getMessage();
}
}
}
上传到obs:
String result= ObsTemplate.getInstance().putObs('源文件地址','上传到obs的地址');