use of com.qcloud.cos.model.ObjectMetadata in project alluxio by Alluxio.
the class COSUnderFileSystem method createEmptyObject.
@Override
public boolean createEmptyObject(String key) {
try {
ObjectMetadata objMeta = new ObjectMetadata();
objMeta.setContentLength(0);
mClient.putObject(mBucketNameInternal, key, new ByteArrayInputStream(new byte[0]), objMeta);
return true;
} catch (CosClientException e) {
LOG.error("Failed to create object: {}", key, e);
return false;
}
}
use of com.qcloud.cos.model.ObjectMetadata in project jeesuite-libs by vakinge.
the class QcloudProvider method getObjectMetadata.
@Override
public CObjectMetadata getObjectMetadata(String bucketName, String fileKey) {
try {
String _bucketName = buildBucketName(bucketName);
String _fileKey = resolveFileKey(bucketName, fileKey);
ObjectMetadata metadata = cosclient.getObjectMetadata(_bucketName, _fileKey);
CObjectMetadata objectMetadata = new CObjectMetadata();
objectMetadata.setCreateTime(metadata.getLastModified());
objectMetadata.setMimeType(metadata.getContentType());
objectMetadata.setFilesize(metadata.getContentLength());
objectMetadata.setHash(metadata.getContentMD5());
objectMetadata.setExpirationTime(metadata.getExpirationTime());
objectMetadata.setCustomMetadatas(metadata.getUserMetadata());
return objectMetadata;
} catch (Exception e) {
throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
}
}
use of com.qcloud.cos.model.ObjectMetadata in project accright-blog by Accright.
the class COSApi method uploadFileToCOS.
/**
* 使用文件流上传文件至COS
* @param file
* @return
*/
public String uploadFileToCOS(MultipartFile file) throws IOException {
// 获取上传文件的名称用于获取后缀
String fileName = file.getOriginalFilename();
// 获取文件的后缀
String prex = fileName.substring(fileName.lastIndexOf("."));
// 设置文件的key
String key = System.currentTimeMillis() + prex;
// 获取文件输入流
InputStream inputStream = file.getInputStream();
// 获取文件元信息
ObjectMetadata objectMetadata = new ObjectMetadata();
// 设置输入流长度为 500
objectMetadata.setContentLength(file.getSize());
// 设置 Content type, 默认是 application/octet-stream
objectMetadata.setContentType(file.getContentType());
PutObjectResult putObjectResult = cosclient.putObject(bucketName, key, inputStream, objectMetadata);
// 关闭输入流
inputStream.close();
// 设置过期时间 ---可以返回带有过期时间的URL
// Date expiration = new Date(new Date().getTime() + 30L * 60L * 1000L);
// URL urlx = cosclient.generatePresignedUrl(bucketName, key, expiration,HttpMethodName.GET);
// return urlx.toString();
// 关闭客户端
cosclient.shutdown();
// 返回URL
return BUCKET_URL + key;
}
use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.
the class CopyFileDemo method copyWithNewMetaDataDemo.
public static void copyWithNewMetaDataDemo() {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
ExecutorService threadPool = Executors.newFixedThreadPool(32);
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
TransferManager transferManager = new TransferManager(cosclient, threadPool);
// 要拷贝的bucket region, 支持跨园区拷贝
Region srcBucketRegion = new Region("ap-shanghai");
// 源bucket, bucket名需包含appid
String srcBucketName = "srcBucket-1251668577";
// 要拷贝的源文件
String srcKey = "aaa/bbb.txt";
// 目的bucket, bucket名需包含appid
String destBucketName = "destBucket-1251668577";
// 要拷贝的目的文件
String destKey = "ccc/ddd.txt";
ClientConfig srcClientConfig = new ClientConfig(srcBucketRegion);
COSClient srcCosclient = new COSClient(cred, srcClientConfig);
GetObjectRequest getReq = new GetObjectRequest(srcBucketName, srcKey);
File srcFile = new File("srcFile");
srcCosclient.getObject(getReq, srcFile);
String srcMD5 = "";
try {
srcMD5 = Md5Utils.md5Hex(srcFile);
} catch (Exception e) {
e.printStackTrace();
}
ObjectMetadata destMeta = new ObjectMetadata();
destMeta.addUserMetadata("md5", srcMD5);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
copyObjectRequest.setNewObjectMetadata(destMeta);
try {
CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
System.out.print(copyObjectResult.getRequestId());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
cosclient.shutdown();
}
use of com.qcloud.cos.model.ObjectMetadata 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());
}
Aggregations