fix: stream tencent cos uploads

This commit is contained in:
hy001 2026-05-15 11:52:55 +08:00
parent f7a08cdf09
commit 403cff15c0

View File

@ -14,8 +14,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.UUID;
@ -48,18 +46,14 @@ public class TencentCosStorageService {
public void upload(String key, InputStream inputStream, long contentLength, String contentType) {
COSClient client = buildClient();
try {
byte[] data = inputStream.readAllBytes();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(data.length);
metadata.setContentLength(contentLength);
if (StringUtils.hasText(contentType)) {
metadata.setContentType(contentType);
}
client.putObject(
new PutObjectRequest(
properties.getBucketName(), key, new ByteArrayInputStream(data), metadata));
} catch (IOException e) {
log.error("tencent cos read upload stream failed key={}", key, e);
throw new IllegalStateException("read upload stream failed", e);
properties.getBucketName(), key, inputStream, metadata));
} catch (Exception e) {
log.error("tencent cos upload failed key={}", key, e);
throw e;