Skip to content

Commit bce0ab1

Browse files
iroquetaBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:CreateFileFromStream' into beta
1 parent c2dd7d4 commit bce0ab1

File tree

6 files changed

+102
-31
lines changed

6 files changed

+102
-31
lines changed

gxcloudstorage-awss3-v1/src/main/java/com/genexus/db/driver/ExternalProviderS3V1.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.amazonaws.services.s3.AmazonS3;
1919
import com.amazonaws.services.s3.AmazonS3Client;
20-
import com.amazonaws.util.IOUtils;
2120

2221
import java.io.FileNotFoundException;
2322
import java.io.FileOutputStream;
@@ -220,23 +219,31 @@ else if (acl == ResourceAccessControlList.PublicReadWrite) {
220219
}
221220

222221
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
223-
byte[] bytes;
222+
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
224223
try {
225-
bytes = IOUtils.toByteArray(input);
224+
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
225+
226226
ObjectMetadata metadata = new ObjectMetadata();
227-
metadata.setContentLength(bytes.length);
227+
metadata.setContentLength(streamInfo.contentLength);
228+
228229
if (externalFileName.endsWith(".tmp")) {
229230
metadata.setContentType("image/jpeg");
230231
}
231232
String upload = "";
232-
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
233-
client.putObject(new PutObjectRequest(bucket, externalFileName, byteArrayInputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
234-
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);
235-
}
233+
client.putObject(new PutObjectRequest(bucket, externalFileName, streamInfo.inputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
234+
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);
236235
return upload;
237236
} catch (IOException ex) {
238237
logger.error("Error while uploading file to the external provider.", ex);
239238
return "";
239+
} finally {
240+
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
241+
try {
242+
streamInfo.tempFile.delete();
243+
} catch (Exception e) {
244+
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
245+
}
246+
}
240247
}
241248
}
242249

gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
import com.genexus.util.StorageUtils;
2323
import com.genexus.StructSdtMessages_Message;
2424
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
25-
import software.amazon.awssdk.utils.IoUtils;
2625

2726
import java.io.*;
2827
import java.net.URI;
2928
import java.net.URLEncoder;
30-
import java.nio.ByteBuffer;
3129
import java.nio.charset.StandardCharsets;
3230
import java.nio.file.Files;
3331
import java.nio.file.Path;
@@ -626,8 +624,10 @@ else if (acl == ResourceAccessControlList.PublicReadWrite)
626624
}
627625

628626
private String uploadWithACL(String externalFileName, InputStream input, ResourceAccessControlList acl) {
627+
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
629628
try {
630-
ByteBuffer byteBuffer = ByteBuffer.wrap(IoUtils.toByteArray(input));
629+
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
630+
631631
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
632632
.bucket(bucket)
633633
.key(externalFileName)
@@ -636,7 +636,7 @@ private String uploadWithACL(String externalFileName, InputStream input, Resourc
636636
putObjectRequestBuilder = putObjectRequestBuilder.acl(internalToAWSACLWithACL(acl));
637637
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
638638

639-
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromByteBuffer(byteBuffer));
639+
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromInputStream(streamInfo.inputStream, streamInfo.contentLength));
640640
if (!response.sdkHttpResponse().isSuccessful()) {
641641
logger.error("Error while uploading file: " + response.sdkHttpResponse().statusText().orElse("Unknown error"));
642642
}
@@ -645,6 +645,15 @@ private String uploadWithACL(String externalFileName, InputStream input, Resourc
645645
} catch (IOException ex) {
646646
logger.error("Error while uploading file to the external provider.", ex);
647647
return "";
648+
} finally {
649+
// Clean up the temporary file if it was created
650+
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
651+
try {
652+
streamInfo.tempFile.delete();
653+
} catch (Exception e) {
654+
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
655+
}
656+
}
648657
}
649658
}
650659

@@ -750,15 +759,17 @@ private String uploadWithoutACL(String localFile, String externalFileName) {
750759
}
751760

752761
private String uploadWithoutACL(String externalFileName, InputStream input) {
762+
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
753763
try {
754-
ByteBuffer byteBuffer = ByteBuffer.wrap(IoUtils.toByteArray(input));
764+
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
765+
755766
PutObjectRequest.Builder putObjectRequestBuilder = PutObjectRequest.builder()
756767
.bucket(bucket)
757768
.key(externalFileName)
758769
.contentType(externalFileName.endsWith(".tmp") ? "image/jpeg" : null);
759770
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
760771

761-
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromByteBuffer(byteBuffer));
772+
PutObjectResponse response = client.putObject(putObjectRequest, RequestBody.fromInputStream(streamInfo.inputStream, streamInfo.contentLength));
762773
if (!response.sdkHttpResponse().isSuccessful()) {
763774
logger.error("Error while uploading file: " + response.sdkHttpResponse().statusText().orElse("Unknown error"));
764775
}
@@ -767,6 +778,15 @@ private String uploadWithoutACL(String externalFileName, InputStream input) {
767778
} catch (IOException ex) {
768779
logger.error("Error while uploading file to the external provider.", ex);
769780
return "";
781+
} finally {
782+
// Clean up the temporary file if it was created
783+
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
784+
try {
785+
streamInfo.tempFile.delete();
786+
} catch (Exception e) {
787+
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
788+
}
789+
}
770790
}
771791
}
772792

