parent
cb1d9ef00a
commit
34089d10f9
@ -0,0 +1,74 @@
|
||||
package com.luoo.tag.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import software.amazon.awssdk.http.SdkHttpResponse;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
|
||||
import util.IdWorker;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* ClassName: TagImageUtil
|
||||
* Description: TagImageUtil
|
||||
* Date: 2024-02-04
|
||||
*
|
||||
* @author zjb
|
||||
*/
|
||||
@Service
|
||||
public class TagS3Service {
|
||||
|
||||
@Resource
|
||||
private S3Client s3Client;
|
||||
|
||||
|
||||
public static final String DOT = ".";
|
||||
public static final String TAG_RESOURCE_PREFIX = "http://cdn.indie.cn/tag/";
|
||||
public static final String BUCKET = "indie";
|
||||
public static final String TEMP_KEY_PREFIX = "temp/";
|
||||
|
||||
|
||||
public String moveTagImage(String srcKey, String oldImage, String tagId) {
|
||||
String image = oldImage;
|
||||
String suffix = image.substring(image.lastIndexOf(DOT));
|
||||
String destKeySuffix = String.format("%s%s", tagId, suffix);
|
||||
String destKey = TAG_RESOURCE_PREFIX + destKeySuffix;
|
||||
int copy = copy(BUCKET, srcKey, destKey);
|
||||
if (copy > 0) {
|
||||
return destKeySuffix;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* s3复制文件
|
||||
*
|
||||
* @param bucket 桶
|
||||
* @param srcKey 源key
|
||||
* @param destKey 目标key
|
||||
* @return -
|
||||
*/
|
||||
public int copy(String bucket, String srcKey, String destKey) {
|
||||
try {
|
||||
CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder()
|
||||
.sourceBucket(bucket).sourceKey(srcKey).destinationBucket(bucket).destinationKey(destKey).build();
|
||||
CopyObjectResponse copyObjectResponse = s3Client.copyObject(copyObjectRequest);
|
||||
SdkHttpResponse sdkHttpResponse = copyObjectResponse.sdkHttpResponse();
|
||||
if (!sdkHttpResponse.isSuccessful()) {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return -1;
|
||||
} finally {
|
||||
s3Client.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getSrcKey(String srcFullPath) {
|
||||
return srcFullPath.substring(srcFullPath.indexOf(TEMP_KEY_PREFIX));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue