Skip to content

Commit 904a357

Browse files
authored
Merge pull request #71 from iminright/linebreak
fix oss download bug
2 parents 75a2220 + faf52c6 commit 904a357

File tree

4 files changed

+77
-32
lines changed

4 files changed

+77
-32
lines changed

rdf-file-oss/src/main/java/com/alipay/rdf/file/storage/FileOssStorage.java

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
package com.alipay.rdf.file.storage;
22

3-
import java.io.ByteArrayInputStream;
4-
import java.io.File;
5-
import java.io.FileInputStream;
6-
import java.io.FileNotFoundException;
7-
import java.io.IOException;
8-
import java.io.InputStream;
9-
import java.util.ArrayList;
10-
import java.util.List;
11-
import java.util.regex.Pattern;
12-
133
import com.alipay.rdf.file.exception.RdfErrorEnum;
144
import com.alipay.rdf.file.exception.RdfFileException;
155
import com.alipay.rdf.file.interfaces.FileFactory;
@@ -25,24 +15,12 @@
2515
import com.aliyun.oss.OSSClient;
2616
import com.aliyun.oss.OSSErrorCode;
2717
import com.aliyun.oss.OSSException;
28-
import com.aliyun.oss.model.AppendObjectRequest;
29-
import com.aliyun.oss.model.AppendObjectResult;
30-
import com.aliyun.oss.model.CompleteMultipartUploadRequest;
31-
import com.aliyun.oss.model.CompleteMultipartUploadResult;
32-
import com.aliyun.oss.model.CopyObjectResult;
33-
import com.aliyun.oss.model.GetObjectRequest;
34-
import com.aliyun.oss.model.InitiateMultipartUploadRequest;
35-
import com.aliyun.oss.model.InitiateMultipartUploadResult;
36-
import com.aliyun.oss.model.ListObjectsRequest;
37-
import com.aliyun.oss.model.OSSObject;
38-
import com.aliyun.oss.model.OSSObjectSummary;
39-
import com.aliyun.oss.model.ObjectListing;
40-
import com.aliyun.oss.model.ObjectMetadata;
41-
import com.aliyun.oss.model.PartETag;
42-
import com.aliyun.oss.model.PutObjectResult;
43-
import com.aliyun.oss.model.UploadFileRequest;
44-
import com.aliyun.oss.model.UploadPartCopyRequest;
45-
import com.aliyun.oss.model.UploadPartCopyResult;
18+
import com.aliyun.oss.model.*;
19+
20+
import java.io.*;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.regex.Pattern;
4624

