MCPcopy Create free account
hub / github.com/Hello-hao/Tbed / COSImageupload

Class COSImageupload

src/main/java/cn/hellohao/service/impl/COSImageupload.java:26–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24import java.util.Map;
25
26@Service
27public class COSImageupload {
28 private static Logger logger = LoggerFactory.getLogger(COSImageupload.class);
29 static COSClient cosClient;
30 static Keys key;
31
32 public ReturnImage ImageuploadCOS(
33 Map<Map<String, String>, File> fileMap, String username, Integer keyID) {
34 ReturnImage returnImage = new ReturnImage();
35 File file = null;
36 FileInputStream stream = null;
37 try {
38 for (Map.Entry<Map<String, String>, File> entry : fileMap.entrySet()) {
39 String prefix = entry.getKey().get("prefix");
40 String ShortUIDName = entry.getKey().get("name");
41 file = entry.getValue();
42 stream = new FileInputStream(file);
43 try {
44 String bucketName = key.getBucketname();
45 String userkey = username + "/" + ShortUIDName + "." + prefix;
46 PutObjectRequest putObjectRequest =
47 new PutObjectRequest(bucketName, userkey, stream,null);
48 PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
49 } catch (CosServiceException serverException) {
50 returnImage.setCode("400");
51 serverException.printStackTrace();
52 } catch (CosClientException clientException) {
53 returnImage.setCode("400");
54 clientException.printStackTrace();
55 }
56 try {if(stream!=null){stream.close();}} catch (Exception ex) { }
57 }
58 returnImage.setCode("200");
59 } catch (Exception e) {
60 try {if(stream!=null){stream.close();}} catch (Exception ex) { }
61 logger.error("COS协议上传时发生异常:",e);
62 returnImage.setCode("500");
63 }
64 return returnImage;
65 }
66
67 public static Integer Initialize(Keys k) {
68 int ret = -1;
69 if (StringUtils.isBlank(k.getAccessKey())
70 || StringUtils.isBlank(k.getAccessSecret())
71 || StringUtils.isBlank(k.getEndpoint())
72 || StringUtils.isBlank(k.getBucketname())
73 || StringUtils.isBlank(k.getRequestAddress())) {
74 return -1;
75 }
76 String secretId = k.getAccessKey();
77 String secretKey = k.getAccessSecret();
78 COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
79 Region region = new Region(k.getEndpoint());
80 ClientConfig clientConfig = new ClientConfig(region);
81 COSClient cos = new COSClient(cred, clientConfig);
82 ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
83 listObjectsRequest.setBucketName(k.getBucketname());

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected