Search in sources :

Example 66 with Region

use of com.qcloud.cos.region.Region in project cloud-sdk by mizhousoft.

the class COSObjectStorageServiceImpl method init.

public void init(COSProfile config) throws CloudSDKException {
    validate(config);
    COSCredentials cred = null;
    if (StringUtils.isBlank(config.getSessionToken())) {
        cred = new BasicCOSCredentials(config.getAccessKey(), config.getSecretKey());
    } else {
        cred = new BasicSessionCredentials(config.getAccessKey(), config.getSecretKey(), config.getSessionToken());
    }
    RegionEnum region = RegionEnum.get(config.getRegion());
    ClientConfig clientConfig = new ClientConfig(new Region(region.getValue()));
    clientConfig.setHttpProtocol(HttpProtocol.https);
    COSClient cosClient = new COSClient(cred, clientConfig);
    this.profile = config;
    this.cosClient = cosClient;
    LOG.info("Init cos client successfully.");
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials) RegionEnum(com.mizhousoft.tencent.RegionEnum) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 67 with Region

use of com.qcloud.cos.region.Region in project yili-music by programmer-yili.

the class CosStorageServiceImpl method createCOSClient.

// Todo: 改造client获取方法
COSClient createCOSClient() {
    // 这里需要已经获取到临时密钥的结果。
    // 临时密钥的生成参考 https://cloud.tencent.com/document/product/436/14048#cos-sts-sdk
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    // ClientConfig 中包含了后续请求 COS 的客户端设置:
    ClientConfig clientConfig = new ClientConfig();
    // 设置 bucket 的地域
    // COS_REGION 请参照 https://cloud.tencent.com/document/product/436/6224
    clientConfig.setRegion(new Region(region));
    // 设置请求协议, http 或者 https
    // 5.6.53 及更低的版本,建议设置使用 https 协议
    // 5.6.54 及更高版本,默认使用了 https
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 以下的设置,是可选的:
    // 设置 socket 读取超时,默认 30s
    clientConfig.setSocketTimeout(30 * 1000);
    // 设置建立连接超时,默认 30s
    clientConfig.setConnectionTimeout(30 * 1000);
    return new COSClient(cred, clientConfig);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 68 with Region

use of com.qcloud.cos.region.Region in project hadoop-cos by tencentyun.

the class CosNativeFileSystemStore method initCOSClient.

private void initCOSClient(URI uri, Configuration conf) throws IOException {
    COSCredentialProviderList cosCredentialProviderList = CosNUtils.createCosCredentialsProviderSet(uri, conf);
    ClientConfig config;
    String customerDomain = conf.get(CosNConfigKeys.CUSTOMER_DOMAIN);
    if (null == customerDomain) {
        String region = conf.get(CosNConfigKeys.COSN_REGION_KEY);
        if (null == region) {
            region = conf.get(CosNConfigKeys.COSN_REGION_PREV_KEY);
        }
        String endpointSuffix = conf.get(CosNConfigKeys.COSN_ENDPOINT_SUFFIX_KEY);
        if (null == endpointSuffix) {
            endpointSuffix = conf.get(CosNConfigKeys.COSN_ENDPOINT_SUFFIX_PREV_KEY);
        }
        if (null == region && null == endpointSuffix) {
            String exceptionMsg = String.format("config '%s' and '%s' specify at least one.", CosNConfigKeys.COSN_REGION_KEY, CosNConfigKeys.COSN_ENDPOINT_SUFFIX_KEY);
            throw new IOException(exceptionMsg);
        }
        if (null == region) {
            config = new ClientConfig(new Region(""));
            config.setEndPointSuffix(endpointSuffix);
        } else {
            config = new ClientConfig(new Region(region));
        }
        this.useL5Id = conf.getBoolean(CosNConfigKeys.COSN_USE_L5_ENABLE, CosNConfigKeys.DEFAULT_COSN_USE_L5_ENABLE);
        this.l5UpdateMaxRetryTimes = conf.getInt(CosNConfigKeys.COSN_L5_UPDATE_MAX_RETRIES_KEY, CosNConfigKeys.DEFAULT_COSN_L5_UPDATE_MAX_RETRIES);
        if (useL5Id) {
            String l5Id = conf.get(CosNConfigKeys.COSN_L5_KEY);
            if (null != l5Id) {
                int l5modId = Integer.parseInt(l5Id.split(",")[0]);
                int l5cmdId = Integer.parseInt(l5Id.split(",")[1]);
                Class<?> l5EndpointResolverClass;
                try {
                    l5EndpointResolverClass = Class.forName("org.apache.hadoop.fs.cosn.TencentCloudL5EndpointResolverImpl");
                    this.l5EndpointResolver = (TencentCloudL5EndpointResolver) l5EndpointResolverClass.newInstance();
                    this.l5EndpointResolver.setModId(l5modId);
                    this.l5EndpointResolver.setCmdId(l5cmdId);
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                    throw new IOException("The current version does not support L5 resolver.", e);
                }
                config.setEndpointResolver(l5EndpointResolver);
            }
        }
    } else {
        config = new ClientConfig(new Region(""));
        LOG.info("Use Customer Domain is {}", customerDomain);
        CustomerDomainEndpointResolver customerDomainEndpointResolver = new CustomerDomainEndpointResolver();
        customerDomainEndpointResolver.setEndpoint(customerDomain);
        config.setEndpointBuilder(customerDomainEndpointResolver);
    }
    boolean useHttps = conf.getBoolean(CosNConfigKeys.COSN_USE_HTTPS_KEY, CosNConfigKeys.DEFAULT_USE_HTTPS);
    if (useHttps) {
        config.setHttpProtocol(HttpProtocol.https);
    } else {
        // 这里 config 的默认值改过了,默认值变成了https了。
        config.setHttpProtocol(HttpProtocol.http);
    }
    int socketTimeoutSec = conf.getInt(CosNConfigKeys.COSN_CLIENT_SOCKET_TIMEOUTSEC, CosNConfigKeys.DEFAULT_CLIENT_SOCKET_TIMEOUTSEC);
    config.setSocketTimeout(socketTimeoutSec * 1000);
    this.crc32cEnabled = conf.getBoolean(CosNConfigKeys.CRC32C_CHECKSUM_ENABLED, CosNConfigKeys.DEFAULT_CRC32C_CHECKSUM_ENABLED);
    this.completeMPUCheckEnabled = conf.getBoolean(CosNConfigKeys.COSN_COMPLETE_MPU_CHECK, CosNConfigKeys.DEFAULT_COSN_COMPLETE_MPU_CHECK_ENABLE);
    // Proxy settings
    String httpProxyIp = conf.getTrimmed(CosNConfigKeys.HTTP_PROXY_IP);
    int httpProxyPort = conf.getInt(CosNConfigKeys.HTTP_PROXY_PORT, CosNConfigKeys.DEFAULT_HTTP_PROXY_PORT);
    if (null != httpProxyIp && !httpProxyIp.isEmpty()) {
        config.setHttpProxyIp(httpProxyIp);
        if (httpProxyPort >= 0) {
            config.setHttpProxyPort(httpProxyPort);
        } else {
            if (useHttps) {
                LOG.warn("Proxy IP set without port. Using HTTPS default port 443");
                config.setHttpProxyPort(443);
            } else {
                LOG.warn("Proxy IP set without port, Using HTTP default port 80");
                config.setHttpProxyPort(80);
            }
        }
        // setting the proxy username and password
        String proxyUserName = conf.get(CosNConfigKeys.HTTP_PROXY_USERNAME);
        String proxyPassword = conf.get(CosNConfigKeys.HTTP_PROXY_PASSWORD);
        if ((null == proxyUserName || proxyUserName.isEmpty()) != (null == proxyPassword || proxyPassword.isEmpty())) {
            String exceptionMessage = String.format("Proxy error: '%s' or '%s' set without the other.", CosNConfigKeys.HTTP_PROXY_USERNAME, CosNConfigKeys.HTTP_PROXY_PASSWORD);
            throw new IllegalArgumentException(exceptionMessage);
        }
        config.setProxyUsername(proxyUserName);
        config.setProxyPassword(proxyPassword);
    }
    String versionNum = getPluginVersionInfo();
    String versionInfo = versionNum.equals("unknown") ? CosNConfigKeys.DEFAULT_USER_AGENT : "cos-hadoop-plugin-v" + versionNum;
    String userAgent = conf.get(CosNConfigKeys.USER_AGENT, versionInfo);
    String emrVersion = conf.get(CosNConfigKeys.TENCENT_EMR_VERSION_KEY);
    if (null != emrVersion && !emrVersion.isEmpty()) {
        userAgent = String.format("%s on %s", userAgent, emrVersion);
    }
    config.setUserAgent(userAgent);
    this.maxRetryTimes = conf.getInt(CosNConfigKeys.COSN_MAX_RETRIES_KEY, CosNConfigKeys.DEFAULT_MAX_RETRIES);
    // 设置COSClient的最大重试次数
    int clientMaxRetryTimes = conf.getInt(CosNConfigKeys.CLIENT_MAX_RETRIES_KEY, CosNConfigKeys.DEFAULT_CLIENT_MAX_RETRIES);
    config.setMaxErrorRetry(clientMaxRetryTimes);
    LOG.info("hadoop cos retry times: {}, cos client retry times: {}", this.maxRetryTimes, clientMaxRetryTimes);
    // 设置连接池的最大连接数目
    config.setMaxConnectionsCount(conf.getInt(CosNConfigKeys.MAX_CONNECTION_NUM, CosNConfigKeys.DEFAULT_MAX_CONNECTION_NUM));
    // 设置是否进行服务器端加密
    String serverSideEncryptionAlgorithm = conf.get(CosNConfigKeys.COSN_SERVER_SIDE_ENCRYPTION_ALGORITHM, "");
    CosNEncryptionMethods cosSSE = CosNEncryptionMethods.getMethod(serverSideEncryptionAlgorithm);
    String sseKey = conf.get(CosNConfigKeys.COSN_SERVER_SIDE_ENCRYPTION_KEY, "");
    String sseContext = conf.get(CosNConfigKeys.COSN_SERVER_SIDE_ENCRYPTION_CONTEXT, "");
    checkEncryptionMethod(config, cosSSE, sseKey);
    this.encryptionSecrets = new CosNEncryptionSecrets(cosSSE, sseKey, sseContext);
    // Set the traffic limit
    this.trafficLimit = conf.getInt(CosNConfigKeys.TRAFFIC_LIMIT, CosNConfigKeys.DEFAULT_TRAFFIC_LIMIT);
    if (this.trafficLimit >= 0 && (this.trafficLimit < 100 * 1024 * 8 || this.trafficLimit > 100 * 1024 * 1024 * 8)) {
        String exceptionMessage = String.format("The '%s' needs to be between %d and %d. but %d.", CosNConfigKeys.TRAFFIC_LIMIT, 100 * 1024 * 8, 100 * 1024 * 1024 * 8, this.trafficLimit);
        throw new IllegalArgumentException(exceptionMessage);
    }
    this.cosClient = new COSClient(cosCredentialProviderList, config);
}
Also used : IOException(java.io.IOException) CustomerDomainEndpointResolver(org.apache.hadoop.fs.cosn.CustomerDomainEndpointResolver) COSClient(com.qcloud.cos.COSClient) COSCredentialProviderList(org.apache.hadoop.fs.auth.COSCredentialProviderList) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 69 with Region

use of com.qcloud.cos.region.Region in project halo by halo-dev.

the class TencentCosFileHandler method delete.

@Override
public void delete(String key) {
    Assert.notNull(key, "File key must not be blank");
    // Get config
    String region = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_REGION).toString();
    String secretId = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_ID).toString();
    String secretKey = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_SECRET_KEY).toString();
    String bucketName = optionService.getByPropertyOfNonNull(TencentCosProperties.COS_BUCKET_NAME).toString();
    COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
    Region regionConfig = new Region(region);
    ClientConfig clientConfig = new ClientConfig(regionConfig);
    // Init OSS client
    COSClient cosClient = new COSClient(cred, clientConfig);
    try {
        cosClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        throw new FileOperationException("附件 " + key + " 从腾讯云删除失败", e);
    } finally {
        cosClient.shutdown();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) FileOperationException(run.halo.app.exception.FileOperationException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) FileOperationException(run.halo.app.exception.FileOperationException)

