com.aliyun.oss.model.ListObjectsRequest Java Examples

The following examples show how to use com.aliyun.oss.model.ListObjectsRequest. 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: AliOSSBlobStoreTest.java    From multiapps-controller with Apache License 2.0 6 votes vote down vote up
@Test
public void testList() throws Exception {
    ObjectListing objectListing = new ObjectListing();
    objectListing.setBucketName(CONTAINER);
    objectListing.setObjectSummaries(getObjectSummaries(3));
    Mockito.when(ossClient.listObjects(any(ListObjectsRequest.class)))
           .thenReturn(objectListing);

    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setUserMetadata(getUserMetadata());

    Mockito.when(ossClient.getObjectMetadata(any(String.class), any(String.class)))
           .thenReturn(objectMetadata);
    Mockito.when(ossClient.generatePresignedUrl(any(), any(), any()))
           .thenReturn(new URL("https://oss-eu-central-1.aliyuncs.com"));
    aliOSSBlobStore.list(CONTAINER, new ListContainerOptions().withDetails())
                   .forEach(storageMetadata -> {
        assertTrue(storageMetadata.getName()
                                  .startsWith(FILENAME));
        assertEquals(PAYLOAD.length(), storageMetadata.getSize());
        assertTrue(storageMetadata.getETag()
                                  .startsWith(FILENAME));
        assertEquals(getUserMetadata(), storageMetadata.getUserMetadata());
    });
}
 
Example #2
Source File: OSSUtils.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
/**
 * 查看某个路径下的文件所占用的资源的大小
 * @param filePath 要查看文件的路径,如 file/image/
 * @return 单位:B
 */
public long getFolderSize(String filePath){
	ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
	listObjectsRequest.setPrefix(filePath); 
	listObjectsRequest.setMaxKeys(1000);
	
	boolean have = true;		//是否有下一页
	String nextMarker = null;
	int size = 0;		//总字节大小,单位:B
	while(have){
		if(nextMarker != null){
			listObjectsRequest.setMarker(nextMarker);
		}
		ObjectListing listO = getOSSClient().listObjects(listObjectsRequest);
		
	    for (OSSObjectSummary objectSummary : listO.getObjectSummaries()) {
	        size += objectSummary.getSize();  
	    }
	    
	    have = listO.isTruncated();
	    nextMarker = listO.getNextMarker();
	}
	return size;
}
 
Example #3
Source File: OSSUtils.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
/**
 * 获取 指定目录下的所有文件对象
 * @param filePath 要查看文件的路径,如 file/image/
 * @return {@link List}
 */
public List<OSSObjectSummary> getFolderObjectList(String filePath){
	ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
	listObjectsRequest.setPrefix(filePath); 
	listObjectsRequest.setMaxKeys(1000);
	List<OSSObjectSummary> list = new ArrayList<OSSObjectSummary>();
	
	boolean have = true;		//是否有下一页
	String nextMarker = null;
	while(have){
		if(nextMarker != null){
			listObjectsRequest.setMarker(nextMarker);
		}
		ObjectListing listO = getOSSClient().listObjects(listObjectsRequest);
		
	    for (OSSObjectSummary objectSummary : listO.getObjectSummaries()) {
	    	list.add(objectSummary);
	    }
	    
	    have = listO.isTruncated();
	    nextMarker = listO.getNextMarker();
	}
	return list;
}
 
Example #4
Source File: OSSUtil.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
/**
 * 查看某个路径下的文件所占用的资源的大小
 * @param filePath 要查看文件的路径,如 file/image/
 * @return 单位:B
 */
public static long getFolderSize(String filePath){
	ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
	listObjectsRequest.setPrefix(filePath); 
	listObjectsRequest.setMaxKeys(1000);
	
	boolean have = true;		//是否有下一页
	String nextMarker = null;
	int size = 0;		//总字节大小,单位:B
	while(have){
		if(nextMarker != null){
			listObjectsRequest.setMarker(nextMarker);
		}
		ObjectListing listO = OSSUtil.getOSSClient().listObjects(listObjectsRequest);
		
	    for (OSSObjectSummary objectSummary : listO.getObjectSummaries()) {
	        size += objectSummary.getSize();  
	    }
	    
	    have = listO.isTruncated();
	    nextMarker = listO.getNextMarker();
	}
	return size;
}
 
Example #5
Source File: OSSUtil.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
/**
 * 获取 指定目录下的所有文件对象
 * @param filePath 要查看文件的路径,如 file/image/
 * @return {@link List}
 */
public static List<OSSObjectSummary> getFolderObjectList(String filePath){
	ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
	listObjectsRequest.setPrefix(filePath); 
	listObjectsRequest.setMaxKeys(1000);
	List<OSSObjectSummary> list = new ArrayList<OSSObjectSummary>();
	
	boolean have = true;		//是否有下一页
	String nextMarker = null;
	while(have){
		if(nextMarker != null){
			listObjectsRequest.setMarker(nextMarker);
		}
		ObjectListing listO = OSSUtil.getOSSClient().listObjects(listObjectsRequest);
		
	    for (OSSObjectSummary objectSummary : listO.getObjectSummaries()) {
	    	list.add(objectSummary);
	    }
	    
	    have = listO.isTruncated();
	    nextMarker = listO.getNextMarker();
	}
	return list;
}
 
