Exemplo n.º 1
0
    /**
     * Create an object (=file)
     *
     * @param containerName Name of the container
     * @param objectName Name of the file
     * @param contents Binary content of the file
     * @throws IOException
     */
    public void createObject(String containerName, String objectName, byte[] contents)
        throws Exception {
      HttpURLConnection conn =
          getConnBuilder(containerName, objectName)
              .method("PUT")
              .addHeader(HttpHeaders.CONTENT_LENGTH_HEADER, String.valueOf(contents.length))
              .getConnection();

      HttpResponse response = Utils.doSendOperation(conn, contents);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          createObject(containerName, objectName, contents);
        } else {
          log.error(
              "Error creating object "
                  + objectName
                  + " in container "
                  + containerName
                  + ",code = "
                  + response.code);
        }
      }
    }
Exemplo n.º 2
0
    /**
     * List files in a folder
     *
     * @param containerName Folder name
     * @return List of file names
     * @throws IOException
     */
    public List<String> listObjects(String containerName) throws Exception {
      HttpURLConnection urlConnection = getConnBuilder(containerName, null).getConnection();

      HttpResponse response = Utils.doReadOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          return listObjects(containerName);
        } else {
          log.error("Error listing container " + containerName + ", code = " + response.code);
        }
      }
      return response.payloadAsLines();
    }
Exemplo n.º 3
0
    /**
     * Create a container, which is equivalent to a bucket
     *
     * @param containerName Name of the container
     * @throws IOException
     */
    public void createContainer(String containerName) throws Exception {
      HttpURLConnection urlConnection =
          getConnBuilder(containerName, null).method("PUT").getConnection();

      HttpResponse response = Utils.doVoidOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          createContainer(containerName);
        } else {
          log.error("Error creating container " + containerName + " ,code = " + response.code);
        }
      }
    }
Exemplo n.º 4
0
    /**
     * Read the content of a file
     *
     * @param containerName Name of the folder
     * @param objectName name of the file
     * @return Content of the files
     * @throws IOException
     */
    public byte[] readObject(String containerName, String objectName) throws Exception {
      HttpURLConnection urlConnection = getConnBuilder(containerName, objectName).getConnection();

      HttpResponse response = Utils.doReadOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          return readObject(containerName, objectName);
        } else {
          log.error(
              "Error reading object "
                  + objectName
                  + " from container "
                  + containerName
                  + ", code = "
                  + response.code);
        }
      }
      return response.payload;
    }
Exemplo n.º 5
0
    /**
     * Delete a object (=file) from the storage
     *
     * @param containerName Folder name
     * @param objectName File name
     * @throws IOException
     */
    public void deleteObject(String containerName, String objectName) throws Exception {
      HttpURLConnection urlConnection =
          getConnBuilder(containerName, objectName).method("DELETE").getConnection();

      HttpResponse response = Utils.doVoidOperation(urlConnection);

      if (!response.isSuccessCode()) {
        if (response.isAuthDenied()) {
          log.warn("Refreshing credentials and retrying");
          authenticate();
          deleteObject(containerName, objectName);
        } else {
          log.error(
              "Error deleting object "
                  + objectName
                  + " from container "
                  + containerName
                  + ",code = "
                  + response.code);
        }
      }
    }