Search in sources :

Example 1 with VirtualFile

use of com.zhouzifei.tool.dto.VirtualFile in project simpleFS by shengdingbox.

the class AwsS3ApiClient method multipartUpload.

@Override
public VirtualFile multipartUpload(InputStream inputStream, MetaDataRequest metaDataRequest) {
    final Date startDate = new Date();
    this.check();
    final String md5 = metaDataRequest.getFileMd5();
    final String name = metaDataRequest.getName();
    final Integer chunkSize = metaDataRequest.getChunkSize();
    final Integer chunk = metaDataRequest.getChunk();
    synchronized (LOCK) {
        final String storageType = StorageTypeConst.ALIYUN.getStorageType();
        final Object o = super.cacheEngine.get(storageType, md5);
        if (Objects.isNull(o)) {
            InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, name);
            InitiateMultipartUploadResult initiateMultipartUploadResult = amazonS3.initiateMultipartUpload(request);
            cacheEngine.add(storageType, md5, initiateMultipartUploadResult.getUploadId());
            cacheEngine.add(storageType, md5 + SLASH + name, name);
            cacheEngine.add(storageType, md5 + SLASH + TAG, new CopyOnWriteArrayList<>());
        }
    }
    UploadPartRequest uploadPartRequest = new UploadPartRequest();
    uploadPartRequest.setBucketName(bucketName);
    uploadPartRequest.setKey(String.valueOf(cacheEngine.get(storageType, md5 + SLASH + name)));
    uploadPartRequest.setUploadId(String.valueOf(cacheEngine.get(storageType, md5)));
    uploadPartRequest.setInputStream(inputStream);
    // 设置分片大小。除了最后一个分片没有大小限制,其他的分片最小为100 KB。
    uploadPartRequest.setPartSize(chunkSize);
    // 设置分片号。每一个上传的分片都有一个分片号,取值范围是1~10000,如果超出此范围,OSS将返回InvalidArgument错误码。
    uploadPartRequest.setPartNumber(chunk + ONE_INT);
    // 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
    UploadPartResult uploadPartResult = amazonS3.uploadPart(uploadPartRequest);
    // 每次上传分片之后,OSS的返回结果包含PartETag。PartETag将被保存在partETags中。
    ((CopyOnWriteArrayList) cacheEngine.get(storageType, md5 + SLASH + TAG)).add(uploadPartResult.getPartETag());
    // 关闭OSSClient。
    amazonS3.shutdown();
    final VirtualFile virtualFile = VirtualFile.builder().originalFileName(this.newFileName).suffix(this.suffix).uploadStartTime(startDate).uploadEndTime(new Date()).filePath(this.newFileName).fileHash(null).fullFilePath(this.newFileUrl + this.newFileName).build();
    progressListener.end(virtualFile);
    return virtualFile;
}
Also used : VirtualFile(com.zhouzifei.tool.dto.VirtualFile) Date(java.util.Date) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with VirtualFile

use of com.zhouzifei.tool.dto.VirtualFile in project simpleFS by shengdingbox.

the class AliyunOssApiClient method multipartUpload.

@Override
public VirtualFile multipartUpload(InputStream inputStream, MetaDataRequest metaDataRequest) {
    final Date startDate = new Date();
    this.check();
    final String md5 = metaDataRequest.getFileMd5();
    final String name = metaDataRequest.getName();
    final Integer chunkSize = metaDataRequest.getChunkSize();
    final Integer chunk = metaDataRequest.getChunk();
    synchronized (LOCK) {
        final String storageType = StorageTypeConst.ALIYUN.getStorageType();
        final Object o = super.cacheEngine.get(storageType, md5);
        if (Objects.isNull(o)) {
            InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, name);
            InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(request);
            cacheEngine.add(storageType, md5, initiateMultipartUploadResult.getUploadId());
            cacheEngine.add(storageType, md5 + SLASH + name, name);
            cacheEngine.add(storageType, md5 + SLASH + TAG, new CopyOnWriteArrayList<>());
        }
    }
    UploadPartRequest uploadPartRequest = new UploadPartRequest();
    uploadPartRequest.setBucketName(bucketName);
    uploadPartRequest.setKey(String.valueOf(cacheEngine.get(storageType, md5 + SLASH + name)));
    uploadPartRequest.setUploadId(String.valueOf(cacheEngine.get(storageType, md5)));
    uploadPartRequest.setInputStream(inputStream);
    // 设置分片大小。除了最后一个分片没有大小限制,其他的分片最小为100 KB。
    uploadPartRequest.setPartSize(chunkSize);
    // 设置分片号。每一个上传的分片都有一个分片号,取值范围是1~10000,如果超出此范围,OSS将返回InvalidArgument错误码。
    uploadPartRequest.setPartNumber(chunk + ONE_INT);
    // 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
    UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
    // 每次上传分片之后,OSS的返回结果包含PartETag。PartETag将被保存在partETags中。
    ((CopyOnWriteArrayList) cacheEngine.get(storageType, md5 + SLASH + TAG)).add(uploadPartResult.getPartETag());
    // 关闭OSSClient。
    client.shutdown();
    VirtualFile virtualFile = VirtualFile.builder().originalFileName(this.newFileName).suffix(this.suffix).uploadStartTime(startDate).uploadEndTime(new Date()).filePath(this.newFileName).fileHash(null).fullFilePath(this.newFileUrl + this.newFileName).build();
    progressListener.end(virtualFile);
    return virtualFile;
}
Also used : VirtualFile(com.zhouzifei.tool.dto.VirtualFile) Date(java.util.Date) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 3 with VirtualFile