gxcloudstorage-azureblob/src/main/java/com/genexus/db/driver/ExternalProviderAzureStorage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ public String upload(String externalFileName, InputStream input, ResourceAccessC
146146
blob.getProperties().setContentType("image/jpeg");
147147
}
148148
try (BlobOutputStream blobOutputStream = blob.openOutputStream()) {
149-
int next = input.read();
150-
while (next != -1) {
151-
blobOutputStream.write(next);
152-
next = input.read();
149+
byte[] buffer = new byte[8192];
150+
int bytesRead;
151+
while ((bytesRead = input.read(buffer)) != -1) {
152+
blobOutputStream.write(buffer, 0, bytesRead);
153153
}
154154
}
155155
return getResourceUrl(externalFileName, acl, DEFAULT_EXPIRATION_MINUTES);

gxcloudstorage-common/src/main/java/com/genexus/db/driver/ExternalProviderHelper.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.genexus.util.Encryption;
44
import com.genexus.util.GXService;
55

6+
import java.io.*;
7+
68
public class ExternalProviderHelper {
79

810
public static String getServicePropertyValue(GXService s, String propName, boolean isSecure){
@@ -23,4 +25,30 @@ public static String getEnvironmentVariable(String name, boolean required) throw
2325
}
2426
return value;
2527
}
28+
29+
public static InputStreamWithLength getInputStreamContentLength(InputStream input) throws IOException {
30+
File tempFile = File.createTempFile("upload-", ".tmp");
31+
try (OutputStream out = new FileOutputStream(tempFile)) {
32+
byte[] buffer = new byte[8192];
33+
int bytesRead;
34+
while ((bytesRead = input.read(buffer)) != -1) {
35+
out.write(buffer, 0, bytesRead);
36+
}
37+
}
38+
long size = tempFile.length();
39+
InputStream newInput = new FileInputStream(tempFile);
40+
return new InputStreamWithLength(newInput, size, tempFile);
41+
}
42+
43+
public static class InputStreamWithLength {
44+
public final InputStream inputStream;
45+
public final long contentLength;
46+
public final File tempFile; // nullable
47+
48+
public InputStreamWithLength(InputStream inputStream, long contentLength, File tempFile) {
49+
this.inputStream = inputStream;
50+
this.contentLength = contentLength;
51+
this.tempFile = tempFile;
52+
}
53+
}
2654
}

gxcloudstorage-googlecloudstorage/src/main/java/com/genexus/db/driver/ExternalProviderGoogle.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import com.google.api.services.storage.model.StorageObject;
1717
import com.google.auth.oauth2.ServiceAccountCredentials;
1818
import com.google.cloud.storage.*;
19-
import org.apache.commons.io.IOUtils;
2019
import org.apache.logging.log4j.LogManager;
2120
import org.apache.logging.log4j.Logger;
2221

@@ -170,16 +169,28 @@ private void setBlobAcl(BlobId blobId, ResourceAccessControlList acl) {
170169
}
171170

172171
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
172+
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
173173
try {
174+
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
175+
174176
BlobId blobId = BlobId.of(bucket, externalFileName);
175177
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
176-
byte[] targetArray = IOUtils.toByteArray(input);
177-
storageClient.create(blobInfo, targetArray);
178+
179+
storageClient.createFrom(blobInfo, streamInfo.tempFile.toPath());
180+
178181
setBlobAcl(blobId, acl);
179182
return getResourceUrl(blobInfo, acl);
180183
} catch (IOException ex) {
181184
handleIOException(ex);
182185
return "";
186+
} finally {
187+
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
188+
try {
189+
streamInfo.tempFile.delete();
190+
} catch (Exception e) {
191+
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
192+
}
193+
}
183194
}
184195
}
185196

gxcloudstorage-ibmcos/src/main/java/com/genexus/db/driver/ExternalProviderIBM.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.ibm.cloud.objectstorage.services.s3.AmazonS3Client;
1515
import com.ibm.cloud.objectstorage.services.s3.AmazonS3ClientBuilder;
1616
import com.ibm.cloud.objectstorage.services.s3.model.*;
17-
import com.ibm.cloud.objectstorage.util.IOUtils;
1817
import org.apache.logging.log4j.LogManager;
1918
import org.apache.logging.log4j.Logger;
2019

@@ -150,24 +149,30 @@ else if (acl == ResourceAccessControlList.PublicReadWrite) {
150149
}
151150

152151
public String upload(String externalFileName, InputStream input, ResourceAccessControlList acl) {
153-
byte[] bytes;
154-
try {
155-
bytes = IOUtils.toByteArray(input);
152+
ExternalProviderHelper.InputStreamWithLength streamInfo = null;
153+
try {
154+
streamInfo = ExternalProviderHelper.getInputStreamContentLength(input);
156155
ObjectMetadata metadata = new ObjectMetadata();
157-
metadata.setContentLength(bytes.length);
156+
metadata.setContentLength(streamInfo.contentLength);
158157
if (externalFileName.endsWith(".tmp")) {
159158
metadata.setContentType("image/jpeg");
160159
}
161160
String upload = "";
162-
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) {
163-
client.putObject(new PutObjectRequest(bucket, externalFileName, byteArrayInputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
164-
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);
165-
}
161+
client.putObject(new PutObjectRequest(bucket, externalFileName, streamInfo.inputStream, metadata).withCannedAcl(internalToAWSACL(acl)));
162+
upload = getResourceUrl(externalFileName, acl, defaultExpirationMinutes);
166163
return upload;
167164
} catch (IOException ex) {
168165
logger.error("Error while uploading file to the external provider.", ex);
169166
return "";
170-
}
167+
} finally {
168+
if (streamInfo != null && streamInfo.tempFile != null && streamInfo.tempFile.exists()) {
169+
try {
170+
streamInfo.tempFile.delete();
171+
} catch (Exception e) {
172+
logger.warn("Could not delete temporary file: " + streamInfo.tempFile.getAbsolutePath(), e);
173+
}
174+
}
175+
}
171176
}
172177

173178
public String get(String externalFileName, ResourceAccessControlList acl, int expirationMinutes) {

0 commit comments

Comments
 (0)