117 lines
4.1 KiB
Java
117 lines
4.1 KiB
Java
package digital.application.utils;
|
|
|
|
import com.volcengine.tos.*;
|
|
import com.volcengine.tos.auth.StaticCredentials;
|
|
import com.volcengine.tos.model.object.PutObjectInput;
|
|
import com.volcengine.tos.model.object.PutObjectOutput;
|
|
import com.volcengine.tos.transport.TransportConfig;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
public class CloudStorageService {
|
|
static Long startTime = 0l;
|
|
|
|
static Long endTime = 0l;
|
|
|
|
static String bucketPath = "hair/result/image/";
|
|
|
|
private static TOSV2 tosClient = null;
|
|
|
|
private static TOSV2 getInstance() {
|
|
if (tosClient == null) {
|
|
tosClient = createTosClient();
|
|
}
|
|
return tosClient;
|
|
|
|
}
|
|
|
|
private static TOSV2 createTosClient() {
|
|
String endpoint = "tos-accelerate.volces.com";
|
|
String region = "ap-southeast-1";
|
|
String accessKey = "AKLTMDA2YjdiNGRmZDJmNGM2Y2JiYTA2MDFmZTVhMGU2Yzc";
|
|
String secretKey = "WWpnMk1HWmxZV1l3TWpVME5EazJZemxtTkRrMU1ERTVNV0pqWlRFMFpqYw==";
|
|
|
|
int idleConnectionTimeMills = 60000;
|
|
int maxConnections = 64;
|
|
|
|
TransportConfig config = TransportConfig.builder()
|
|
.idleConnectionTimeMills(idleConnectionTimeMills)
|
|
.maxConnections(maxConnections)
|
|
.build();
|
|
TOSClientConfiguration configuration = TOSClientConfiguration.builder()
|
|
.transportConfig(config)
|
|
.region(region)
|
|
.endpoint(endpoint)
|
|
.credentials(new StaticCredentials(accessKey, secretKey))
|
|
.build();
|
|
|
|
TOSV2 tos = new TOSV2ClientBuilder().build(configuration);
|
|
return tos;
|
|
}
|
|
|
|
|
|
private static boolean saveByteDanceTos(String fileName, InputStream inputStream) {
|
|
|
|
startTime = System.currentTimeMillis();
|
|
TOSV2 tos = getInstance();
|
|
endTime = System.currentTimeMillis();
|
|
startTime = System.currentTimeMillis();
|
|
|
|
String bucketName = "hairstyle";
|
|
// TOSV2 提供的所有接口均会抛出 TosException 异常,需要使用 try-catch 进行捕获并处理。
|
|
try {
|
|
// 设置上传的桶名和对象名
|
|
String objKey = bucketPath + fileName;
|
|
|
|
PutObjectInput putObjectInput = new PutObjectInput().setBucket(bucketName).setKey(objKey).setContent(inputStream);
|
|
// 上传对象
|
|
PutObjectOutput output = tos.putObject(putObjectInput);
|
|
|
|
endTime = System.currentTimeMillis();
|
|
|
|
return true;
|
|
} catch (TosClientException e) {
|
|
// 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送
|
|
System.out.println("putObject failed");
|
|
System.out.println("Message: " + e.getMessage());
|
|
if (e.getCause() != null) {
|
|
e.getCause().printStackTrace();
|
|
}
|
|
} catch (TosServerException e) {
|
|
// 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息
|
|
System.out.println("putObject failed");
|
|
System.out.println("StatusCode: " + e.getStatusCode());
|
|
System.out.println("Code: " + e.getCode());
|
|
System.out.println("Message: " + e.getMessage());
|
|
System.out.println("RequestID: " + e.getRequestID());
|
|
} catch (Throwable t) {
|
|
// 作为兜底捕获其他异常,一般不会执行到这里
|
|
System.out.println("putObject failed");
|
|
System.out.println("unexpected exception, message: " + t.getMessage());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 上传文件 到 云存储
|
|
*
|
|
* @param fileName
|
|
* @param fileStream
|
|
* @return
|
|
*/
|
|
public static String saveFileToBucket(String fileName, InputStream fileStream) {
|
|
String resultUrl = null;
|
|
try {
|
|
boolean saveResult = saveByteDanceTos(fileName, fileStream);
|
|
if(saveResult){
|
|
resultUrl = "https://hairstyle.tos-accelerate.volces.com/"+bucketPath+fileName;
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return resultUrl;
|
|
}
|
|
|
|
}
|