예제 #1
0
  public static void main(String[] args) throws IOException {

    OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    try {
      // createBuket("create-by-code", client);
      // UploadMethod.sendStr("create-by-code", "Hello OSS!", client);
      // UploadMethod.uploadFile("create-by-code", new File("c:/hello oss.txt"),
      // client);
      // UploadMethod.uploadFileProgress(bucketName, new File(
      // "E:/MOVE/Videos/神探夏洛克-恐怖的新娘/Sherlock Abominable Bride 2015 720p.mkv"),
      // client);
      // UploadMethod.createDir(bucketName, "新建文件夹", client);
      // UploadMethod.createDir(bucketName, "新建文件夹/a", client);
      // UploadMethod.uploadFile(bucketName, new File("c:/test.txt"),
      // "新建文件夹/a/", client);
      // DownloadMethod.readContent(bucketName, "Hello OSS!", client);
      // DownloadMethod.downloadFile(bucketName, client, "新建文件夹/a/test.txt");
      // OpratorMethod.listBucket(client);
      OpratorMethod.listObj(bucketName, client);
    } catch (Exception e) {
      System.err.println(e);
    } finally {
      client.shutdown();
    }
  }
  public static void main(String[] args) throws IOException {
    /*
     * Constructs a client instance with your account for accessing OSS
     */
    OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);

    try {
      /*
       * Append an object from specfied input stream, keep in mind that
       * position should be set to zero at first time.
       */
      String content = "Thank you for using Aliyun Object Storage Service";
      InputStream instream = new ByteArrayInputStream(content.getBytes());
      Long firstPosition = 0L;
      System.out.println("Begin to append object at position(" + firstPosition + ")");
      AppendObjectResult appendObjectResult =
          client.appendObject(new AppendObjectRequest(bucketName, key, instream).withPosition(0L));
      System.out.println(
          "\tNext position="
              + appendObjectResult.getNextPosition()
              + ", CRC64="
              + appendObjectResult.getObjectCRC()
              + "\n");

      /*
       * Continue to append the object from specfied file descriptor at last position
       */
      Long nextPosition = appendObjectResult.getNextPosition();
      System.out.println("Continue to append object at last position(" + nextPosition + "):");
      appendObjectResult =
          client.appendObject(
              new AppendObjectRequest(bucketName, key, createTempFile())
                  .withPosition(nextPosition));
      System.out.println(
          "\tNext position="
              + appendObjectResult.getNextPosition()
              + ", CRC64="
              + appendObjectResult.getObjectCRC());

      /*
       * View object type of the appendable object
       */
      OSSObject object = client.getObject(bucketName, key);
      System.out.println("\tObject type=" + object.getObjectMetadata().getObjectType() + "\n");
      // Do not forget to close object input stream if not use it any more
      object.getObjectContent().close();

      /*
       * Delete the appendable object
       */
      System.out.println("Deleting an appendable object");
      client.deleteObject(bucketName, key);

    } catch (OSSException oe) {
      System.out.println(
          "Caught an OSSException, which means your request made it to OSS, "
              + "but was rejected with an error response for some reason.");
      System.out.println("Error Message: " + oe.getErrorCode());
      System.out.println("Error Code:       " + oe.getErrorCode());
      System.out.println("Request ID:      " + oe.getRequestId());
      System.out.println("Host ID:           " + oe.getHostId());
    } catch (ClientException ce) {
      System.out.println(
          "Caught an ClientException, which means the client encountered "
              + "a serious internal problem while trying to communicate with OSS, "
              + "such as not being able to access the network.");
      System.out.println("Error Message: " + ce.getMessage());
    } finally {
      /*
       * Do not forget to shut down the client finally to release all allocated resources.
       */
      client.shutdown();
    }
  }