Search in sources :

Example 1 with FileInfo

use of com.qiniu.storage.model.FileInfo in project eladmin by elunez.

the class QiNiuServiceImpl method synchronize.

@Override
@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 (qiniuContentRepository.findByKey(FileUtil.getFileNameNoEx(item.key)) == null) {
                qiniuContent = new QiniuContent();
                qiniuContent.setSize(FileUtil.getSize(Integer.parseInt(item.fsize + "")));
                qiniuContent.setSuffix(FileUtil.getExtensionName(item.key));
                qiniuContent.setKey(FileUtil.getFileNameNoEx(item.key));
                qiniuContent.setType(config.getType());
                qiniuContent.setBucket(config.getBucket());
                qiniuContent.setUrl(config.getHost() + "/" + item.key);
                qiniuContentRepository.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(me.zhengjie.exception.BadRequestException) QiniuContent(me.zhengjie.domain.QiniuContent) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with FileInfo

use of com.qiniu.storage.model.FileInfo in project DimpleBlog by martin-chips.

the class QiNiuServiceImpl method synchronize.

@Override
@Transactional
public int synchronize() {
    QiNiuConfig qiNiuConfig = getQiNiuConfig();
    if (!qiNiuConfig.check()) {
        throw new CustomException("七牛云配置信息不完整,请先填写七牛云配置信息");
    }
    // 构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(QiNiuUtils.getRegion(qiNiuConfig.getZone()));
    Auth auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
    BucketManager bucketManager = new BucketManager(auth, cfg);
    // 文件名前缀
    String prefix = "";
    // 每次迭代的长度限制,最大1000,推荐值 1000
    int limit = 1000;
    // 指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
    String delimiter = "";
    // 列举空间文件列表
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(qiNiuConfig.getBucket(), prefix, limit, delimiter);
    int count = 0;
    while (fileListIterator.hasNext()) {
        // 处理获取的file list结果
        QiNiuContent qiNiuContent;
        // 删除所有数据
        qiNiuContentMapper.clearContent();
        FileInfo[] items = fileListIterator.next();
        if (Objects.nonNull(items)) {
            String username = SecurityUtils.getUsername();
            for (FileInfo item : items) {
                qiNiuContent = new QiNiuContent();
                qiNiuContent.setSize(FileUtils.getSizeString(Integer.parseInt(item.fsize + "")));
                qiNiuContent.setSuffix(FileUtils.getExtensionName(item.key));
                qiNiuContent.setName(FileUtils.getFileNameNoExtension(item.key));
                qiNiuContent.setType(qiNiuConfig.getType());
                qiNiuContent.setBucket(qiNiuConfig.getBucket());
                qiNiuContent.setUrl("http://" + qiNiuConfig.getHost() + "/" + item.key);
                qiNiuContent.setCreateBy(username);
                qiNiuContent.setCreateTime(convertUnixTime(item.putTime + ""));
                count += qiNiuContentMapper.insertContent(qiNiuContent);
            }
        }
    }
    return count;
}
Also used : Configuration(com.qiniu.storage.Configuration) FileInfo(com.qiniu.storage.model.FileInfo) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) QiNiuConfig(com.dimple.project.common.domain.QiNiuConfig) QiNiuContent(com.dimple.project.tool.domain.QiNiuContent) CustomException(com.dimple.common.exception.CustomException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with FileInfo

use of com.qiniu.storage.model.FileInfo in project accright-blog by Accright.

the class QiniuApi method getFileInfo.

public FileInfo getFileInfo(String fileName) {
    BucketManager bucketManager = new BucketManager(this.auth);
    FileInfo info = null;
    try {
        info = bucketManager.stat(config.getQiniuBucketName(), fileName);
        log.info(info.hash);
        log.info(info.key);
    } catch (QiniuException e) {
        Response r = e.response;
        log.error(r.toString(), e);
    }
    return info;
}
Also used : Response(com.qiniu.http.Response) QiniuException(com.qiniu.common.QiniuException) FileInfo(com.qiniu.storage.model.FileInfo) BucketManager(com.qiniu.storage.BucketManager)

Example 4 with FileInfo

use of com.qiniu.storage.model.FileInfo in project oss-spring-boot-starter by ArtIsLong.

the class QiNiuOssClient method getInfo.

@SneakyThrows
@Override
public OssInfo getInfo(String targetName, Boolean isRecursion) {
    String key = getKey(targetName, false);
    OssInfo ossInfo = getBaseInfo(targetName);
    if (isRecursion && isDirectory(key)) {
        FileListing listFiles = bucketManager.listFiles(getBucket(), key, "", 1000, "/");
        System.out.println(listFiles);
        List<OssInfo> fileOssInfos = new ArrayList<>();
        List<OssInfo> directoryInfos = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(listFiles.items)) {
            for (FileInfo fileInfo : listFiles.items) {
                fileOssInfos.add(getInfo(OssPathUtil.replaceKey(fileInfo.key, getBasePath(), false), false));
            }
        }
        if (ObjectUtil.isNotEmpty(listFiles.commonPrefixes)) {
            for (String commonPrefix : listFiles.commonPrefixes) {
                String target = OssPathUtil.replaceKey(commonPrefix, getBasePath(), true);
                directoryInfos.add(getInfo(target, true));
            }
        }
        if (ObjectUtil.isNotEmpty(fileOssInfos) && fileOssInfos.get(0) instanceof FileOssInfo) {
            ReflectUtil.setFieldValue(ossInfo, "fileInfos", fileOssInfos);
        }
        if (ObjectUtil.isNotEmpty(directoryInfos) && directoryInfos.get(0) instanceof DirectoryOssInfo) {
            ReflectUtil.setFieldValue(ossInfo, "directoryInfos", directoryInfos);
        }
    }
    return ossInfo;
}
Also used : FileInfo(com.qiniu.storage.model.FileInfo) FileListing(com.qiniu.storage.model.FileListing) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) ArrayList(java.util.ArrayList) FileOssInfo(io.github.artislong.model.FileOssInfo) OssInfo(io.github.artislong.model.OssInfo) DirectoryOssInfo(io.github.artislong.model.DirectoryOssInfo) FileOssInfo(io.github.artislong.model.FileOssInfo) SneakyThrows(lombok.SneakyThrows)

