use of com.qcloud.cos.model.ObjectMetadata in project alluxio by Alluxio.
the class COSOutputStream method close.
/**
* Closes this output stream. When an output stream is closed, the local temporary file is
* uploaded to COS Service. Once the file is uploaded, the temporary file is deleted.
*/
@Override
public void close() throws IOException {
if (mClosed.getAndSet(true)) {
return;
}
mLocalOutputStream.close();
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile))) {
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(mFile.length());
if (mHash != null) {
byte[] hashBytes = mHash.digest();
meta.setContentMD5(new String(Base64.encodeBase64(hashBytes)));
}
mCosClient.putObject(mBucketName, mKey, in, meta);
} catch (CosClientException e) {
LOG.error("Failed to upload {}. ", mKey);
throw new IOException(e);
} finally {
// upload or if the upload failed.
if (!mFile.delete()) {
LOG.error("Failed to delete temporary file @ {}", mFile.getPath());
}
}
return;
}
use of com.qcloud.cos.model.ObjectMetadata in project blog by X1192176811.
the class CosUploadStrategyImpl method upload.
@Override
public void upload(String path, String fileName, InputStream inputStream) {
// 生成cos客户端
COSClient cosclient = getCosClient();
try {
// 从输入流上传(需提前告知输入流的长度, 否则可能导致 oom)
ObjectMetadata objectMetadata = new ObjectMetadata();
// 设置输入流长度
objectMetadata.setContentLength(inputStream.available());
cosclient.putObject(cosConfigProperties.getBucketName(), path + fileName, inputStream, objectMetadata);
} catch (IOException e) {
log.error(e.getMessage());
}
// 关闭客户端
cosclient.shutdown();
}
Aggregations