Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
kkkkkkkkkkkkkkeee committed Feb 28, 2020
1 parent cab953c commit 4ac0b3b
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.ruiyu.tencentcos.tencentcos;

import com.tencent.qcloud.core.auth.BasicLifecycleCredentialProvider;
import com.tencent.qcloud.core.auth.QCloudLifecycleCredentials;
import com.tencent.qcloud.core.auth.SessionQCloudCredentials;
import com.tencent.qcloud.core.common.QCloudClientException;

/**
* 方法二:使用临时密钥进行签名(推荐使用这种方法),此处假设已获取了临时密钥 tempSecretKey, tempSecrekId,
* sessionToken, expiredTime.
*/
class LocalSessionCredentialProvider extends BasicLifecycleCredentialProvider {
private String tempSecretId;
private String tempSecretKey;
private String sessionToken;
private long expiredTime;

public LocalSessionCredentialProvider(String tempSecretId, String tempSecretKey, String sessionToken, long expiredTime) {
this.tempSecretId = tempSecretId;
this.tempSecretKey = tempSecretKey;
this.sessionToken = sessionToken;
this.expiredTime = expiredTime;
}

/**
* 返回 SessionQCloudCredential
*/
@Override
protected QCloudLifecycleCredentials fetchNewCredentials() throws QCloudClientException {
return new SessionQCloudCredentials(tempSecretId, tempSecretKey, sessionToken, expiredTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.ruiyu.tencentcos.tencentcos;
//
//val config = TreeMap<String, Any>()
//// 您的 SecretID
// config["SecretId"] = CosConfig.SecretId
//// 您的 SecretKey
// config["SecretKey"] = CosConfig.SecretKey
//// 临时密钥有效时长,单位是秒,如果没有设置,默认是30分钟
// config["durationInSeconds"] = 1800
// val data = StorageSts.getCredential(config).getJSONObject("data")

//-----------------------


//
//import com.qcloud.Module.Sts;
//import com.qcloud.QcloudApiModuleCenter;
//import com.qcloud.Utilities.Json.JSONObject;
//
//import java.util.TreeMap;
//
//public class StorageSts {
//
// private static final String POLICY = "{\"statement\": [{\"action\": [\"name/cos:*\"],\"effect\": \"allow\",\"resource\":\"*\"}],\"version\": \"2.0\"}";
// private static final int DEFAULT_DURATION_IN_SECONDS = 1800;
//
// public static JSONObject getCredential(TreeMap<String, Object> config) {
// config.put("RequestMethod", "POST");
//
// QcloudApiModuleCenter module = new QcloudApiModuleCenter(new Sts(),
// config);
//
// TreeMap<String, Object> params = new TreeMap<String, Object>();
//
// params.put("name", "tac-storage-sts-java");
// String policy = config.get("policy") == null ? POLICY : (String) config.get("policy");
// params.put("policy", policy);
// int durationInSeconds = config.get("durationInSeconds") == null ? DEFAULT_DURATION_IN_SECONDS :
// (Integer) config.get("durationInSeconds");
// params.put("durationSeconds", durationInSeconds);
//
// try {
// /* call 方法正式向指定的接口名发送请求,并把请求参数 params 传入,返回即是接口的请求结果。 */
// String result = module.call("GetFederationToken", params);
// return new JSONObject(result);
// } catch (Exception e) {
// System.out.println("error..." + e.getMessage());
// e.printStackTrace();
// }
//
// return null;
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.ruiyu.tencentcos.tencentcos;

import android.app.Activity;
import android.util.Log;

import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlProgressListener;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.transfer.COSXMLUploadTask;
import com.tencent.cos.xml.transfer.TransferConfig;
import com.tencent.cos.xml.transfer.TransferManager;

import java.util.HashMap;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
* TencentCosPlugin
*/
public class TencentCosPlugin implements MethodCallHandler {
Registrar registrar;
MethodChannel channel;

public TencentCosPlugin(Registrar registrar, MethodChannel channel) {
this.registrar = registrar;
this.channel = channel;
}

/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "tencent_cos");
channel.setMethodCallHandler(new TencentCosPlugin(registrar, channel));
}

@Override
public void onMethodCall(MethodCall call, final Result result) {
if (call.method.equals("TencentCos.uploadFile")) {
LocalSessionCredentialProvider localCredentialProvider = new LocalSessionCredentialProvider(call.<String>argument("secretId"),
call.<String>argument("secretKey"), call.<String>argument("sessionToken"),
Long.parseLong(call.argument("expiredTime").toString())
);
String region = call.argument("region");
String appid = call.argument("appid");
String bucket = call.argument("bucket");
String cosPath = call.argument("cosPath");
final String localPath = call.argument("localPath");
TransferConfig transferConfig = new TransferConfig.Builder().build();
CosXmlServiceConfig.Builder builder = new CosXmlServiceConfig.Builder().setAppidAndRegion(appid, region).setDebuggable(false).isHttps(true);
//创建 CosXmlServiceConfig 对象,根据需要修改默认的配置参数
CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig(builder);
CosXmlService cosXmlService = new CosXmlService(registrar.context(), serviceConfig, localCredentialProvider);
//初始化 TransferManager
TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);

//上传文件
COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath, localPath, null);

final HashMap<String, Object> data = new HashMap<>();
data.put("localPath", localPath);
data.put("cosPath", cosPath);
cosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {

@Override
public void onProgress(long complete, long target) {
((Activity) registrar.activeContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
channel.invokeMethod("onProgress", data);
}
});

Log.e("TencentCosPlugin", "onProgress =${progress * 100.0 / max}%");
}
});
//设置返回结果回调
cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {
@Override
public void onSuccess(CosXmlRequest request, CosXmlResult httPesult) {
Log.d("TencentCosPlugin", "Success: " + httPesult.printResult());
((Activity) registrar.activeContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
result.success(data);
}
});
}

@Override
public void onFail(CosXmlRequest request, CosXmlClientException exception, CosXmlServiceException serviceException) {
Log.d("TencentCosPlugin", "Failed: " + (exception.toString() + serviceException.toString()));
data.put("message", (exception.toString() + serviceException.toString()));
((Activity) registrar.activeContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
result.error("失败了", "失败了", "失败了");
}
});

}
});
} else {
result.notImplemented();
}
}

}


0 comments on commit 4ac0b3b

Please sign in to comment.