Search in sources :

Example 76 with ObjectMetadata

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) FileInputStream(java.io.FileInputStream)

Example 77 with ObjectMetadata

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();
}
Also used : COSClient(com.qcloud.cos.COSClient) IOException(java.io.IOException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)77 CosClientException (com.qcloud.cos.exception.CosClientException)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)23 IOException (java.io.IOException)22 File (java.io.File)21 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)17 COSClient (com.qcloud.cos.COSClient)15 ClientConfig (com.qcloud.cos.ClientConfig)14 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 Region (com.qcloud.cos.region.Region)14 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Test (org.junit.Test)11 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)8 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 COSObject (com.qcloud.cos.model.COSObject)6