4725
/**
4826
* Copyright (C) 2013-2018 Ant Financial Services Group
@@ -232,6 +210,7 @@ public List<String> listFiles(String folderName, FilePathFilter... fileFilters)
232210
* helper method for adding FilePathFilter check when listing files
233211
*
234212
* @param folderName
213+
* @param all
235214
* @param fileFilters
236215
* @return
237216
*/
@@ -276,11 +255,15 @@ public List<String> listAllFiles(String folderName, FilePathFilter... fileFilter
276255
public void download(String srcOSSPath, String toFile) {
277256
srcOSSPath = toOSSPath(srcOSSPath);
278257
List<String> fileNames = listAllFiles(srcOSSPath);
279-
if (isExist(srcOSSPath)) {
258+
if(fileNames.isEmpty()){
259+
// 以下两种情况需要抛出异常:
260+
// 1.单个文件场景且文件未找到
261+
// 2.文件夹场景但文件夹下没有任何文件(可能是文件夹名称错误也可能是事实上该文件夹下就没有文件,因为oss无法区分这两种情况)
262+
if(!isExist(srcOSSPath)){
263+
throw new RdfFileException("rdf-file# oss donwLoad srcOSSPath=" + srcOSSPath + " 不存在",
264+
RdfErrorEnum.NOT_EXSIT);
265+
}
280266
fileNames.add(srcOSSPath);
281-
} else {
282-
throw new RdfFileException("rdf-file# oss donwLoad srcOSSPath=" + srcOSSPath + " 不存在",
283-
RdfErrorEnum.NOT_EXSIT);
284267
}
285268
String temp = "";
286269
for (String name : fileNames) {

rdf-file-oss/src/test/java/com/alipay/rdf/file/FileOssStorageTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package com.alipay.rdf.file;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.util.Collections;
56
import java.util.Map;
67
import java.util.WeakHashMap;
78

9+
import com.alipay.rdf.file.exception.RdfErrorEnum;
10+
import com.alipay.rdf.file.exception.RdfFileException;
11+
import com.alipay.rdf.file.util.RdfFileUtil;
12+
import com.alipay.rdf.file.util.TemporaryFolderUtil;
813
import org.junit.After;
14+
import org.junit.Assert;
915
import org.junit.Before;
1016
import org.junit.Test;
1117

@@ -78,6 +84,54 @@ public void testWeakHash() throws Exception {
7884
Thread.sleep(1000);
7985
}
8086

87+
@Test
88+
public void testDownload_file() throws IOException {
89+
// file exists
90+
String src = File.class.getResource("/data/data1.txt").getPath();
91+
String osspath = "rdf/oss/data1.txt";
92+
93+
fileStorage.upload(src, osspath, true);
94+
TemporaryFolderUtil tf = new TemporaryFolderUtil();
95+
tf.create();
96+
String localFilePath = RdfFileUtil.combinePath(tf.getRoot().getAbsolutePath(), "download_data1.txt");
97+
fileStorage.download(osspath, localFilePath);
98+
File localFile = new File(localFilePath);
99+
Assert.assertTrue(localFile.exists());
100+
tf.delete();
101+
102+
// file not exists
103+
try {
104+
fileStorage.download("FileNotExists", localFilePath);
105+
}catch (RdfFileException e){
106+
Assert.assertTrue(e.getErrorEnum() == RdfErrorEnum.NOT_EXSIT);
107+
}
108+
}
109+
110+
@Test
111+
public void testDownload_files() throws IOException {
112+
// dir exists
113+
String src = File.class.getResource("/downloadtestdir").getPath();
114+
String osspath = "rdf/oss/downloadtestdir";
115+
116+
fileStorage.upload(src, osspath, true);
117+
TemporaryFolderUtil tf = new TemporaryFolderUtil();
118+
tf.create();
119+
String localFilePath = tf.getRoot().getAbsolutePath();
120+
fileStorage.download(osspath, localFilePath);
121+
File data1 = new File(RdfFileUtil.combinePath(localFilePath, "data1.txt"));
122+
File data2 = new File(RdfFileUtil.combinePath(localFilePath, "data1.txt"));
123+
Assert.assertTrue(data1.exists());
124+
Assert.assertTrue(data2.exists());
125+
tf.delete();
126+
127+
// dir not exists
128+
try {
129+
fileStorage.download("DirNotExists", localFilePath);
130+
}catch (RdfFileException e){
131+
Assert.assertTrue(e.getErrorEnum() == RdfErrorEnum.NOT_EXSIT);
132+
}
133+
}
134+
81135
@After
82136
public void after() {
83137
fileStorage.delete("rdf/oss/");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
总笔数:100|总金额:300.03
2+
流水号|基金公司订单号|订单申请时间|普通日期|普通日期时间|普通数字|金额|年龄|长整型|布尔值|备注
3+
seq_0|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注1
4+
seq_1|inst_seq_1|2013-11-10 15:56:12|20131110|20131113 12:33:34|23.34|11.88|33|56789|false|
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
总笔数:100|总金额:300.03
2+
流水号|基金公司订单号|订单申请时间|普通日期|普通日期时间|普通数字|金额|年龄|长整型|布尔值|备注
3+
seq_0|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注1
4+
seq_1|inst_seq_1|2013-11-10 15:56:12|20131110|20131113 12:33:34|23.34|11.88|33|56789|false|

0 commit comments

Comments
 (0)