Files
MCCustomSkinLoader/BuildScripts/cos.gradle
T
2018-11-24 01:38:49 +08:00

57 lines
1.7 KiB
Groovy

/**
* Gradle Script for COS
* It can be used to upload or download file from Tencent Cloud COS.
* COS: https://cloud.tencent.com/document/product/436
*/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.qcloud:cos_api:5.4.6'
}
}
import com.qcloud.cos.COSClient
import com.qcloud.cos.ClientConfig
import com.qcloud.cos.auth.BasicCOSCredentials
import com.qcloud.cos.auth.COSCredentials
import com.qcloud.cos.model.PutObjectRequest
import com.qcloud.cos.region.Region
String getKey(String filename) {
String name = filename.substring(0, filename.lastIndexOf('.'))
if (filename.endsWith(".json"))
return String.format(
"versions/%s/%s",
name,
filename
)
if (filename.endsWith(".jar"))
return String.format(
"libraries/customskinloader/%s/%s/%s",
name.substring(0, name.indexOf('-')),
name.substring(name.indexOf('-') + 1),
filename
)
return filename
}
void uploadFileToCOS(String key, File file) {
COSCredentials cred = new BasicCOSCredentials(System.getenv("COS_SECRET_ID"), System.getenv("COS_SECRET_KEY"))
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"))
COSClient cosClient = new COSClient(cred, clientConfig)
String bucketName = System.getenv("COS_BUCKET")
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file)
cosClient.putObject(putObjectRequest)
println("Uploaded to COS: " + key)
}
void uploadFileToCOS(File file) {
uploadFileToCOS(getKey(file.getName()), file)
}
ext {
uploadFileToCOS = this.&uploadFileToCOS
}