OSS

103 阅读1分钟

dependencies

<dependencies>
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.10.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
</dependencies>

OSSUtil

public class OSSUtil {

  private final String bucketName;
  private final OSS ossClient;
  private static final Integer MAX_KEYS = 100;

  public OSSUtil(String endpoint, String accessKeyId, String accessKeySecret, String bucketName) {
    this.bucketName = bucketName;
    this.ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  }

  public void createBucket(String bucketName) {
    ossClient.createBucket(bucketName);
  }

  public void putFileStream(String filePath, InputStream inputStream) {
    ossClient.putObject(bucketName, filePath, inputStream);
  }

  public File getFile(String filePath) throws IOException {
    File file = File.createTempFile("test", "oss");
    ossClient.getObject(new GetObjectRequest(bucketName, filePath), file);
    return file;
  }

  /**
   * If you use a fuzzy prefix, you will get the folders or files matched by the fuzzy search. If
   * you use an exact path, you will get a list of files under that path. If you use the exact file
   * path, you won't get anything.
   */
  public List<OSSFileInfoEntity> getFileInfoList(String filePath) {
    ObjectListing objectListing;
    String nextMarker = null;
    List<OSSFileInfoEntity> ossFileInfoList = new ArrayList<>();
    do {
      objectListing =
          ossClient.listObjects(
              new ListObjectsRequest(bucketName)
                  .withPrefix(filePath)
                  .withDelimiter("/")
                  .withMarker(nextMarker)
                  .withMaxKeys(MAX_KEYS));

      // List direct child file and skip the path itself
      List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
      for (OSSObjectSummary summary : sums) {
        String fileName = summary.getKey();
        if (fileName.equals(filePath)) {
          continue;
        }
        ossFileInfoList.add(new OSSFileInfoEntity().isFile(true).filePath(fileName));
      }

      // List direct child path
      List<String> comm = objectListing.getCommonPrefixes();
      for (String pathName : comm) {
        ossFileInfoList.add(
            new OSSFileInfoEntity().isFile(false).filePath(pathName));
      }

      nextMarker = objectListing.getNextMarker();
    } while (objectListing.isTruncated());
    return ossFileInfoList;
  }
}

OSSTest

public class OSSUtilTest {

  OSSUtil ossUtil = new OSSUtil(
      "",
      "",
      "",
      ""
  );

  private String filePath = "chaojie/testpath/testfile";

  @Test
  public void testCreateBucket() {
    ossUtil.createBucket("test-oss-my-bucket");
  }

  @Test
  public void testPutFileStream() throws FileNotFoundException {
    InputStream inputStream = new FileInputStream("/Users/admin/Desktop/OSS.md");
    ossUtil.putFileStream(filePath, inputStream);
  }

  @Test
  public void testGetFile() throws IOException {
    File file = ossUtil.getFile(filePath);
    System.out.println("======>" + file.getPath());
  }

  @Test
  public void testGetFileInfoList() {
    List<OSSFileInfoEntity> result = ossUtil.getFileInfoList("chaojie/testfile");
    for(var fileInfo : result) {
      System.out.println(fileInfo);
    }
  }
}