/**
   * * Create a bucket with the specified name in the specified project. If successful, return the
   * created bucket metadata.
   *
   * <p>For more information, see <a
   * href="https://cloud.google.com/storage/docs/json_api/v1/buckets/insert"
   * target="_blank">Buckets:insert</a>
   *
   * @param bucketName The name of the bucket to create.
   * @throws IOException IO error
   * @return true if the operation succeeded; otherwise, false
   */
  public static boolean insertBucket(String bucketName) throws IOException {
    displayMessageHeader("Create the bucket: " + bucketName);

    Storage.Buckets buckets = storageService.buckets();
    boolean bucketCreated = false;

    // Set the bucket access control list.
    ObjectAccessControl acl = new ObjectAccessControl();
    acl.setEntity("allAuthenticatedUsers").setRole("READER");

    // Create bucket.
    Bucket newBucket = new Bucket().setName(bucketName);
    newBucket.setLocation("US");
    newBucket.setDefaultObjectAcl(ImmutableList.of(acl));

    Storage.Buckets.Insert insertBucket = buckets.insert(settings.getProject(), newBucket);

    try {
      @SuppressWarnings("unused")
      Bucket createdBucket = insertBucket.execute();
      bucketCreated = true;
      System.out.println(String.format("Bucket %s created", bucketName));
    } catch (GoogleJsonResponseException e) {
      GoogleJsonError error = e.getDetails();
      if (error.getCode() == HTTP_CONFLICT
          && error.getMessage().contains("You already own this bucket.")) {
        System.out.println("already exists");
      } else {
        throw e;
      }
    }
    return bucketCreated;
  }
Esempio n. 2
0
 /**
  * Logs the given throwable and shows an error alert dialog with its message.
  *
  * @param activity activity
  * @param tag log tag to use
  * @param t throwable to log and show
  */
 public static void logAndShow(Activity activity, String tag, Throwable t) {
   Log.e(tag, "Error", t);
   String message = t.getMessage();
   // Exceptions that occur in your Cloud Endpoint implementation classes
   // are wrapped as GoogleJsonResponseExceptions
   if (t instanceof GoogleJsonResponseException) {
     GoogleJsonError details = ((GoogleJsonResponseException) t).getDetails();
     if (details != null) {
       message = details.getMessage();
     }
   }
   showError(activity, message);
 }
 /**
  * Logs the given throwable and shows an error alert dialog with its message.
  *
  * @param activity activity
  * @param tag log tag to use
  * @param t throwable to log and show
  */
 public static void logAndShow(Activity activity, String tag, Throwable t) {
   Log.e(tag, "Error", t);
   String message = t.getMessage();
   if (t instanceof GoogleJsonResponseException) {
     GoogleJsonError details = ((GoogleJsonResponseException) t).getDetails();
     if (details != null) {
       message = details.getMessage();
     }
   } else if (t.getCause() instanceof GoogleAuthException) {
     message = ((GoogleAuthException) t.getCause()).getMessage();
   }
   showError(activity, message);
 }