Skip to content

Commit

Permalink
set read limit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Aug 20, 2024
1 parent b4cff14 commit bf1ad28
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.lucee</groupId>
<artifactId>s3-extension</artifactId>
<version>2.0.2.19-SNAPSHOT</version>
<version>2.0.2.20-SNAPSHOT</version>
<packaging>pom</packaging>
<name>S3 Extension</name>

Expand Down
42 changes: 36 additions & 6 deletions source/java/src/org/lucee/extension/resource/s3/S3.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.lucee.extension.resource.s3;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -105,6 +106,7 @@ public class S3 {
public static final String[] PROVIDERS = new String[] { AWS, WASABI, BACKBLAZE, ".digitaloceanspaces.com", DREAM_IO, GOOGLE };

private static final ConcurrentHashMap<String, Object> tokens = new ConcurrentHashMap<String, Object>();
private static final int READ_LIMIT = 1024 * 1024;
private static int errorThreshold = 0;
private static int warnThreshold = 0;

Expand Down Expand Up @@ -1742,7 +1744,10 @@ public void write(String bucketName, String objectName, String data, String mime
md.setLastModified(new Date());
// create a PutObjectRequest passing the folder name suffixed by /
md.setContentLength(bytes.length);
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(bytes), md);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
bis.mark(READ_LIMIT);
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, bis, md);
por.getRequestClientOptions().setReadLimit(READ_LIMIT); // Set the read limit on the client

if (acl != null) setACL(por, acl);
try {
Expand Down Expand Up @@ -1774,6 +1779,7 @@ public void write(String bucketName, String objectName, String data, String mime
}
finally {
client.release();
Util.closeEL(bis);
}
}
}
Expand Down Expand Up @@ -1811,7 +1817,11 @@ public void write(String bucketName, String objectName, byte[] data, String mime
}
else {
ObjectMetadata md = new ObjectMetadata();
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(data), md);
ByteArrayInputStream bis = new ByteArrayInputStream(data);
bis.mark(READ_LIMIT);
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, bis, md);
por.getRequestClientOptions().setReadLimit(READ_LIMIT); // Set the read limit on the client

String ct = CFMLEngineFactory.getInstance().getResourceUtil().getMimeType(data, null);
if (ct != null) md.setContentType(ct);
md.setLastModified(new Date());
Expand All @@ -1837,6 +1847,7 @@ public void write(String bucketName, String objectName, byte[] data, String mime
}
finally {
client.release();
Util.closeEL(bis);
}
}
}
Expand Down Expand Up @@ -1910,11 +1921,15 @@ public void write(String bucketName, String objectName, File file, Object acl, S

}
else {

// create a PutObjectRequest passing the folder name suffixed by /
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, file);
if (acl != null) setACL(por, acl);
por.setMetadata(md);
InputStream is = null;
try {
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, is = getInputStream(file), md);
por.getRequestClientOptions().setReadLimit(READ_LIMIT); // Set the read limit on the client
if (acl != null) setACL(por, acl);
por.setMetadata(md);

client.putObject(por);
flushExists(bucketName, objectName);
}
Expand Down Expand Up @@ -1942,6 +1957,7 @@ public void write(String bucketName, String objectName, File file, Object acl, S
}
finally {
client.release();
Util.closeEL(is);
}
}
}
Expand Down Expand Up @@ -1984,7 +2000,9 @@ public void write(String bucketName, String objectName, Resource res, Object acl
// create a PutObjectRequest passing the folder name suffixed by /

try {
PutObjectRequest por = new PutObjectRequest(bucketName, objectName, is = res.getInputStream(), md);

PutObjectRequest por = new PutObjectRequest(bucketName, objectName, is = getInputStream(res), md);
por.getRequestClientOptions().setReadLimit(READ_LIMIT); // Set the read limit on the client
if (acl != null) setACL(por, acl);

client.putObject(por);
Expand Down Expand Up @@ -2019,6 +2037,18 @@ public void write(String bucketName, String objectName, Resource res, Object acl
}
}

private InputStream getInputStream(Resource res) throws IOException {
BufferedInputStream bis = new BufferedInputStream(res.getInputStream());
bis.mark(READ_LIMIT);
return bis;
}

private InputStream getInputStream(File file) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
bis.mark(1024 * 1024);
return bis;
}

public Struct getMetaDataStruct(String bucketName, String objectName) throws S3Exception {
Struct sct = CFMLEngineFactory.getInstance().getCreationUtil().createStruct();
bucketName = improveBucketName(bucketName);
Expand Down

0 comments on commit bf1ad28

Please sign in to comment.