public PageSet<? extends StorageMetadata> list(String container, ListContainerOptions options) {
    List<StorageObjectMetadata> listFiles;
    try {
      String dir = options.getDir();
      if (dir != null) {
        listFiles = service.listFiles(container, dir, options.isRecursive());
      } else {
        listFiles = service.listFiles(container, "", options.isRecursive());
      }

      List<MutableStorageMetadata> jCloudsMetadata = new ArrayList<MutableStorageMetadata>();
      if (!listFiles.isEmpty()) {
        for (StorageObjectMetadata metadata : listFiles) {
          jCloudsMetadata.add(generateJcloudsMetadata(metadata));
        }
      } else {
        StorageObjectMetadata metadata = new StorageObjectMetadata();
        if (dir != null) metadata.setPathAndName(dir);
        metadata.setType(StorageObjectConstants.DIRECTORY_TYPE);
        jCloudsMetadata.add(generateJcloudsMetadata(metadata));
      }

      return new PageSetImpl<StorageMetadata>(jCloudsMetadata, null);
    } catch (MethodNotSupportedException e) {
      e.printStackTrace();
      return null;
    }
  }
  private void checkForContainerContent(
      final String containerName, String inDirectory, Set<String> expectedBlobKeys) {
    ListContainerOptions options = ListContainerOptions.Builder.recursive();
    if (null != inDirectory && !"".equals(inDirectory)) options.inDirectory(inDirectory);

    PageSet<? extends StorageMetadata> blobsRetrieved = blobStore.list(containerName, options);
    for (Iterator<? extends StorageMetadata> it = blobsRetrieved.iterator(); it.hasNext(); ) {
      // TODO: FluentIterable
      if (it.next().getType() != StorageType.BLOB) {
        it.remove();
      }
    }

    // nothing expected
    if (null == expectedBlobKeys || 0 == expectedBlobKeys.size()) {
      assertTrue(
          blobsRetrieved.isEmpty(),
          "Wrong blob number retrieved in the container [" + containerName + "]");
      return;
    }

    // copies values
    Set<String> expectedBlobKeysCopy = Sets.newHashSet();
    for (String value : expectedBlobKeys) {
      expectedBlobKeysCopy.add(value);
    }
    assertEquals(
        blobsRetrieved.size(),
        expectedBlobKeysCopy.size(),
        "Wrong blob number retrieved in the container [" + containerName + "]");
    for (StorageMetadata data : blobsRetrieved) {
      String blobName = data.getName();
      if (!expectedBlobKeysCopy.remove(blobName)) {
        fail(
            "List for container ["
                + containerName
                + "] contains unexpected value ["
                + blobName
                + "]");
      }
    }
    assertTrue(
        expectedBlobKeysCopy.isEmpty(),
        "List operation for container [" + containerName + "] doesn't return all values.");
  }