Example 70 with Region

use of com.qcloud.cos.region.Region in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest method testTransferManagerCopySmallFileFromDiffRegion.

// transfer manager对不同园区5G以下文件进行使用put object copy
@Ignore
public void testTransferManagerCopySmallFileFromDiffRegion() throws CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
    String srcRegion = "ap-guangzhou";
    ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
    COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
    String srcBucketName = "chengwus3gz-1251668577";
    String srcKey = "ut_copy/len1G.txt";
    String destKey = "ut_copy_dest/len1G.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(new Region(srcRegion), srcBucketName, srcKey, bucket, destKey);
    Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
    CopyResult copyResult = copy.waitForCopyResult();
    assertNotNull(copyResult.getRequestId());
    assertNotNull(copyResult.getDateStr());
    clearObject(destKey);
}
Also used : CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Copy(com.qcloud.cos.transfer.Copy) Region(com.qcloud.cos.region.Region) CopyResult(com.qcloud.cos.model.CopyResult) Ignore(org.junit.Ignore)

Aggregations

Region (com.qcloud.cos.region.Region)121 COSCredentials (com.qcloud.cos.auth.COSCredentials)105 ClientConfig (com.qcloud.cos.ClientConfig)103 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)101 COSClient (com.qcloud.cos.COSClient)99 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)23 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)13 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)8 Copy (com.qcloud.cos.transfer.Copy)8 Test (org.junit.Test)8 CopyResult (com.qcloud.cos.model.CopyResult)7 LinkedList (java.util.LinkedList)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)6