Search in sources :

Example 1 with CUploadResult

use of com.mendmix.cos.CUploadResult in project jeesuite-libs by vakinge.

the class AwsProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    try {
        String bucketName = object.getBucketName();
        if (StringUtils.isEmpty(bucketName)) {
            throw new JeesuiteBaseException("BucketName 不能为空");
        }
        String fileKey = object.getFileKey();
        PutObjectResponse putObjectResponse = null;
        long size = 0;
        if (object.getFile() != null) {
            size = object.getFile().length();
            PutObjectRequest putRequest = PutObjectRequest.builder().bucket(bucketName).key(fileKey).contentType(object.getMimeType()).build();
            putObjectResponse = s3Client.putObject(putRequest, object.getFile().toPath());
        } else if (object.getInputStream() != null) {
            size = object.getInputStream().available();
            PutObjectRequest putRequest = PutObjectRequest.builder().bucket(bucketName).key(fileKey).contentType(object.getMimeType()).build();
            putObjectResponse = s3Client.putObject(putRequest, RequestBody.fromInputStream(object.getInputStream(), object.getInputStream().available()));
        } else if (object.getBytes() != null) {
            size = object.getBytes().length;
            PutObjectRequest putRequest = PutObjectRequest.builder().bucket(bucketName).key(fileKey).contentType(object.getMimeType()).build();
            putObjectResponse = s3Client.putObject(putRequest, RequestBody.fromBytes(object.getBytes()));
        }
        if (putObjectResponse != null) {
            CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(bucketName, fileKey, 300), null);
            uploadResult.setMimeType(object.getMimeType());
            uploadResult.setFileSize(size);
            return uploadResult;
        }
    } catch (JeesuiteBaseException e) {
        throw e;
    } catch (Exception e) {
        LOGGER.warn("上传失败, e={}", ExceptionUtils.getMessage(e), e);
        throw new JeesuiteBaseException(e.getMessage());
    }
    return null;
}
Also used : CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) PutObjectResponse(software.amazon.awssdk.services.s3.model.PutObjectResponse) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 2 with CUploadResult

use of com.mendmix.cos.CUploadResult in project jeesuite-libs by vakinge.

the class MinioProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    try {
        String bucketName = object.getBucketName();
        if (StringUtils.isEmpty(bucketName)) {
            throw new JeesuiteBaseException("BucketName 不能为空");
        }
        String fileKey = object.getFileKey();
        InputStream inputStream = object.getInputStream();
        byte[] objectBytes = object.getBytes();
        ObjectWriteResponse objectWriteResponse = null;
        long size = 0;
        if (object.getFile() != null) {
            objectWriteResponse = minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).filename(object.getFile().getAbsolutePath()).object(fileKey).contentType(object.getMimeType()).build());
            size = object.getFile().length();
        } else if (objectBytes != null) {
            byte[] bytes = objectBytes;
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileKey).contentType(object.getMimeType()).stream(bis, bytes.length, -1).build());
            size = bytes.length;
            bis.close();
        } else if (inputStream != null) {
            objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileKey).contentType(object.getMimeType()).stream(inputStream, inputStream.available(), -1).build());
            size = inputStream.available();
        } else {
            throw new JeesuiteBaseException("upload object is NULL");
        }
        if (objectWriteResponse != null) {
            CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(object.getBucketName(), fileKey, 300), null);
            uploadResult.setMimeType(object.getMimeType());
            uploadResult.setFileSize(size);
            return uploadResult;
        }
    } catch (JeesuiteBaseException jbex) {
        throw jbex;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : ObjectWriteResponse(io.minio.ObjectWriteResponse) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 3 with CUploadResult

use of com.mendmix.cos.CUploadResult in project jeesuite-libs by vakinge.

the class QcloudProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    PutObjectRequest request;
    String fileKey = object.getFileKey();
    String bucketName = buildBucketName(object.getBucketName());
    if (object.getFile() != null) {
        request = new PutObjectRequest(bucketName, fileKey, object.getFile());
    } else if (object.getBytes() != null) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(object.getBytes());
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, inputStream, objectMetadata);
    } else if (object.getInputStream() != null) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, object.getInputStream(), objectMetadata);
    } else {
        throw new IllegalArgumentException("upload object is NULL");
    }
    try {
        if (object.getFileSize() > conf.getMaxAllowdSingleFileSize()) {
            Upload upload = transferManager.upload(request);
            com.qcloud.cos.model.UploadResult result = upload.waitForUploadResult();
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getCrc64Ecma());
        } else {
            PutObjectResult result = cosclient.putObject(request);
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getContentMd5());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) Upload(com.qcloud.cos.transfer.Upload) CosServiceException(com.qcloud.cos.exception.CosServiceException) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) IOException(java.io.IOException) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ByteArrayInputStream(java.io.ByteArrayInputStream) CObjectMetadata(com.mendmix.cos.CObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 4 with CUploadResult

use of com.mendmix.cos.CUploadResult in project jeesuite-libs by vakinge.

the class AliyunProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    try {
        PutObjectRequest request;
        String fileKey = object.getFileKey();
        String bucketName = buildBucketName(object.getBucketName());
        if (object.getFile() != null) {
            request = new PutObjectRequest(bucketName, fileKey, object.getFile());
        } else if (object.getBytes() != null) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(object.getBytes());
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(object.getFileSize());
            request = new PutObjectRequest(bucketName, fileKey, inputStream, objectMetadata);
        } else if (object.getInputStream() != null) {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(object.getFileSize());
            request = new PutObjectRequest(bucketName, fileKey, object.getInputStream(), objectMetadata);
        } else {
            throw new IllegalArgumentException("upload object is NULL");
        }
        PutObjectResult result = ossClient.putObject(request);
        if (result.getResponse() == null || result.getResponse().isSuccessful()) {
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getServerCRC().toString());
        } else {
            throw new RuntimeException(result.getResponse().getErrorResponseAsString());
        }
    } catch (OSSException e) {
        throw new RuntimeException(e.getErrorMessage());
    }
}
Also used : CUploadResult(com.mendmix.cos.CUploadResult) ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectResult(com.aliyun.oss.model.PutObjectResult) OSSException(com.aliyun.oss.OSSException) CObjectMetadata(com.mendmix.cos.CObjectMetadata) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 5 with CUploadResult