Example #6
Source File: OSSNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Override
public void remove(String folderPath, AuthenticationInfo subject) {
  String nextMarker = null;
  ObjectListing objectListing = null;
  do {
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName)
            .withPrefix(rootFolder + folderPath + "/")
            .withMarker(nextMarker);
    objectListing = ossClient.listObjects(listObjectsRequest);
    if (objectListing.getObjectSummaries().size() > 0) {
      List<String> keys = new ArrayList();
      for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
        keys.add(s.getKey());
      }
      DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(keys);
      ossClient.deleteObjects(deleteObjectsRequest);
    }

    nextMarker = objectListing.getNextMarker();
  } while (objectListing.isTruncated());
}
 
Example #7
Source File: OSSUtil.java    From anyline with Apache License 2.0 5 votes vote down vote up
/** 
 * 下载prefix目录下的所有文件到本地dir目录 
 * @param dir  dir
 * @param prefix  prefix
 * @return return
 */ 
public boolean download(File dir, String prefix){ 
	if(null == prefix){ 
		prefix = ""; 
	} 
	if(prefix.startsWith("/")){ 
		prefix = prefix.substring(1); 
	} 
	final int maxKeys = 200; 
	String nextMarker = null; 
	ObjectListing objectListing; 
	do { 
	    objectListing = client.listObjects(new ListObjectsRequest(config.BUCKET) 
	    .withPrefix(prefix).withMarker(nextMarker).withMaxKeys(maxKeys)); 
	    List<OSSObjectSummary> sums = objectListing.getObjectSummaries(); 
	    for (OSSObjectSummary s : sums) { 
	    	String key = s.getKey(); 
	    	if(key.endsWith("/")){ 
	    		continue; 
	    	} 
	        File file = new File(dir, key); 
	        File parent = file.getParentFile(); 
	        if(null != parent && !parent.exists()){ 
	        	parent.mkdirs(); 
	        } 
	        try{ 
	        	client.getObject(new GetObjectRequest(config.BUCKET, key), file); 
	        }catch(Exception e){ 
	        	e.printStackTrace(); 
	        } 
	        if(ConfigTable.isDebug() && log.isWarnEnabled()){ 
	        	log.warn("[oss download file][local:{}][remote:{}]",file.getAbsolutePath(),key); 
	        } 
	    } 
	    nextMarker = objectListing.getNextMarker(); 
	     
	} while (objectListing.isTruncated()); 

	return true; 
}
 
Example #8
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@Override
public PageSet<? extends StorageMetadata> list(String container, ListContainerOptions options) {
    return doOssOperation(oss -> {
        ListObjectsRequest request = toListObjectRequest(container, options);
        ObjectListing objectListing = oss.listObjects(request);
        List<StorageMetadata> storageMetadataList = toStorageMetadataList(oss, container, objectListing);
        return new PageSetImpl<>(storageMetadataList, objectListing.getNextMarker());
    });
}
 
Example #9
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private ListObjectsRequest toListObjectRequest(String container, ListContainerOptions options) {
    ListObjectsRequest request = new ListObjectsRequest(container);
    if (options.getMaxResults() != null) {
        request.setMaxKeys(options.getMaxResults());
    }
    if (options.getMarker() != null) {
        request.setMarker(options.getMarker());
    }
    return request;
}
 
Example #10
Source File: OSSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, NoteInfo> list(AuthenticationInfo subject) throws IOException {
  Map<String, NoteInfo> notesInfo = new HashMap<>();
  final int maxKeys = 200;
  String nextMarker = null;
  ObjectListing objectListing = null;
  do {
    objectListing = ossClient.listObjects(
            new ListObjectsRequest(bucketName)
                    .withPrefix(rootFolder + "/")
                    .withMarker(nextMarker)
                    .withMaxKeys(maxKeys));
    List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
    for (OSSObjectSummary s : sums) {
      if (s.getKey().endsWith(".zpln")) {
        try {
          String noteId = getNoteId(s.getKey());
          String notePath = getNotePath(rootFolder, s.getKey());
          notesInfo.put(noteId, new NoteInfo(noteId, notePath));
        } catch (IOException e) {
          LOGGER.warn(e.getMessage());
        }
      } else {
        LOGGER.debug("Skip invalid note file: " + s.getKey());
      }
    }
    nextMarker = objectListing.getNextMarker();
  } while (objectListing.isTruncated());

  return notesInfo;
}
 
Example #11
Source File: OSSNotebookRepo.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void move(String folderPath, String newFolderPath, AuthenticationInfo subject) {
  final int maxKeys = 200;
  String nextMarker = null;
  ObjectListing objectListing = null;
  do {
    objectListing = ossClient.listObjects(
            new ListObjectsRequest(bucketName)
                    .withPrefix(rootFolder + folderPath + "/")
                    .withMarker(nextMarker)
                    .withMaxKeys(maxKeys));
    List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
    for (OSSObjectSummary s : sums) {
      if (s.getKey().endsWith(".zpln")) {
        try {
          String noteId = getNoteId(s.getKey());
          String notePath = getNotePath(rootFolder, s.getKey());
          String newNotePath = newFolderPath + notePath.substring(folderPath.length());
          move(noteId, notePath, newNotePath, subject);
        } catch (IOException e) {
          LOGGER.warn(e.getMessage());
        }
      } else {
        LOGGER.debug("Skip invalid note file: " + s.getKey());
      }
    }
    nextMarker = objectListing.getNextMarker();
  } while (objectListing.isTruncated());

}