|
@@ -10,13 +10,17 @@ import org.junit.Test;
|
|
|
import java.io.File;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.URL;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
public class BOSClientUtil {
|
|
|
- private final static String accessKey= Global.getConfig("bos_access_key");
|
|
|
- private final static String secretKey= Global.getConfig("bos_secret_key");
|
|
|
- private final static String endpoint= Global.getConfig("bos_endpoint");
|
|
|
- private final static String bucketName= Global.getConfig("bos_buck_name");
|
|
|
- private static BosClient bosClient;
|
|
|
+ private final String accessKey= Global.getConfig("bos_access_key");
|
|
|
+ private final String secretKey= Global.getConfig("bos_secret_key");
|
|
|
+ private final String endpoint= Global.getConfig("bos_endpoint");
|
|
|
+ private final String bucketName= Global.getConfig("bos_buck_name");
|
|
|
+ private final String urlTem = "https://BUCKNAME.su.bcebos.comKEY";
|
|
|
+ private BosClient bosClient;
|
|
|
+
|
|
|
|
|
|
public BOSClientUtil() {
|
|
|
BosClientConfiguration config = new BosClientConfiguration();
|
|
@@ -30,7 +34,7 @@ public class BOSClientUtil {
|
|
|
* 获取BosClient对象
|
|
|
* @return
|
|
|
*/
|
|
|
- public static BosClient getBosClient() {
|
|
|
+ public BosClient getBosClient() {
|
|
|
BosClientConfiguration config = new BosClientConfiguration();
|
|
|
config.setMaxConnections(10);
|
|
|
config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
|
|
@@ -44,7 +48,7 @@ public class BOSClientUtil {
|
|
|
* @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
|
|
|
* @return
|
|
|
*/
|
|
|
- public static PutObjectResponse uploadFileToBos(File file,
|
|
|
+ public PutObjectResponse uploadFileToBos(File file,
|
|
|
String objectKey) {
|
|
|
return bosClient.putObject(bucketName, objectKey, file);
|
|
|
}
|
|
@@ -55,7 +59,7 @@ public class BOSClientUtil {
|
|
|
* @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
|
|
|
* @return
|
|
|
*/
|
|
|
- public static PutObjectResponse uploadInputStreamToBos(InputStream inputStream,
|
|
|
+ public PutObjectResponse uploadInputStreamToBos(InputStream inputStream,
|
|
|
String objectKey) {
|
|
|
return bosClient.putObject(bucketName, objectKey, inputStream);
|
|
|
}
|
|
@@ -67,7 +71,7 @@ public class BOSClientUtil {
|
|
|
* @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
|
|
|
* @return 上传成功后的tag
|
|
|
*/
|
|
|
- public static void deleteObject(String objectKey) {
|
|
|
+ public void deleteObject(String objectKey) {
|
|
|
bosClient.deleteObject(bucketName, objectKey);
|
|
|
}
|
|
|
|
|
@@ -77,45 +81,96 @@ public class BOSClientUtil {
|
|
|
* @param objectKey 文件夹和文件名
|
|
|
* @return 目标文件的下载url
|
|
|
*/
|
|
|
- public static String generatePresignedUrl(String objectKey) {
|
|
|
+ public String generatePresignedUrl(String objectKey) {
|
|
|
URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
|
|
|
return url.toString();
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static String upload(String path,File file){
|
|
|
+ public String upload(String path,File file){
|
|
|
String objectKey = path+file.getName();
|
|
|
bosClient.putObject(bucketName, objectKey, file);
|
|
|
- URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
|
|
|
- return url.toString();
|
|
|
+ String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",objectKey);
|
|
|
+// URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
|
|
|
+ return url;
|
|
|
}
|
|
|
|
|
|
- public static String upload(String path,InputStream inputStream){
|
|
|
- String objectKey = path;
|
|
|
- bosClient.putObject(bucketName, objectKey, inputStream);
|
|
|
- URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
|
|
|
- return url.toString();
|
|
|
+ public String upload(String path,InputStream inputStream){
|
|
|
+ bosClient.putObject(bucketName, path, inputStream);
|
|
|
+ String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",path);
|
|
|
+// URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
|
|
|
+ return url;
|
|
|
}
|
|
|
|
|
|
+// @Test
|
|
|
+// public void testPut(){
|
|
|
+// File file = new File("E:/bk1.png");
|
|
|
+// String name = file.getName();
|
|
|
+// BOSClientUtil.uploadFileToBos(file,"/test/"+name);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void testDel(){
|
|
|
+// BOSClientUtil.deleteObject("/test/1.txt");
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Test
|
|
|
+// public void testUrl(){
|
|
|
+// File file = new File("E:/bk1.png");
|
|
|
+// String upload = BOSClientUtil.upload("/text1/", file);
|
|
|
+// System.out.println(upload);
|
|
|
+// }
|
|
|
+
|
|
|
@Test
|
|
|
- public void testPut(){
|
|
|
- File file = new File("E:/bk1.png");
|
|
|
- String name = file.getName();
|
|
|
- BOSClientUtil.uploadFileToBos(file,"/test/"+name);
|
|
|
+ public void uploadAll(){
|
|
|
+ ArrayList<String> listFileName = new ArrayList<String>();
|
|
|
+ String filePath = "D:/IDEA workspace/total_process/src/main/webapp/static/";
|
|
|
+ getAllFileName(filePath,listFileName);
|
|
|
+ int count = 1;
|
|
|
+ int total = listFileName.size();
|
|
|
+ System.out.println(total);
|
|
|
+ for(String name : listFileName){
|
|
|
+ if(name != null) {
|
|
|
+ int statice = name.indexOf("static");
|
|
|
+ String name1 = name.substring(statice + 6, name.length());
|
|
|
+ File file = new File(name);
|
|
|
+// System.out.println("/static"+name1);
|
|
|
+ bosClient.putObject(bucketName, "/static"+name1, file);
|
|
|
+// BOSClientUtil.upload("/static"+name1,file);
|
|
|
+ System.out.println("---------正在上传 "+file.getName()+"-------");
|
|
|
+ System.out.println("上传第"+count+"个资源,共"+total+"个资源");
|
|
|
+ count++;
|
|
|
+// try {
|
|
|
+// bosClient.deleteObject(bucketName, " /static" + name1);
|
|
|
+// }catch (Exception e){
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Test
|
|
|
- public void testDel(){
|
|
|
- BOSClientUtil.deleteObject("/test/1.txt");
|
|
|
- }
|
|
|
+public void getAllFileName(String path, ArrayList<String> listFileName){
|
|
|
+ File file = new File(path);
|
|
|
+ File [] files = file.listFiles();
|
|
|
+ String [] names = file.list();
|
|
|
+ if(names != null){
|
|
|
+ String [] completNames = new String[names.length];
|
|
|
+ for(int i=0;i<names.length;i++){
|
|
|
+ if(files[i].isFile()) {
|
|
|
+ completNames[i] = path + names[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ listFileName.addAll(Arrays.asList(completNames));
|
|
|
+ }
|
|
|
+ for(File a:files){
|
|
|
+ if(a.isDirectory()){//如果文件夹下有子文件夹,获取子文件夹下的所有文件全路径。
|
|
|
+ getAllFileName(a.getAbsolutePath().replaceAll("\\\\","/")+"/",listFileName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- @Test
|
|
|
- public void testUrl(){
|
|
|
- File file = new File("E:/bk1.png");
|
|
|
- String upload = BOSClientUtil.upload("/text1/", file);
|
|
|
- System.out.println(upload);
|
|
|
- }
|
|
|
|
|
|
|
|
|
}
|