use of com.zhouzifei.tool.dto.VirtualFile in project simpleFS by shengdingbox.

the class AwsS3ApiClient method fileList.

@Override
public List<VirtualFile> fileList(FileListRequesr fileListRequesr) {
    this.check();
    // 设置最大个数。
    final int maxKeys = 200;
    String nextContinuationToken = null;
    // 指定前缀,例如exampledir/object。
    final String keyPrefix = fileListRequesr.getPrefix();
    ListObjectsV2Result result = null;
    List<VirtualFile> virtualFiles = new ArrayList<>();
    // 指定返回结果使用URL编码,则您需要对结果中的prefix、delemiter、startAfter、key和commonPrefix进行URL解码。
    do {
        result = amazonS3.listObjectsV2(bucketName);
        // 文件名称解码。
        for (S3ObjectSummary s3ObjectSummary : result.getObjectSummaries()) {
            String decodedKey = null;
            try {
                decodedKey = URLDecoder.decode(s3ObjectSummary.getKey(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            VirtualFile virtualFile = VirtualFile.builder().originalFileName(decodedKey).suffix(this.suffix).uploadStartTime(s3ObjectSummary.getLastModified()).uploadEndTime(s3ObjectSummary.getLastModified()).filePath(this.newFileName).size(s3ObjectSummary.getSize()).fileHash(s3ObjectSummary.getETag()).fullFilePath(this.newFileUrl + decodedKey).build();
            virtualFiles.add(virtualFile);
        }
        nextContinuationToken = result.getNextContinuationToken();
    } while (result.isTruncated());
    // 关闭OSSClient。
    amazonS3.shutdown();
    return virtualFiles;
}
Also used : VirtualFile(com.zhouzifei.tool.dto.VirtualFile) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with VirtualFile

use of com.zhouzifei.tool.dto.VirtualFile in project simpleFS by shengdingbox.

the class AwsS3ApiClient method main.

public static void main(String[] args) {
    final FileProperties fileProperties = new FileProperties();
    final AwsFileProperties awsFileProperties = fileProperties.getAws();
    awsFileProperties.setAccessKey("LTAI5tFpTDE26XYiPmH9dxDz");
    awsFileProperties.setSecretKey("9gC7gs5kEJJmZec6a6QupoefIL82Kr");
    awsFileProperties.setEndpoint("oss-cn-beijing.aliyuncs.com");
    awsFileProperties.setBucketName("simple-fs");
    awsFileProperties.setDomainUrl("https://simple-fs.oss-cn-beijing.aliyuncs.com/");
    final AwsS3ApiClient awsS3ApiClient = new AwsS3ApiClient(fileProperties);
    final File file = new File("/Users/Dabao/Downloads/videoplayback.mp4");
    final VirtualFile virtualFile = awsS3ApiClient.uploadFile(file);
    System.out.println(virtualFile);
    System.out.println(1);
}
Also used : VirtualFile(com.zhouzifei.tool.dto.VirtualFile) FileProperties(com.zhouzifei.tool.config.FileProperties) AwsFileProperties(com.zhouzifei.tool.config.AwsFileProperties) AwsFileProperties(com.zhouzifei.tool.config.AwsFileProperties) VirtualFile(com.zhouzifei.tool.dto.VirtualFile) File(java.io.File)

Example 5 with VirtualFile

use of com.zhouzifei.tool.dto.VirtualFile in project simpleFS by shengdingbox.

the class QCloudOssApiClient method fileList.

@Override
public List<VirtualFile> fileList(FileListRequesr fileListRequesr) {
    List<VirtualFile> virtualFiles = new ArrayList<>();
    // 指定返回结果使用URL编码,则您需要对结果中的prefix、delemiter、startAfter、key和commonPrefix进行URL解码。
    ObjectListing objectListing = null;
    do {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        // 设置 bucket 名称
        listObjectsRequest.setBucketName(bucketName);
        // 设置列出的对象名以 prefix 为前缀
        listObjectsRequest.setPrefix(fileListRequesr.getPrefix());
        // 设置最大列出多少个对象, 一次 listobject 最大支持1000
        listObjectsRequest.setMaxKeys(fileListRequesr.getSize());
        try {
            objectListing = cosClient.listObjects(listObjectsRequest);
        } catch (CosClientException e) {
            e.printStackTrace();
        }
        // 文件名称解码。
        for (COSObjectSummary s : objectListing.getObjectSummaries()) {
            String decodedKey = null;
            try {
                decodedKey = URLDecoder.decode(s.getKey(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            VirtualFile virtualFile = VirtualFile.builder().originalFileName(decodedKey).suffix(this.suffix).uploadStartTime(s.getLastModified()).uploadEndTime(s.getLastModified()).filePath(this.newFileName).size(s.getSize()).fileHash(s.getETag()).fullFilePath(decodedKey).build();
            virtualFiles.add(virtualFile);
        }
        String nextContinuationToken = objectListing.getNextMarker();
    } while (objectListing.isTruncated());
    // 确认本进程不再使用 cosClient 实例之后,关闭之
    cosClient.shutdown();
    return virtualFiles;
}
Also used : VirtualFile(com.zhouzifei.tool.dto.VirtualFile) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

VirtualFile (com.zhouzifei.tool.dto.VirtualFile)7 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ArrayList (java.util.ArrayList)3 FileProperties (com.zhouzifei.tool.config.FileProperties)2 File (java.io.File)2 Date (java.util.Date)2 CosClientException (com.qcloud.cos.exception.CosClientException)1 AwsFileProperties (com.zhouzifei.tool.config.AwsFileProperties)1 XmlyFileProperties (com.zhouzifei.tool.config.XmlyFileProperties)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1