fix: stream tencent cos uploads
This commit is contained in:
parent
f7a08cdf09
commit
403cff15c0
@ -14,8 +14,6 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@ -48,18 +46,14 @@ public class TencentCosStorageService {
|
|||||||
public void upload(String key, InputStream inputStream, long contentLength, String contentType) {
|
public void upload(String key, InputStream inputStream, long contentLength, String contentType) {
|
||||||
COSClient client = buildClient();
|
COSClient client = buildClient();
|
||||||
try {
|
try {
|
||||||
byte[] data = inputStream.readAllBytes();
|
|
||||||
ObjectMetadata metadata = new ObjectMetadata();
|
ObjectMetadata metadata = new ObjectMetadata();
|
||||||
metadata.setContentLength(data.length);
|
metadata.setContentLength(contentLength);
|
||||||
if (StringUtils.hasText(contentType)) {
|
if (StringUtils.hasText(contentType)) {
|
||||||
metadata.setContentType(contentType);
|
metadata.setContentType(contentType);
|
||||||
}
|
}
|
||||||
client.putObject(
|
client.putObject(
|
||||||
new PutObjectRequest(
|
new PutObjectRequest(
|
||||||
properties.getBucketName(), key, new ByteArrayInputStream(data), metadata));
|
properties.getBucketName(), key, inputStream, metadata));
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("tencent cos read upload stream failed key={}", key, e);
|
|
||||||
throw new IllegalStateException("read upload stream failed", e);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("tencent cos upload failed key={}", key, e);
|
log.error("tencent cos upload failed key={}", key, e);
|
||||||
throw e;
|
throw e;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user