예제 #3
0
 /**
  * This implementation invokes {@link S3Client#listBucket}
  *
  * @param container bucket name
  */
 @Override
 public PageSet<? extends StorageMetadata> list(String container, ListContainerOptions options) {
   ListBucketOptions httpOptions = container2BucketListOptions.apply(options);
   PageSet<? extends StorageMetadata> list =
       bucket2ResourceList.apply(sync.listBucket(container, httpOptions));
   return options.isDetailed()
       ? fetchBlobMetadataProvider.get().setContainerName(container).apply(list)
       : list;
 }
 public ListContainerOptions apply(ListBucketOptions[] optionsList) {
   ListContainerOptions options = new ListContainerOptions();
   if (optionsList.length != 0) {
     if (optionsList[0].getDelimiter() == null) {
       options.recursive();
     } else if (!optionsList[0].getDelimiter().equals("/")) {
       throw new IllegalArgumentException("only '/' is allowed as a blobstore delimiter");
     }
     if (optionsList[0].getMarker() != null) {
       options.afterMarker(optionsList[0].getMarker());
     }
     if (optionsList[0].getMaxResults() != null) {
       options.maxResults(optionsList[0].getMaxResults());
     }
     if (optionsList[0].getPrefix() != null) {
       options.underPath(optionsList[0].getPrefix());
     }
   }
   return options;
 }
 @Override
 public void clearContainer(String container, ListContainerOptions options) {
   filesystemContainerNameValidator.validate(container);
   if (options.getDir() != null) {
     container += denormalize("/" + options.getDir());
   }
   try {
     File containerFile = openFolder(container);
     File[] children = containerFile.listFiles();
     if (null != children) {
       for (File child : children)
         if (options.isRecursive() || child.isFile()) {
           Utils.deleteRecursively(child);
         }
     }
   } catch (IOException e) {
     logger.error(e, "An error occurred while clearing container %s", container);
     Throwables.propagate(e);
   }
 }
  /** default maxResults is 1000 */
  @Override
  public ListenableFuture<PageSet<? extends StorageMetadata>> list(
      final String container, ListContainerOptions options) {

    // Check if the container exists
    if (!containerExistsSyncImpl(container)) {
      return immediateFailedFuture(cnfe(container));
    }

    // Loading blobs from container
    Iterable<String> blobBelongingToContainer = null;
    try {
      blobBelongingToContainer = storageStrategy.getBlobKeysInsideContainer(container);
    } catch (IOException e) {
      logger.error(e, "An error occurred loading blobs contained into container %s", container);
      Throwables.propagate(e);
    }

    SortedSet<StorageMetadata> contents =
        newTreeSet(
            transform(
                blobBelongingToContainer,
                new Function<String, StorageMetadata>() {
                  public StorageMetadata apply(String key) {
                    Blob oldBlob = loadFileBlob(container, key);

                    checkState(
                        oldBlob != null,
                        "blob "
                            + key
                            + " is not present although it was in the list of "
                            + container);
                    checkState(
                        oldBlob.getMetadata() != null,
                        "blob " + container + "/" + key + " has no metadata");
                    MutableBlobMetadata md = copy(oldBlob.getMetadata());
                    String directoryName = ifDirectoryReturnName.execute(md);
                    if (directoryName != null) {
                      md.setName(directoryName);
                      md.setType(StorageType.RELATIVE_PATH);
                    }
                    return md;
                  }
                }));

    String marker = null;
    if (options != null) {
      if (options.getMarker() != null) {
        final String finalMarker = options.getMarker();
        StorageMetadata lastMarkerMetadata =
            find(
                contents,
                new Predicate<StorageMetadata>() {
                  public boolean apply(StorageMetadata metadata) {
                    return metadata.getName().equals(finalMarker);
                  }
                });
        contents = contents.tailSet(lastMarkerMetadata);
        contents.remove(lastMarkerMetadata);
      }

      final String prefix = options.getDir();
      if (prefix != null) {
        contents =
            newTreeSet(
                filter(
                    contents,
                    new Predicate<StorageMetadata>() {
                      public boolean apply(StorageMetadata o) {
                        return (o != null
                            && o.getName().startsWith(prefix)
                            && !o.getName().equals(prefix));
                      }
                    }));
      }

      Integer maxResults = options.getMaxResults() != null ? options.getMaxResults() : 1000;
      if (contents.size() > 0) {
        SortedSet<StorageMetadata> contentsSlice = firstSliceOfSize(contents, maxResults);
        if (!contentsSlice.contains(contents.last())) {
          // Partial listing
          marker = contentsSlice.last().getName();
        } else {
          marker = null;
        }
        contents = contentsSlice;
      }

      final String delimiter = options.isRecursive() ? null : File.separator;
      if (delimiter != null) {
        SortedSet<String> commonPrefixes = null;
        Iterable<String> iterable =
            transform(contents, new CommonPrefixes(prefix != null ? prefix : null, delimiter));
        commonPrefixes = iterable != null ? newTreeSet(iterable) : new TreeSet<String>();
        commonPrefixes.remove(CommonPrefixes.NO_PREFIX);

        contents =
            newTreeSet(
                filter(contents, new DelimiterFilter(prefix != null ? prefix : null, delimiter)));

        Iterables.<StorageMetadata>addAll(
            contents,
            transform(
                commonPrefixes,
                new Function<String, StorageMetadata>() {
                  public StorageMetadata apply(String o) {
                    MutableStorageMetadata md = new MutableStorageMetadataImpl();
                    md.setType(StorageType.RELATIVE_PATH);
                    md.setName(o);
                    return md;
                  }
                }));
      }

      // trim metadata, if the response isn't supposed to be detailed.
      if (!options.isDetailed()) {
        for (StorageMetadata md : contents) {
          md.getUserMetadata().clear();
        }
      }
    }

    return Futures.<PageSet<? extends StorageMetadata>>immediateFuture(
        new PageSetImpl<StorageMetadata>(contents, marker));
  }