import com.obs.services.ObsClient;
import com.obs.services.model.ObjectMetadata;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
public class ObsUploadUtil {
public static String endPoint = "*******";
public static String accessKeyId = "*******";
public static String accessKeySecret = "*******";
public static String bucketName = "*******";
public static String oss_domain = "*******";
public static ObsClient obsClient = new ObsClient(accessKeyId, accessKeySecret, endPoint);
public static String obsUpload(HttpServletRequest request, MultipartFile file) throws IOException{
String fileName = "";
if(file!=null && !"".equals(file.getOriginalFilename()) && file.getOriginalFilename()!=null){
InputStream content = file.getInputStream();
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(file.getSize());
String originalFilename = file.getOriginalFilename();
fileName = UUID.randomUUID().toString().replaceAll("-","") + originalFilename.subSequence(originalFilename.lastIndexOf("."), originalFilename.length());
obsClient.putObject(bucketName,"admin/"+fileName,content,meta);
if(fileName != null && !"".equals(fileName)){
System.out.println(fileName);
fileName = oss_domain+"admin/"+fileName;
}
}
return fileName;
}
public static boolean deleteObject(String bucketUrl) {
try {
bucketUrl=bucketUrl.replace(oss_domain+"web","");
obsClient.deleteObject(bucketName, bucketUrl);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
}
return true;
}
}