codebot :
나는 GCP 클라우드 스토리지에 업로드 크기 데이터 파일의 3기가바이트 있습니다. 나는 업로드 튜토리얼 객체 GCP에있는 예제를 시도했다. 내가 업로드하려고 해요 그러나 나는 다음과 같은 오류가 발생했습니다.
java.lang.OutOfMemoryError: Required array size too large
다음과 같이 내가 시도
BlobId blobId = BlobId.of(gcpBucketName, "ft/"+file.getName());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.get().create(blobInfo, Files.readAllBytes(Paths.get(file.getAbsolutePath())));
return blob.exists();
나는이 문제를 어떻게 해결할 수 있습니다? GCP 클라우드 스토리지 자바 클라이언트를 사용하여 대용량 파일을 업로드 할 수있는 가능한 방법이 있습니까?
알렉세이 Alexeenka :
저장 버전 :
<artifactId>google-cloud-storage</artifactId>
<version>1.63.0</version>
예비:
BlobId blobId = BlobId.of(BUCKET_NAME, date.format(BASIC_ISO_DATE) + "/" + prefix + "/" + file.getName());
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("application/gzip").build();
uploadToStorage(storage, file, blobInfo);
홈페이지 방법 :
private void uploadToStorage(Storage storage, File uploadFrom, BlobInfo blobInfo) throws IOException {
// For small files:
if (uploadFrom.length() < 1_000_000) {
byte[] bytes = Files.readAllBytes(uploadFrom.toPath());
storage.create(blobInfo, bytes);
return;
}
// For big files:
// When content is not available or large (1MB or more) it is recommended to write it in chunks via the blob's channel writer.
try (WriteChannel writer = storage.writer(blobInfo)) {
byte[] buffer = new byte[10_240];
try (InputStream input = Files.newInputStream(uploadFrom.toPath())) {
int limit;
while ((limit = input.read(buffer)) >= 0) {
writer.write(ByteBuffer.wrap(buffer, 0, limit));
}
}
}
}