use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class GeneratePresignedUrlDemo method GeneratePresignedUploadUrl.
// 生成预签名的上传连接
public static void GeneratePresignedUploadUrl() {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "********************************");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
String key = "aaa.txt";
Date expirationTime = new Date(System.currentTimeMillis() + 30 * 60 * 1000);
// 填写本次请求的 header。Host 头部会自动补全,只需填入其他头部
Map<String, String> headers = new HashMap<String, String>();
// 填写本次请求的 params。
Map<String, String> params = new HashMap<String, String>();
URL url = cosclient.generatePresignedUrl(bucketName, key, expirationTime, HttpMethodName.PUT, headers, params);
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
// 写入要上传的数据
out.write("This text uploaded as object.");
out.close();
int responseCode = connection.getResponseCode();
System.out.println("Service returned response code " + responseCode);
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
cosclient.shutdown();
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class GetObjectDemo method GetObjectToFileDemo.
public static void GetObjectToFileDemo() {
// 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
String key = "test/my_test.json";
String bucketName = "mybucket-1251668577";
boolean useTrafficLimit = false;
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
if (useTrafficLimit) {
getObjectRequest.setTrafficLimit(8 * 1024 * 1024);
}
File localFile = new File("my_test.json");
ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localFile);
System.out.println(objectMetadata.getContentLength());
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class GetObjectURLDemo method getObjectUrlWithEndpoint.
public static void getObjectUrlWithEndpoint() {
// 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);
// 设置自定义的域名
UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("test.endpoint.com", "service.cos.myqcloud.com");
clientConfig.setEndpointBuilder(endpointBuilder);
// 生成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));
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class RestoreObjectDemo method restoreObjectDemo.
public static void restoreObjectDemo() {
// 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
String key = "test/my_data.txt";
String bucketName = "mybucket-1251668577";
// 上传一个类型为归档的文件
File localFile = new File("test/my_data.txt");
ObjectMetadata metadata = new ObjectMetadata();
metadata.setHeader("x-cos-storage-class", "Archive");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.setMetadata(metadata);
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// 设置restore得到的临时副本过期天数为1天
RestoreObjectRequest restoreObjectRequest = new RestoreObjectRequest(bucketName, key, 1);
// 设置恢复模式为Standard,其他的可选模式包括Expedited和Bulk,三种恢复模式在费用和速度上不一样
CASJobParameters casJobParameters = new CASJobParameters();
casJobParameters.setTier(Tier.Standard);
restoreObjectRequest.setCASJobParameters(casJobParameters);
cosclient.restoreObject(restoreObjectRequest);
}
use of com.qcloud.cos.COSClient in project cos-java-sdk-v5 by tencentyun.
the class SSECustomerDemo method SSECustomerDownload.
public static void SSECustomerDownload() {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("COS_SECRETID", "COS_SECRETKEY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
clientConfig.setHttpProtocol(HttpProtocol.https);
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "examplebucket-1250000000";
String key = "aaa/bbb.txt";
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
SSECustomerKey sseCustomerKey = new SSECustomerKey("MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=");
getObjectRequest.setSSECustomerKey(sseCustomerKey);
try {
COSObject cosObject = cosclient.getObject(getObjectRequest);
COSObjectInputStream cosObjectInputStream = cosObject.getObjectContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(cosObjectInputStream));
System.out.println(bufferedReader.readLine());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 关闭客户端
cosclient.shutdown();
}
Aggregations