com.qiniu.storage.model.FileInfo Java Examples

The following examples show how to use com.qiniu.storage.model.FileInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QiniuService.java    From qiniu with MIT License 6 votes vote down vote up
/**
 * 获取空间文件列表
 */
public void listFile() {
    MainController main = MainController.getInstance();
    // 列举空间文件列表
    BucketManager.FileListIterator iterator = sdkManager.getFileListIterator(main.bucketCB.getValue());
    ArrayList<FileBean> files = new ArrayList<>();
    main.setDataLength(0);
    main.setDataSize(0);
    // 处理结果
    while (iterator.hasNext()) {
        FileInfo[] items = iterator.next();
        for (FileInfo item : items) {
            main.setDataLength(main.getDataLength() + 1);
            main.setDataSize(main.getDataSize() + item.fsize);
            // 将七牛的时间单位(100纳秒)转换成毫秒,然后转换成时间
            String time = Formatter.timeStampToString(item.putTime / 10000);
            String size = Formatter.formatSize(item.fsize);
            FileBean file = new FileBean(item.key, item.mimeType, size, time);
            files.add(file);
        }
    }
    main.setResData(FXCollections.observableArrayList(files));
}
 
Example #2
Source File: QiNiuUtil.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * 列举指定前缀的文件
 *
 * @param prefix
 */
public void fileList(String prefix) {
    //每次迭代的长度限制,最大1000,推荐值 1000
    int limit = 1000;
    //指定目录分隔符,列出所有公共前缀(模拟列出目录效果)。缺省值为空字符串
    String delimiter = "";
    //列举空间文件列表
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucketName, prefix, limit, delimiter);
    while (fileListIterator.hasNext()) {
        //处理获取的file list结果
        FileInfo[] items = fileListIterator.next();
        for (FileInfo item : items) {
            log.info(item.key);
        }
    }
}
 
Example #3
Source File: QiNiuServiceImpl.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void synchronize(QiniuConfig config) {
    if (config.getId() == null) {
        throw new SkException("请先添加相应配置,再操作");
    }
    //构造一个带指定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 (qiniuContentDao.findByKey(FileUtils.getFileNameNoEx(item.key)) == null) {
                qiniuContent = new QiniuContent();
                qiniuContent.setSize(FileUtils.getSize(Integer.parseInt(item.fsize + "")));
                qiniuContent.setSuffix(FileUtils.getExtensionName(item.key));
                qiniuContent.setKey(FileUtils.getFileNameNoEx(item.key));
                qiniuContent.setType(config.getType());
                qiniuContent.setBucket(config.getBucket());
                qiniuContent.setUrl(config.getHost() + "/" + item.key);
                qiniuContentDao.save(qiniuContent);
            }
        }
    }
}
 
Example #4
Source File: QiNiuCloudTemplate.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获取文件元信息
 *
 * @param bucketName 存储桶名称
 * @param fileName   存储桶文件名称
 * @return 文件元信息
 */
@Override
@SneakyThrows
public OssFileMetaInfo getFileMetaInfo(String bucketName, String fileName) {
	final FileInfo fileInfo = bucketManager.stat(getBucketName(bucketName), fileName);
	OssFileMetaInfo metaInfo = new OssFileMetaInfo();
	metaInfo.setName(StrUtil.isNotBlank(fileInfo.key) ? fileInfo.key : fileName);
	metaInfo.setLink(getFileLink(metaInfo.getName()));
	metaInfo.setHash(fileInfo.hash);
	metaInfo.setLength(fileInfo.fsize);
	// 单位是 100 纳秒 所以除以 1000 * 10
	metaInfo.setUploadTime(new Date(fileInfo.putTime / (1000 * 10)));
	metaInfo.setContentType(fileInfo.mimeType);
	return metaInfo;
}
 
Example #5
Source File: QiniuOssClientTest.java    From markdown-image-kit with MIT License 5 votes vote down vote up
public static String upload(File file, String bucket, String accessKey, String secretKey, String endpoint) {
    //构造一个带指定Zone对象的配置类
    Configuration cfg = new Configuration(Zone.zone0());
    //...其他参数参考类注释

    UploadManager uploadManager = new UploadManager(cfg);
    //默认不指定key的情况下,以文件内容的hash值作为文件名
    String key = "test.png";

    Auth auth = Auth.create(accessKey, secretKey);
    String upToken = auth.uploadToken(bucket);
    BucketManager bucketManager = new BucketManager(auth, cfg);

    try {
        Response response = uploadManager.put(file, key, upToken);
        //解析上传成功的结果
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        log.info(putRet.key);
        log.info(putRet.hash);

        FileInfo fileInfo = bucketManager.stat(bucket, key);
        System.out.println(fileInfo.hash);
        System.out.println(fileInfo.fsize);
        System.out.println(fileInfo.mimeType);
        System.out.println(fileInfo.putTime);
        return endpoint + key;
    } catch (QiniuException ex) {
        Response r = ex.response;
        log.info(r.toString());
        try {
            log.info(r.bodyString());
        } catch (QiniuException ex2) {
            //ignore
        }
    }
    return "";
}
 
Example #6
Source File: QiNiuServiceImpl.java    From yshopmall with Apache License 2.0 5 votes vote down vote up
@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 QueryWrapper<QiniuContent>().eq("name",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);
                }
            }
        }
    }
 
Example #7
Source File: QiNiuServiceImpl.java    From eladmin with Apache License 2.0 5 votes vote down vote up
@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);
            }
        }
    }
}
 
Example #8
Source File: QiniuTemplate.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SneakyThrows
public OssFile statFile(String bucketName, String fileName) {
	FileInfo stat = bucketManager.stat(getBucketName(bucketName), fileName);
	OssFile ossFile = new OssFile();
	ossFile.setName(stat.key);
	ossFile.setName(Func.isEmpty(stat.key) ? fileName : stat.key);
	ossFile.setLink(fileLink(ossFile.getName()));
	ossFile.setHash(stat.hash);
	ossFile.setLength(stat.fsize);
	ossFile.setPutTime(new Date(stat.putTime / 10000));
	ossFile.setContentType(stat.mimeType);
	return ossFile;
}
 
Example #9
Source File: QiNiuUtil.java    From javabase with Apache License 2.0 5 votes vote down vote up
/**
 * 检查文件是否存在。图片已经存在就不上传了
 *
 * @param key
 * @return
 */
private boolean checkExist(String key) {
    try {
        // 调用stat()方法获取文件的信息
        FileInfo info = bucketManager.stat(bucketName, key);
        return true;
    } catch (QiniuException e) {
        //log.info("图片不存在:"+key);
        return false;
    }
}
 
Example #10
Source File: QiNiuUtil.java    From javabase with Apache License 2.0 5 votes vote down vote up
public void deleteFileList(String prefix) throws Exception {
    BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucketName, prefix, 1000, "");
    while (fileListIterator.hasNext()) {
        //处理获取的file list结果
        FileInfo[] items = fileListIterator.next();
        List<String> list = new ArrayList<>(items.length);
        for (FileInfo item : items) {
            list.add(item.key);
        }
       getDeleteBlockingDeque().put(list);
    }
}
 
Example #11
Source File: QiNiuServiceImpl.java    From DimpleBlog with Apache License 2.0 4 votes vote down vote up
@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;
}