use of com.mendmix.cos.CUploadResult in project jeesuite-libs by vakinge.

the class HuaweicloudProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    String bucketName = object.getBucketName();
    if (StringUtils.isBlank(bucketName)) {
        throw new JeesuiteBaseException("BucketName 不能为空");
    }
    InputStream inputStream = object.getInputStream();
    File file = object.getFile();
    String fileKey = object.getFileKey();
    byte[] bytes = object.getBytes();
    long size = 0;
    logger.info("bucknetName={}, fileKey={}", bucketName, fileKey);
    PutObjectResult putObjectResult = null;
    try {
        if (file != null) {
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(object.getMimeType());
            putObjectResult = obsClient.putObject(bucketName, fileKey, file, metadata);
            size = file.length();
        } else if (bytes != null) {
            ByteArrayInputStream input = new ByteArrayInputStream(bytes);
            putObjectResult = obsClient.putObject(bucketName, fileKey, input);
            size = bytes.length;
            input.close();
        } else if (inputStream != null) {
            putObjectResult = obsClient.putObject(bucketName, fileKey, inputStream);
            size = inputStream.available();
        } else {
            throw new JeesuiteBaseException("upload object is NULL");
        }
        if (putObjectResult != null) {
            AccessControlList acl = new AccessControlList();
            if (!isBucketPrivate(bucketName)) {
                acl = AccessControlList.REST_CANNED_PUBLIC_READ;
            }
            obsClient.setObjectAcl(bucketName, fileKey, acl);
            CUploadResult uploadResult = new CUploadResult(fileKey, getDownloadUrl(object.getBucketName(), fileKey, 300), null);
            uploadResult.setMimeType(object.getMimeType());
            uploadResult.setFileSize(size);
            return uploadResult;
        }
    } catch (Exception e) {
        logger.error("上传文件出错, bucketName={}, fileKey={}, e={}", bucketName, fileKey, ExceptionUtils.getMessage(e));
        throw new JeesuiteBaseException(e.getMessage());
    }
    return null;
}
Also used : AccessControlList(com.obs.services.model.AccessControlList) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) PutObjectResult(com.obs.services.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) File(java.io.File) CObjectMetadata(com.mendmix.cos.CObjectMetadata) ObjectMetadata(com.obs.services.model.ObjectMetadata) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Aggregations

CUploadResult (com.mendmix.cos.CUploadResult)6 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 CObjectMetadata (com.mendmix.cos.CObjectMetadata)3 InputStream (java.io.InputStream)2 OSSException (com.aliyun.oss.OSSException)1 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)1 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)1 PutObjectResult (com.aliyun.oss.model.PutObjectResult)1 AccessControlList (com.obs.services.model.AccessControlList)1 ObjectMetadata (com.obs.services.model.ObjectMetadata)1 PutObjectResult (com.obs.services.model.PutObjectResult)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 PutObjectResult (com.qcloud.cos.model.PutObjectResult)1 Upload (com.qcloud.cos.transfer.Upload)1 QiniuException (com.qiniu.common.QiniuException)1 Response (com.qiniu.http.Response)1 ObjectWriteResponse (io.minio.ObjectWriteResponse)1