Example 5 with FileInfo

use of com.qiniu.storage.model.FileInfo in project Tbed by Hello-hao.

the class KODOImageupload method Initialize.

public static Integer Initialize(Keys k) {
    int ret = -1;
    if (k.getEndpoint() != null && k.getAccessSecret() != null && k.getEndpoint() != null && k.getBucketname() != null && k.getRequestAddress() != null) {
        if (!k.getEndpoint().equals("") && !k.getAccessSecret().equals("") && !k.getEndpoint().equals("") && !k.getBucketname().equals("") && !k.getRequestAddress().equals("")) {
            Configuration cfg;
            if (k.getEndpoint().equals("1")) {
                cfg = new Configuration(Zone.zone0());
            } else if (k.getEndpoint().equals("2")) {
                cfg = new Configuration(Zone.zone1());
            } else if (k.getEndpoint().equals("3")) {
                cfg = new Configuration(Zone.zone2());
            } else if (k.getEndpoint().equals("4")) {
                cfg = new Configuration(Zone.zoneNa0());
            } else {
                cfg = new Configuration(Zone.zoneAs0());
            }
            UploadManager uploadManager = new UploadManager(cfg);
            Auth auth = Auth.create(k.getAccessKey(), k.getAccessSecret());
            // auth.uploadToken(k.getBucketname());
            String upToken = auth.uploadToken(k.getBucketname(), null, 7200, null);
            BucketManager bmObj = new BucketManager(auth, cfg);
            BucketManager.FileListIterator fileListIterator = null;
            try {
                fileListIterator = bmObj.createFileListIterator(k.getBucketname(), "", 1, "/");
                FileInfo[] items = fileListIterator.next();
                if (items != null) {
                    ret = 1;
                    bucketManager = bmObj;
                    key = k;
                }
            } catch (Exception e) {
                System.out.println("KODO Object Is null");
                ret = -1;
            }
        }
    }
    return ret;
}
Also used : Configuration(com.qiniu.storage.Configuration) FileInfo(com.qiniu.storage.model.FileInfo) BucketManager(com.qiniu.storage.BucketManager) Auth(com.qiniu.util.Auth) UploadManager(com.qiniu.storage.UploadManager) QiniuException(com.qiniu.common.QiniuException)

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