Unverified Commit e7d415a6 authored by raylax's avatar raylax Committed by GitHub

Merge pull request #1 from hearace1/fix_OOM_when_upload_large_file

use the file inputstream for uploading
parents 9645d8db 5df6d4ef
package org.inurl.jenkins.plugin; package org.inurl.jenkins.plugin;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import javax.annotation.Nonnull;
import com.aliyun.oss.OSSClient; import com.aliyun.oss.OSSClient;
import hudson.Extension; import hudson.Extension;
import hudson.FilePath; import hudson.FilePath;
...@@ -18,12 +24,6 @@ import org.jenkinsci.Symbol; ...@@ -18,12 +24,6 @@ import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.QueryParameter;
import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class OSSPublisher extends Publisher implements SimpleBuildStep { public class OSSPublisher extends Publisher implements SimpleBuildStep {
private final String endpoint; private final String endpoint;
...@@ -40,7 +40,6 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -40,7 +40,6 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
private final String maxRetries; private final String maxRetries;
public String getEndpoint() { public String getEndpoint() {
return endpoint; return endpoint;
} }
...@@ -70,7 +69,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -70,7 +69,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
} }
@DataBoundConstructor @DataBoundConstructor
public OSSPublisher(String endpoint, String accessKeyId, String accessKeySecret, String bucketName, String localPath, String remotePath, String maxRetries) { public OSSPublisher(String endpoint, String accessKeyId, String accessKeySecret, String bucketName,
String localPath, String remotePath, String maxRetries) {
this.endpoint = endpoint; this.endpoint = endpoint;
this.accessKeyId = accessKeyId; this.accessKeyId = accessKeyId;
this.accessKeySecret = Secret.fromString(accessKeySecret); this.accessKeySecret = Secret.fromString(accessKeySecret);
...@@ -86,7 +86,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -86,7 +86,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
} }
@Override @Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher,
@Nonnull TaskListener listener) throws InterruptedException, IOException {
PrintStream logger = listener.getLogger(); PrintStream logger = listener.getLogger();
OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret.getPlainText()); OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret.getPlainText());
String local = localPath.substring(1); String local = localPath.substring(1);
...@@ -104,7 +105,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -104,7 +105,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
} }
private void upload(OSSClient client, PrintStream logger, String base, FilePath path, boolean root) throws InterruptedException, IOException { private void upload(OSSClient client, PrintStream logger, String base, FilePath path, boolean root)
throws InterruptedException, IOException {
if (path.isDirectory()) { if (path.isDirectory()) {
for (FilePath f : path.list()) { for (FilePath f : path.list()) {
upload(client, logger, base + (root ? "" : ("/" + path.getName())), f, false); upload(client, logger, base + (root ? "" : ("/" + path.getName())), f, false);
...@@ -114,7 +116,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -114,7 +116,8 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
uploadFile(client, logger, base + "/" + path.getName(), path); uploadFile(client, logger, base + "/" + path.getName(), path);
} }
private void uploadFile(OSSClient client, PrintStream logger, String key, FilePath path) throws InterruptedException, IOException { private void uploadFile(OSSClient client, PrintStream logger, String key, FilePath path)
throws InterruptedException, IOException {
if (!path.exists()) { if (!path.exists()) {
logger.println("file [" + path.getRemote() + "] not exists, skipped"); logger.println("file [" + path.getRemote() + "] not exists, skipped");
return; return;
...@@ -123,7 +126,7 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -123,7 +126,7 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
int retries = 0; int retries = 0;
do { do {
if (retries > 0) { if (retries > 0) {
logger.println("upload retrying (" + retries + "/" + maxRetries +")"); logger.println("upload retrying (" + retries + "/" + maxRetries + ")");
} }
try { try {
uploadFile0(client, logger, key, path); uploadFile0(client, logger, key, path);
...@@ -135,14 +138,14 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -135,14 +138,14 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
throw new RuntimeException("upload fail, more than the max of retries"); throw new RuntimeException("upload fail, more than the max of retries");
} }
private void uploadFile0(OSSClient client, PrintStream logger, String key, FilePath path) throws InterruptedException, IOException { private void uploadFile0(OSSClient client, PrintStream logger, String key, FilePath path)
throws InterruptedException, IOException {
String realKey = key; String realKey = key;
if (realKey.startsWith("/")) { if (realKey.startsWith("/")) {
realKey = realKey.substring(1); realKey = realKey.substring(1);
} }
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
path.copyTo(outputStream); InputStream inputStream = path.read();
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
logger.println("uploading [" + path.getRemote() + "] to [" + realKey + "]"); logger.println("uploading [" + path.getRemote() + "] to [" + realKey + "]");
client.putObject(bucketName, realKey, inputStream); client.putObject(bucketName, realKey, inputStream);
} }
...@@ -172,7 +175,6 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep { ...@@ -172,7 +175,6 @@ public class OSSPublisher extends Publisher implements SimpleBuildStep {
return checkValue(value, Messages.OSSPublish_MissingAccessKeySecret()); return checkValue(value, Messages.OSSPublish_MissingAccessKeySecret());
} }
public FormValidation doCheckBucketName(@QueryParameter(required = true) String value) { public FormValidation doCheckBucketName(@QueryParameter(required = true) String value) {
return checkValue(value, Messages.OSSPublish_MissingBucketName()); return checkValue(value, Messages.OSSPublish_MissingBucketName());
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment