Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
work
target
/.idea
*.iml
.DS_Store
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.0.2</version>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
Expand Down
328 changes: 168 additions & 160 deletions src/main/java/com/fit2cloud/jenkins/aliyunoss/AliyunOSSClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.fit2cloud.jenkins.aliyunoss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.Protocol;
import com.aliyun.oss.model.ObjectMetadata;
import hudson.FilePath;
import hudson.model.AbstractBuild;
Expand All @@ -14,164 +17,169 @@
import java.util.StringTokenizer;

public class AliyunOSSClient {
private static final String fpSeparator = ";";

public static boolean validateAliyunAccount(
final String aliyunAccessKey, final String aliyunSecretKey) throws AliyunOSSException {
try {
OSSClient client = new OSSClient(aliyunAccessKey, aliyunSecretKey);
client.listBuckets();
} catch (Exception e) {
throw new AliyunOSSException("阿里云账号验证失败:" + e.getMessage());
}
return true;
}


public static boolean validateOSSBucket(String aliyunAccessKey,
String aliyunSecretKey, String bucketName) throws AliyunOSSException{
try {
OSSClient client = new OSSClient(aliyunAccessKey, aliyunSecretKey);
client.getBucketLocation(bucketName);
} catch (Exception e) {
throw new AliyunOSSException("验证Bucket名称失败:" + e.getMessage());
}
return true;
}

public static int upload(AbstractBuild<?, ?> build, BuildListener listener,
final String aliyunAccessKey, final String aliyunSecretKey, final String aliyunEndPointSuffix, String bucketName,String expFP,String expVP) throws AliyunOSSException {
OSSClient client = new OSSClient(aliyunAccessKey, aliyunSecretKey);
String location = client.getBucketLocation(bucketName);
String endpoint = "http://" + location + aliyunEndPointSuffix;
client = new OSSClient(endpoint, aliyunAccessKey, aliyunSecretKey);
int filesUploaded = 0; // Counter to track no. of files that are uploaded
try {
FilePath workspacePath = build.getWorkspace();
if (workspacePath == null) {
listener.getLogger().println("工作空间中没有任何文件.");
return filesUploaded;
}
StringTokenizer strTokens = new StringTokenizer(expFP, fpSeparator);
FilePath[] paths = null;

listener.getLogger().println("开始上传到阿里云OSS...");
listener.getLogger().println("上传endpoint是:" + endpoint);

while (strTokens.hasMoreElements()) {
String fileName = strTokens.nextToken();
String embeddedVP = null;
if (fileName != null) {
int embVPSepIndex = fileName.indexOf("::");
if (embVPSepIndex != -1) {
if (fileName.length() > embVPSepIndex + 1) {
embeddedVP = fileName.substring(embVPSepIndex + 2, fileName.length());
if (Utils.isNullOrEmpty(embeddedVP)) {
embeddedVP = null;
}
if (embeddedVP != null && !embeddedVP.endsWith(Utils.FWD_SLASH)) {
embeddedVP = embeddedVP + Utils.FWD_SLASH;
}
}
fileName = fileName.substring(0, embVPSepIndex);
}
}

if (Utils.isNullOrEmpty(fileName)) {
return filesUploaded;
}

FilePath fp = new FilePath(workspacePath, fileName);

if (fp.exists() && !fp.isDirectory()) {
paths = new FilePath[1];
paths[0] = fp;
} else {
paths = workspacePath.list(fileName);
}

if (paths.length != 0) {
for (FilePath src : paths) {
String key = "";
if (Utils.isNullOrEmpty(expVP)
&& Utils.isNullOrEmpty(embeddedVP)) {
key = src.getName();
} else {
String prefix = expVP;
if (!Utils.isNullOrEmpty(embeddedVP)) {
if (Utils.isNullOrEmpty(expVP)) {
prefix = embeddedVP;
} else {
prefix = expVP + embeddedVP;
}
}
key = prefix + src.getName();
}
long startTime = System.currentTimeMillis();
InputStream inputStream = src.read();
try {
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(src.length());
meta.setContentType(getContentType(src));
listener.getLogger().println("File: " + src.getName() + ", Content Type: " + meta.getContentType());
client.putObject(bucketName, key, inputStream, meta);
} finally {
try {
inputStream.close();
} catch (IOException e) {
}
}
long endTime = System.currentTimeMillis();
listener.getLogger().println("Uploaded object ["+ key + "] in " + getTime(endTime - startTime));
listener.getLogger().println("版本下载地址:"+"http://"+bucketName+"."+location+aliyunEndPointSuffix+"/"+key);
filesUploaded++;
}
}else {
listener.getLogger().println(expFP+"下未找到Artifacts,请确认\"要上传的Artifacts\"中路径配置正确或部署包已正常生成,如pom.xml里assembly插件及配置正确。");
}
}
} catch (Exception e) {
e.printStackTrace();
throw new AliyunOSSException(e.getMessage(), e.getCause());
}
return filesUploaded;
}

public static String getTime(long timeInMills) {
return DurationFormatUtils.formatDuration(timeInMills, "HH:mm:ss.S") + " (HH:mm:ss.S)";
}


// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
// support the common web file types for now
private static final String[] COMMON_CONTENT_TYPES = {
".js", "application/js",
".json", "application/json",
".svg", "image/svg+xml",
".woff", "application/x-font-woff",
".woff2", "application/x-font-woff",
".ttf", "application/x-font-ttf"
};

// http://www.rgagnon.com/javadetails/java-0487.html
// we don't use the more robust solutions (checking magic headers) here, because the file stream might be
// loaded remotely, so that would be time consuming in checking, even hangs the Jenkins build in my test.
private static String getContentType(FilePath filePath) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String fileName = filePath.getName();
String type = fileNameMap.getContentTypeFor(fileName);

if (type == null) {
for (int i = 0; i < COMMON_CONTENT_TYPES.length; i += 2) {
String extension = COMMON_CONTENT_TYPES[i];
int beginIndex = Math.max(0, fileName.length() - extension.length());
if (fileName.substring(beginIndex).equals(extension)) {
return COMMON_CONTENT_TYPES[i + 1];
}
}
type = "application/octet-stream";
}
return type;
}
private static final String fpSeparator = ";";

public static boolean validateAliyunAccount(
final String endpoint,
final String aliyunAccessKey, final String aliyunSecretKey) throws AliyunOSSException {
try {
OSS client = new OSSClientBuilder().build(endpoint, aliyunAccessKey, aliyunSecretKey);
client.listBuckets();
} catch (Exception e) {
throw new AliyunOSSException("阿里云账号验证失败:" + e.getMessage());
}
return true;
}

public static boolean validateOSSBucket(String aliyunAccessKey,
String aliyunSecretKey, String aliyunEndPointSuffix, String bucketName) throws AliyunOSSException {
try {
OSS client = new OSSClientBuilder().build(aliyunEndPointSuffix, aliyunAccessKey, aliyunSecretKey);
client.getBucketLocation(bucketName);
} catch (Exception e) {
throw new AliyunOSSException("验证Bucket名称失败:" + e.getMessage());
}
return true;
}

public static int upload(AbstractBuild<?, ?> build, BuildListener listener,
final String aliyunAccessKey, final String aliyunSecretKey,
final String aliyunEndPointSuffix,
String bucketName, String expFP, String expVP) throws AliyunOSSException {
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
if (System.getProperty("http.proxyPort") != null) {
clientBuilderConfiguration.setProtocol(Protocol.HTTP);
clientBuilderConfiguration.setProxyHost(System.getProperty("http.proxyHost"));
clientBuilderConfiguration.setProxyPort(Integer.valueOf(System.getProperty("http.proxyPort")));
}
OSS client = new OSSClientBuilder().build(aliyunEndPointSuffix, aliyunAccessKey, aliyunSecretKey, clientBuilderConfiguration);
int filesUploaded = 0; // Counter to track no. of files that are uploaded
try {
FilePath workspacePath = build.getWorkspace();
if (workspacePath == null) {
listener.getLogger().println("工作空间中没有任何文件.");
return filesUploaded;
}
StringTokenizer strTokens = new StringTokenizer(expFP, fpSeparator);
FilePath[] paths = null;

listener.getLogger().println("开始上传到阿里云OSS...");
listener.getLogger().println("上传endpoint是:" + bucketName);

while (strTokens.hasMoreElements()) {
String fileName = strTokens.nextToken();
String embeddedVP = null;
if (fileName != null) {
int embVPSepIndex = fileName.indexOf("::");
if (embVPSepIndex != -1) {
if (fileName.length() > embVPSepIndex + 1) {
embeddedVP = fileName.substring(embVPSepIndex + 2, fileName.length());
if (Utils.isNullOrEmpty(embeddedVP)) {
embeddedVP = null;
}
if (embeddedVP != null && !embeddedVP.endsWith(Utils.FWD_SLASH)) {
embeddedVP = embeddedVP + Utils.FWD_SLASH;
}
}
fileName = fileName.substring(0, embVPSepIndex);
}
}

if (Utils.isNullOrEmpty(fileName)) {
return filesUploaded;
}

FilePath fp = new FilePath(workspacePath, fileName);

if (fp.exists() && !fp.isDirectory()) {
paths = new FilePath[1];
paths[0] = fp;
} else {
paths = workspacePath.list(fileName);
}

if (paths.length != 0) {
for (FilePath src : paths) {
String key = "";
if (Utils.isNullOrEmpty(expVP)
&& Utils.isNullOrEmpty(embeddedVP)) {
key = src.getName();
} else {
String prefix = expVP;
if (!Utils.isNullOrEmpty(embeddedVP)) {
if (Utils.isNullOrEmpty(expVP)) {
prefix = embeddedVP;
} else {
prefix = expVP + embeddedVP;
}
}
key = prefix + src.getName();
}
long startTime = System.currentTimeMillis();
InputStream inputStream = src.read();
try {
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(src.length());
meta.setContentType(getContentType(src));
listener.getLogger().println("File: " + src.getName() + ", Content Type: " + meta.getContentType());
client.putObject(bucketName, key, inputStream, meta);
} finally {
try {
inputStream.close();
} catch (IOException e) {
}
}
long endTime = System.currentTimeMillis();
listener.getLogger().println("Uploaded object [" + key + "] in " + getTime(endTime - startTime));
listener.getLogger().println("版本下载地址:" + "http://" + bucketName + "." + aliyunEndPointSuffix + "/" + key);
filesUploaded++;
}
} else {
listener.getLogger().println(expFP + "下未找到Artifacts,请确认\"要上传的Artifacts\"中路径配置正确或部署包已正常生成,如pom.xml里assembly插件及配置正确。");
}
}
} catch (Exception e) {
e.printStackTrace();
throw new AliyunOSSException(e.getMessage(), e.getCause());
}
return filesUploaded;
}

public static String getTime(long timeInMills) {
return DurationFormatUtils.formatDuration(timeInMills, "HH:mm:ss.S") + " (HH:mm:ss.S)";
}


// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
// support the common web file types for now
private static final String[] COMMON_CONTENT_TYPES = {
".js", "application/js",
".json", "application/json",
".svg", "image/svg+xml",
".woff", "application/x-font-woff",
".woff2", "application/x-font-woff",
".ttf", "application/x-font-ttf"
};

// http://www.rgagnon.com/javadetails/java-0487.html
// we don't use the more robust solutions (checking magic headers) here, because the file stream might be
// loaded remotely, so that would be time consuming in checking, even hangs the Jenkins build in my test.
private static String getContentType(FilePath filePath) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String fileName = filePath.getName();
String type = fileNameMap.getContentTypeFor(fileName);

if (type == null) {
for (int i = 0; i < COMMON_CONTENT_TYPES.length; i += 2) {
String extension = COMMON_CONTENT_TYPES[i];
int beginIndex = Math.max(0, fileName.length() - extension.length());
if (fileName.substring(beginIndex).equals(extension)) {
return COMMON_CONTENT_TYPES[i + 1];
}
}
type = "application/octet-stream";
}
return type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public FormValidation doCheckAccount(
return FormValidation.error("阿里云EndPointSuffix不能为空!");
}
try {
AliyunOSSClient.validateAliyunAccount(aliyunAccessKey,
AliyunOSSClient.validateAliyunAccount(aliyunEndPointSuffix,aliyunAccessKey,
aliyunSecretKey);
} catch (Exception e) {
return FormValidation.error(e.getMessage());
Expand All @@ -139,7 +139,7 @@ public FormValidation doCheckBucket(@QueryParameter String val)
}
try {
AliyunOSSClient.validateOSSBucket(aliyunAccessKey,
aliyunSecretKey, val);
aliyunSecretKey, aliyunEndPointSuffix,val);
} catch (Exception e) {
return FormValidation.error(e.getMessage());
}
Expand Down Expand Up @@ -215,7 +215,6 @@ public boolean perform(AbstractBuild build, Launcher launcher,BuildListener list
listener.getLogger().println("上传Artifacts到阿里云OSS成功,上传文件个数:" + filesUploaded);
success = true;
}

} catch (Exception e) {
this.logger.println("上传Artifact到阿里云OSS失败,错误消息如下:");
this.logger.println(e.getMessage());
Expand Down