Search in sources :

Example 11 with FileInfo

use of com.qiniu.storage.model.FileInfo in project yshopmall by guchengwuyue.

the class QiNiuServiceImpl method synchronize.

@Override
// @CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void synchronize(QiniuConfig config) {
    if (config.getId() == null) {
        throw new BadRequestException("请先添加相应配置,再操作");
    }
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiNiuUtil.getRegion(config.getZone()));
    Auth auth = Auth.create(config.getAccessKey(), config.getSecretKey());
    BucketManager bucketManager = new BucketManager(auth, cfg);
    // 文件名前缀
    String prefix = "";
    // 每次迭代的长度限制,最大1000,推荐值 1000
    int limit = 1000;
    // 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
    String delimiter = "";
    // 列举空间文件列表
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(config.getBucket(), prefix, limit, delimiter);
    while (fileListIterator.hasNext()) {
        // 处理获取的file list结果
        QiniuContent qiniuContent;
        FileInfo[] items = fileListIterator.next();
        for (FileInfo item : items) {
            if (qiniuContentService.getOne(new LambdaQueryWrapper<QiniuContent>().eq(QiniuContent::getName, FileUtil.getFileNameNoEx(item.key))) == null) {
                qiniuContent = new QiniuContent();
                qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(item.fsize + "")));
                qiniuContent.setSuffix(FileUtil.getExtensionName(item.key));
                qiniuContent.setName(FileUtil.getFileNameNoEx(item.key));
                qiniuContent.setType(config.getType());
                qiniuContent.setBucket(config.getBucket());
                qiniuContent.setUrl(config.getHost() + "/" + item.key);
                qiniuContentService.save(qiniuContent);
            }
        }
    }
}
Also used : Configuration(com.qiniu.storage.Configuration) FileInfo(com.qiniu.storage.model.FileInfo) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) BadRequestException(co.yixiang.exception.BadRequestException) QiniuContent(co.yixiang.modules.tools.domain.QiniuContent) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

FileInfo (com.qiniu.storage.model.FileInfo)11 BucketManager (com.qiniu.storage.BucketManager)6 QiniuException (com.qiniu.common.QiniuException)5 Configuration (com.qiniu.storage.Configuration)5 Auth (com.qiniu.util.Auth)5 Date (java.util.Date)4 Transactional (org.springframework.transaction.annotation.Transactional)3 DirectoryOssInfo (io.github.artislong.model.DirectoryOssInfo)2 FileOssInfo (io.github.artislong.model.FileOssInfo)2 OssInfo (io.github.artislong.model.OssInfo)2 SneakyThrows (lombok.SneakyThrows)2 BadRequestException (co.yixiang.exception.BadRequestException)1 QiniuContent (co.yixiang.modules.tools.domain.QiniuContent)1 CustomException (com.dimple.common.exception.CustomException)1 QiNiuConfig (com.dimple.project.common.domain.QiNiuConfig)1 QiNiuContent (com.dimple.project.tool.domain.QiNiuContent)1 CObjectMetadata (com.jeesuite.cos.CObjectMetadata)1 CObjectMetadata (com.mendmix.cos.CObjectMetadata)1 Response (com.qiniu.http.Response)1 UploadManager (com.qiniu.storage.UploadManager)1