com.aliyun.oss.model.ObjectListing Java Examples

The following examples show how to use com.aliyun.oss.model.ObjectListing. 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: OssCossEndpoint.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public OssObjectListing listObjects(String bucketName, String prefix) {
	OssObjectListing objectList = new OssObjectListing();
	ObjectListing ossObjectListing = ossClient.listObjects(bucketName, prefix);
	objectList.setBucketName(ossObjectListing.getBucketName());
	objectList.setPrefix(ossObjectListing.getPrefix());
	objectList.setEncodingType(ossObjectListing.getEncodingType());
	objectList.setDelimiter(ossObjectListing.getDelimiter());
	objectList.setMarker(ossObjectListing.getMarker());
	objectList.setMaxKeys(ossObjectListing.getMaxKeys());
	objectList.setTruncated(ossObjectListing.isTruncated());
	objectList.setNextMarker(ossObjectListing.getNextMarker());
	objectList.setCommonPrefixes(ossObjectListing.getCommonPrefixes());
	return objectList;
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: OSSInterface.java    From charging_pile_cloud with MIT License 5 votes vote down vote up
/**
 * 查看某个存储区域的所有文件
 *
 * @param bucketName 要查看的存储区域
 * @return
 */
public List<OSSObjectSummary> findObject(String bucketName) {
    if (bucketName == null || bucketName.equals("")) {
        bucketName = bucketName_user;
    }
    ObjectListing objectListing = ossClient.listObjects(bucketName);
    List<OSSObjectSummary> list = objectListing.getObjectSummaries();
    ossClient.shutdown();
    return list;
}
 
Example #10
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 #11
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 #12
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());

}
 
Example #13
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
private List<StorageMetadata> toStorageMetadataList(OSS oss, String container, ObjectListing objectListing) {
    return objectListing.getObjectSummaries()
                        .stream()
                        .map(ossObjectSummary -> toStorageMetadata(oss, container, ossObjectSummary))
                        .collect(Collectors.toList());
}