use of com.qcloud.cos.COSClient 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.");
}
use of com.qcloud.cos.COSClient 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);
}
use of com.qcloud.cos.COSClient 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);
}
use of com.qcloud.cos.COSClient 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();
}
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class GetObjectURLDemo method getObjectUrlWithVersionId.
public static void getObjectUrlWithVersionId() {
// getObjectUrl 不需要验证身份信息
COSCredentials cred = new AnonymousCOSCredentials();
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 设置生成的 url 的协议名
clientConfig.setHttpProtocol(HttpProtocol.https);
// 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
String key = "test/my_test中文.json";
String bucketName = "mybucket-1251668577";
String versionId = "xxxyyyzzz111222333";
System.out.println(cosclient.getObjectUrl(bucketName, key, versionId));
}
Aggregations