Java Code Examples for com.google.api.services.storage.model.StorageObject#getName()

The following examples show how to use com.google.api.services.storage.model.StorageObject#getName() . 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: GcsFileSystem.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Expands a pattern into {@link MatchResult}.
 *
 * @throws IllegalArgumentException if {@code gcsPattern} does not contain globs.
 */
@VisibleForTesting
MatchResult expand(GcsPath gcsPattern) throws IOException {
  String prefix = GcsUtil.getNonWildcardPrefix(gcsPattern.getObject());
  Pattern p = Pattern.compile(GcsUtil.wildcardToRegexp(gcsPattern.getObject()));

  LOG.debug(
      "matching files in bucket {}, prefix {} against pattern {}",
      gcsPattern.getBucket(),
      prefix,
      p.toString());

  String pageToken = null;
  List<Metadata> results = new ArrayList<>();
  do {
    Objects objects = options.getGcsUtil().listObjects(gcsPattern.getBucket(), prefix, pageToken);
    if (objects.getItems() == null) {
      break;
    }

    // Filter objects based on the regex.
    for (StorageObject o : objects.getItems()) {
      String name = o.getName();
      // Skip directories, which end with a slash.
      if (p.matcher(name).matches() && !name.endsWith("/")) {
        LOG.debug("Matched object: {}", name);
        results.add(toMetadata(o));
      }
    }
    pageToken = objects.getNextPageToken();
  } while (pageToken != null);
  return MatchResult.create(Status.OK, results);
}
 
Example 2
Source File: GcsPath.java    From beam with Apache License 2.0 4 votes vote down vote up
/** Creates a GcsPath from a {@linkplain StorageObject}. */
public static GcsPath fromObject(StorageObject object) {
  return new GcsPath(null, object.getBucket(), object.getName());
}
 
Example 3
Source File: GcsUtil.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Expands a pattern into matched paths. The pattern path may contain globs, which are expanded in
 * the result. For patterns that only match a single object, we ensure that the object exists.
 */
public List<GcsPath> expand(GcsPath gcsPattern) throws IOException {
  Pattern p = null;
  String prefix = null;
  if (isWildcard(gcsPattern)) {
    // Part before the first wildcard character.
    prefix = getNonWildcardPrefix(gcsPattern.getObject());
    p = Pattern.compile(wildcardToRegexp(gcsPattern.getObject()));
  } else {
    // Not a wildcard.
    try {
      // Use a get request to fetch the metadata of the object, and ignore the return value.
      // The request has strong global consistency.
      getObject(gcsPattern);
      return ImmutableList.of(gcsPattern);
    } catch (FileNotFoundException e) {
      // If the path was not found, return an empty list.
      return ImmutableList.of();
    }
  }

  LOG.debug(
      "matching files in bucket {}, prefix {} against pattern {}",
      gcsPattern.getBucket(),
      prefix,
      p.toString());

  String pageToken = null;
  List<GcsPath> results = new ArrayList<>();
  do {
    Objects objects = listObjects(gcsPattern.getBucket(), prefix, pageToken);
    if (objects.getItems() == null) {
      break;
    }

    // Filter objects based on the regex.
    for (StorageObject o : objects.getItems()) {
      String name = o.getName();
      // Skip directories, which end with a slash.
      if (p.matcher(name).matches() && !name.endsWith("/")) {
        LOG.debug("Matched object: {}", name);
        results.add(GcsPath.fromObject(o));
      }
    }
    pageToken = objects.getNextPageToken();
  } while (pageToken != null);

  return results;
}