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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations