查询阿里云存储文件并导出excle 保存到本地

科技探索者 2017-11-13

java OSS 阿里云服务器 aliyun string static 存储

说明:怎么样读取阿里云服务器的文件并导出到本地excle 来统计阿里云存放多少文件呢?下面的代码详细给你介绍:



package util;


import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.RandomAccessFile;

import java.util.List;

import java.util.Map;


import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import jxl.write.biff.RowsExceededException;


import com.aliyun.openservices.ClientConfiguration;

import com.aliyun.openservices.oss.OSSClient;

import com.aliyun.openservices.oss.model.ListObjectsRequest;

import com.aliyun.openservices.oss.model.OSSObject;

import com.aliyun.openservices.oss.model.OSSObjectSummary;

import com.aliyun.openservices.oss.model.ObjectListing;

import com.aliyun.openservices.oss.model.ObjectMetadata;


/**

 * Created by helenon 2017/1/5.

 */

public class Test {

private static final String ACCESS_ID = "";//阿里云服务器ACCESS_ID 

private static final String ACCESS_KEY = "";//阿里云服务器ACCESS_KEY 

private static final String OSS_ENDPOINT = "http://oss.aliyuncs.com/";

private static final String BUCKET_NAME = ";//阿里云服务器BUCKET_NAME 


public static void main(String[] args)  {

pageObject(); 

}

//分页获取所有Object,每页maxKeys条Object。

public static void pageObject() {

// 可以使用ClientConfiguration对象设置代理服务器、最大重试次数等参数。

ClientConfiguration config = new ClientConfiguration();

OSSClient ossClient = new OSSClient(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY, config);

final int maxKeys = 1000;//最大记录

String nextMarker = null;

File file=new File("D:/helen.xls");//excle 存放路径

WritableWorkbook workbook =null;

WritableSheet sheet=null;

        OutputStream os = null;

        ObjectListing listing=null;

    int i = 0;

double sumSize = 0;//总大小

try {

os = new FileOutputStream(file);

workbook = Workbook.createWorkbook(os);

   //创建新的一页

   sheet = workbook.createSheet("First Sheet", 0);

  

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

       do {

       // 构造ListObjectsRequest请求

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(BUCKET_NAME);

listObjectsRequest.setPrefix("helen/view");

listObjectsRequest.setMarker(nextMarker);

listObjectsRequest.setMaxKeys(maxKeys);

listing = ossClient.listObjects(listObjectsRequest);

List<OSSObjectSummary> sums = listing.getObjectSummaries();

for (OSSObjectSummary s : sums) {

try {

String size=getPrintSize(s.getSize());

System.out.println("文件名:"+s.getKey() + "   ---   文件大小:" + size );

i++;

Label title1 = new Label(0,0,"文件名");

sheet.addCell(title1);

Label title2 = new Label(1,0,"文件大小");

   sheet.addCell(title2);

Label filename = new Label(0,i,s.getKey());

sheet.addCell(filename);

Label fileSize = new Label(1,i,size);

       sheet.addCell(fileSize);

double t = s.getSize()/1000000;

sumSize += t;

} catch (RowsExceededException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

System.out.println(sumSize + " M");

nextMarker = listing.getNextMarker();

} while (listing.isTruncated());

       try {

        workbook.write();

workbook.close();

        os.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (WriteException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


       

}

//字节转换

public static String getPrintSize(long size) {  

   //如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义  

   if (size < 1024) {  

       return String.valueOf(size) + "B";  

   } else {  

       size = size / 1024;  

   }  

   //如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位  

   //因为还没有到达要使用另一个单位的时候  

   //接下去以此类推  

   if (size < 1024) {  

       return String.valueOf(size) + "KB";  

   } else {  

       size = size / 1024;  

   }  

   if (size < 1024) {  

       //因为如果以MB为单位的话,要保留最后1位小数,  

       //因此,把此数乘以100之后再取余  

       size = size * 100;  

       return String.valueOf((size / 100)) + "."  

               + String.valueOf((size % 100)) + "MB";  

   } else {  

       //否则如果要以GB为单位的,先除于1024再作同样的处理  

       size = size * 100 / 1024;  

       return String.valueOf((size / 100)) + "."  

               + String.valueOf((size % 100)) + "GB";  

   }  

}  

}

本文转自杨海龙的博客博客51CTO博客,原文链接http://blog.51cto.com/7218743/1906828如需转载请自行联系原作者

IT达仁
登录 后评论
下一篇
云攻略小攻
904人浏览
2019-10-21
相关推荐
Fundebug是这样备份数据的
716人浏览
2018-09-27 10:18:44
示例
431人浏览
2012-11-14 14:45:00
导出SQL运行结果的方法总结
7894人浏览
2016-12-19 10:05:06
0
0
0
901