1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| public class TencentCOSUploadUtil extends UploadAbstractUtil {
public static final Logger logger = LoggerFactory.getLogger(TencentCOSUploadUtil.class);
private String qAccessKey;
private String qSecretKey;
private String qBucket;
private String qRegion;
private String qEndpoint;
private String qEndpointExternal;
private COSClient cosClient;
public TencentCOSUploadUtil(String basedir, String qAccessKey, String qSecretKey, String qBucket, String qRegion, String qEndpoint, String qEndpointExternal) { super(basedir); this.qAccessKey = qAccessKey; this.qSecretKey = qSecretKey; this.qBucket = qBucket; this.qRegion = qRegion; this.qEndpoint = qEndpoint; this.qEndpointExternal = qEndpointExternal; }
@Override protected String upload(File tempFile, String realName) { initClient(); cosClient.putObject(qBucket,realName,tempFile); URL url =cosClient.generatePresignedUrl(qBucket, realName, new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100)); String urlString = String.valueOf(url).split("\\?")[0]; logger.info("腾讯云COS上传服务------图片内网url:[{}]", urlString); urlString = urlString.replaceAll(qEndpoint, qEndpointExternal); logger.info("腾讯云COS上传服务------图片外网url:[{}]", urlString); return urlString; }
@Override String upload(MultipartFile file) { initClient(); String key = generateUploadFileName(file); try (InputStream is = new ByteArrayInputStream(file.getBytes())) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding(StandardCharsets.UTF_8.name()); metadata.setContentLength(file.getSize()); metadata.setContentType(file.getContentType()); cosClient.putObject(qBucket,key, is, metadata); URL url = cosClient.generatePresignedUrl(qBucket, key, new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100)); String urlString = String.valueOf(url).split("\\?")[0]; logger.info("腾讯云COS上传服务------图片内网url:[{}]", urlString); urlString = urlString.replaceAll(qEndpoint, qEndpointExternal); logger.info("腾讯云COS上传服务------图片外网url:[{}]", urlString); return urlString; }catch (IOException e){ logger.error("使用腾讯云COS上传文件出现异常",e); throw new RuntimeException(e); } }
@Override protected String upload(byte[] bytes, String contentType) { initClient(); String realName = UUID.randomUUID().toString() + ".jpg"; try (InputStream is = new ByteArrayInputStream(bytes)) { ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentEncoding(StandardCharsets.UTF_8.name()); metadata.setContentLength(is.available()); metadata.setContentType(contentType); cosClient.putObject(qBucket,realName, is, metadata); URL url = cosClient.generatePresignedUrl(qBucket, realName, new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100)); String urlString = String.valueOf(url).split("\\?")[0]; logger.info("腾讯云COS上传服务------图片内网url:[{}]", urlString); urlString = urlString.replaceAll(qEndpoint, qEndpointExternal); logger.info("腾讯云COS上传服务------图片外网url:[{}]", urlString); return urlString; }catch (IOException e){ logger.error("使用腾讯云COS上传文件出现异常",e); throw new RuntimeException(e); } }
@Override protected void initClient() { if(cosClient==null){ COSCredentials cosCredentials = new BasicCOSCredentials(qAccessKey,qSecretKey); ClientConfig clientConfig = new ClientConfig(new Region(qRegion)); cosClient = new COSClient(cosCredentials,clientConfig); } }
public void shutdown(){ cosClient.shutdown(); } }
|