Search in sources :

Example 1 with Item

use of io.minio.messages.Item in project fess by codelibs.

the class AdminStorageAction method getFileItems.

public static List<Map<String, Object>> getFileItems(final String prefix) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final ArrayList<Map<String, Object>> list = new ArrayList<>();
    try {
        final MinioClient minioClient = createClient(fessConfig);
        final ListObjectsArgs args = ListObjectsArgs.builder().bucket(fessConfig.getStorageBucket()).prefix(prefix != null && prefix.length() > 0 ? prefix + "/" : prefix).recursive(false).includeUserMetadata(false).useApiVersion1(false).build();
        for (final Result<Item> result : minioClient.listObjects(args)) {
            final Map<String, Object> map = new HashMap<>();
            final Item item = result.get();
            final String objectName = item.objectName();
            map.put("id", encodeId(objectName));
            map.put("name", getName(objectName));
            map.put("hashCode", item.hashCode());
            map.put("size", item.size());
            map.put("directory", item.isDir());
            if (!item.isDir()) {
                map.put("lastModified", item.lastModified());
            }
            list.add(map);
            if (list.size() > fessConfig.getStorageMaxItemsInPageAsInteger()) {
                break;
            }
        }
    } catch (final ErrorResponseException e) {
        final String code = e.errorResponse().code();
        if ("NoSuchBucket".equals(code)) {
            final MinioClient minioClient = createClient(fessConfig);
            try {
                final MakeBucketArgs args = MakeBucketArgs.builder().bucket(fessConfig.getStorageBucket()).build();
                minioClient.makeBucket(args);
                logger.info("Created bucket: {}", fessConfig.getStorageBucket());
            } catch (final Exception e1) {
                logger.warn("Failed to create bucket: {}", fessConfig.getStorageBucket(), e1);
            }
        } else if (logger.isDebugEnabled()) {
            logger.debug("Failed to access {}", fessConfig.getStorageEndpoint(), e);
        }
    } catch (final Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to access {}", fessConfig.getStorageEndpoint(), e);
        }
    }
    return list;
}
Also used : MakeBucketArgs(io.minio.MakeBucketArgs) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) StorageException(org.codelibs.fess.exception.StorageException) ErrorResponseException(io.minio.errors.ErrorResponseException) ListObjectsArgs(io.minio.ListObjectsArgs) Item(io.minio.messages.Item) MinioClient(io.minio.MinioClient) ErrorResponseException(io.minio.errors.ErrorResponseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Item

use of io.minio.messages.Item in project Universal-G-Code-Sender by winder.

the class S3FileSystemView method getFiles.

@Override
public File[] getFiles(final File dir, boolean useFileHiding) {
    // If there is no bucket (i.e. just "s3:/"), add the buckets.
    if (!hasBucket(dir.toString())) {
        try {
            final List<File> files = new ArrayList<>(1);
            for (Bucket bucket : buckets) {
                files.add(new S3VirtualFile("s3:/" + bucket.name() + "/", 0));
            }
            return files.toArray(new File[files.size()]);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.log(Level.WARNING, "An error occurred listing buckets on S3.", ex);
        }
        return new S3VirtualFile[0];
    }
    ArrayList<S3VirtualFile> ret = new ArrayList<>();
    Matcher m = parseURI(dir.toString());
    try {
        if (m.matches()) {
            String bucket = m.group("bucket");
            // Path should start with a slash unless it's empty
            String path = m.group("path");
            if (!"".equals(path)) {
                path += "/";
            }
            Iterable<Result<Item>> objects = minioClient.listObjects(bucket, path, false);
            String prefix = "s3:/" + bucket + "/";
            String dirMatch = dir.toString() + "/";
            for (Result<Item> res : objects) {
                Item i = res.get();
                String name = prefix + i.objectName();
                // listObjects matches the current directory, filter it out.
                if (name.equals(dirMatch)) {
                    continue;
                }
                S3VirtualFile f = new S3VirtualFile(name, i.objectSize());
                if (!f.isDirectory()) {
                    try {
                        f.setLastModified(i.lastModified().getTime());
                    } catch (Exception e) {
                    // The mock server doesn't play well with the lastModified field.
                    }
                }
                ret.add(f);
            }
        }
    } catch (Exception ex) {
        logger.log(Level.WARNING, "An error occurred listing files on S3.", ex);
    }
    return ret.toArray(new S3VirtualFile[0]);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) InvalidBucketNameException(io.minio.errors.InvalidBucketNameException) InvalidResponseException(io.minio.errors.InvalidResponseException) NoResponseException(io.minio.errors.NoResponseException) IOException(java.io.IOException) InternalException(io.minio.errors.InternalException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RegionConflictException(io.minio.errors.RegionConflictException) ErrorResponseException(io.minio.errors.ErrorResponseException) InvalidKeyException(java.security.InvalidKeyException) InsufficientDataException(io.minio.errors.InsufficientDataException) Result(io.minio.Result) Item(io.minio.messages.Item) Bucket(io.minio.messages.Bucket) File(java.io.File)

Aggregations

ErrorResponseException (io.minio.errors.ErrorResponseException)2 Item (io.minio.messages.Item)2 ArrayList (java.util.ArrayList)2 ListObjectsArgs (io.minio.ListObjectsArgs)1 MakeBucketArgs (io.minio.MakeBucketArgs)1 MinioClient (io.minio.MinioClient)1 Result (io.minio.Result)1 InsufficientDataException (io.minio.errors.InsufficientDataException)1 InternalException (io.minio.errors.InternalException)1 InvalidBucketNameException (io.minio.errors.InvalidBucketNameException)1 InvalidResponseException (io.minio.errors.InvalidResponseException)1 NoResponseException (io.minio.errors.NoResponseException)1 RegionConflictException (io.minio.errors.RegionConflictException)1 Bucket (io.minio.messages.Bucket)1 File (java.io.File)1 IOException (java.io.IOException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1