Search in sources :

Example 16 with CloudSDKException

use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.

the class AliyunSendSmsClient method init.

public void init(AliyunSmsProfile profile) throws CloudSDKException {
    AssertUtils.notNull(profile.getAccessKeyId(), "Access key id is null.");
    AssertUtils.notNull(profile.getAccessKeySecret(), "Access key secret is null.");
    AssertUtils.notNull(profile.getEndpoint(), "Endpoint is null.");
    Config config = new Config().setAccessKeyId(profile.getAccessKeyId()).setAccessKeySecret(profile.getAccessKeySecret()).setEndpoint(profile.getEndpoint());
    try {
        this.client = new com.aliyun.dysmsapi20170525.Client(config);
    } catch (Exception e) {
        throw new CloudSDKException("Init sms client failed.", e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) Config(com.aliyun.teaopenapi.models.Config) Client(com.aliyun.dysmsapi20170525.Client) CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException)

Example 17 with CloudSDKException

use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.

the class AliyunSendSmsClient method send.

/**
 * {@inheritDoc}
 */
@Override
public void send(String phoneNumber, Map<String, String> paramMap, CloudSmsTemplate smsTemplate) throws CloudSDKException {
    if (null == smsTemplate) {
        throw new CloudSDKException("Sms template is null.");
    }
    String signName = smsTemplate.getSignName();
    String templateId = smsTemplate.getTemplateId().toString();
    SendSmsRequest request = new SendSmsRequest();
    request.setPhoneNumbers(phoneNumber);
    request.setSignName(signName);
    request.setTemplateCode(templateId);
    try {
        if (null != paramMap) {
            String templateParam = JSONUtils.toJSONString(paramMap);
            request.setTemplateParam(templateParam);
        }
        SendSmsResponse result = this.client.sendSms(request);
        SendSmsResponseBody body = result.getBody();
        if (null == body || !"ok".equalsIgnoreCase(body.getCode())) {
            throw new CloudSDKException("Send sms failed, code is " + body.code + ", message is " + body.getMessage() + ", request id is " + body.getRequestId() + ", bizId is " + body.getBizId() + '.');
        }
    } catch (Exception e) {
        throw new CloudSDKException("Send sms failed.", e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) SendSmsRequest(com.aliyun.dysmsapi20170525.models.SendSmsRequest) SendSmsResponseBody(com.aliyun.dysmsapi20170525.models.SendSmsResponseBody) SendSmsResponse(com.aliyun.dysmsapi20170525.models.SendSmsResponse) CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException)

Example 18 with CloudSDKException

use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.

the class TestAliyunObjectStorageServiceImpl method testgenPresignedUploadUrl.

@Test
public void testgenPresignedUploadUrl() {
    String objectName = "/pre/testd/test.txt";
    URL url = null;
    try {
        String filePath = TestAliyunObjectStorageServiceImpl.class.getClassLoader().getResource("test.txt").getPath();
        File localFile = new File(filePath);
        String contentMd5 = FileDigestUtils.md5AsBase64(localFile);
        long signExpired = 60 * 1000;
        url = objectStoreageService.genPresignedUploadUrl(BUCKETNAME, objectName, signExpired, contentMd5);
        System.out.println(url.toString());
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        connection.setRequestProperty("content-md5", contentMd5);
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        FileInputStream fStream = new FileInputStream(localFile);
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        int length = -1;
        while ((length = fStream.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }
        fStream.close();
        out.close();
        int responseCode = connection.getResponseCode();
        System.out.println("Service returned response code " + responseCode);
    } catch (CloudSDKException e) {
        Assert.fail(e.getMessage());
    } catch (ProtocolException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 19 with CloudSDKException

use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.

the class TestAliyunObjectStorageServiceImpl method testgetObjectMetadata.

@Test
public void testgetObjectMetadata() {
    try {
        ObjectMetadata objectMetadata = objectStoreageService.getObjectMetadata(BUCKETNAME, OBJECTNAME);
        System.out.println(objectMetadata.toString());
    } catch (CloudSDKException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) ObjectMetadata(com.mizhousoft.cloudsdk.oss.ObjectMetadata) Test(org.junit.Test)

Example 20 with CloudSDKException

use of com.mizhousoft.cloudsdk.CloudSDKException in project cloud-sdk by mizhousoft.

the class FileDigestUtils method md5AsBase64.

public static String md5AsBase64(File file) throws CloudSDKException {
    try (InputStream istream = new FileInputStream(file)) {
        byte[] binaryData = DigestUtils.md5(IOUtils.toByteArray(istream));
        String md5 = Base64.encodeBase64String(binaryData);
        return md5;
    } catch (IOException e) {
        throw new CloudSDKException("Read file failed.", e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

CloudSDKException (com.mizhousoft.cloudsdk.CloudSDKException)44 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)14 Test (org.junit.Test)13 URL (java.net.URL)8 ObjectMetadata (com.mizhousoft.cloudsdk.oss.ObjectMetadata)6 File (java.io.File)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 RecognitionResult (com.mizhousoft.cloudsdk.nlp.RecognitionResult)3 Date (java.util.Date)3 GeneratePresignedUrlRequest (com.aliyun.oss.model.GeneratePresignedUrlRequest)2 TempCredential (com.mizhousoft.cloudsdk.TempCredential)2 OSSTempCredential (com.mizhousoft.cloudsdk.oss.OSSTempCredential)2 MediaMeta (com.mizhousoft.cloudsdk.vod.MediaMeta)2 CoverTypeEnum (com.mizhousoft.tencent.vod.constants.CoverTypeEnum)2 MediaTypeEnum (com.mizhousoft.tencent.vod.constants.MediaTypeEnum)2 GeneratePresignedUrlRequest (com.qcloud.cos.model.GeneratePresignedUrlRequest)2 VinOCRRequest (com.tencentcloudapi.ocr.v20181119.models.VinOCRRequest)2 VinOCRResponse (com.tencentcloudapi.ocr.v20181119.models.VinOCRResponse)2 CoverBySnapshotTaskInput (com.tencentcloudapi.vod.v20180717.models.CoverBySnapshotTaskInput)2