Skip to content

Commit 68ea8cf

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
added file does not have verification
1 parent d0d6511 commit 68ea8cf

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

src/main/java/com/aliyun/oss/common/utils/CodingUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
package com.aliyun.oss.common.utils;
2121

22+
import static com.aliyun.oss.common.utils.IOUtils.checkFile;
2223
import static com.aliyun.oss.internal.OSSUtils.COMMON_RESOURCE_MANAGER;
2324

25+
import java.io.File;
2426
import java.util.List;
2527

2628
/**
@@ -95,4 +97,10 @@ public static boolean checkParamRange(long param, long from, boolean leftInclusi
9597
}
9698
}
9799
}
100+
101+
public static void assertFileExist(String filePath, String key) {
102+
if (!checkFile(new File(filePath))) {
103+
throw new IllegalArgumentException(COMMON_RESOURCE_MANAGER.getFormattedString("FileNotExist", key));
104+
}
105+
}
98106
}

src/main/java/com/aliyun/oss/internal/OSSUploadOperation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package com.aliyun.oss.internal;
2121

22+
import static com.aliyun.oss.common.utils.CodingUtils.assertFileExist;
2223
import static com.aliyun.oss.common.utils.CodingUtils.assertParameterNotNull;
2324
import static com.aliyun.oss.common.utils.LogUtils.logException;
2425
import static com.aliyun.oss.internal.OSSUtils.ensureBucketNameValid;
@@ -331,6 +332,7 @@ public UploadFileResult uploadFile(UploadFileRequest uploadFileRequest) throws T
331332
ensureObjectKeyValid(key);
332333

333334
assertParameterNotNull(uploadFileRequest.getUploadFile(), "uploadFile");
335+
assertFileExist(uploadFileRequest.getUploadFile(), "uploadFile");
334336

335337
// The checkpoint is enabled without specifying the checkpoint file,
336338
// using the default one.

src/main/resources/common.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ FailedToParseResponse=Failed to parse the response result.
44
ParameterIsNull=The parameter "{0}" is null.
55
ParameterStringIsEmpty=The parameter "{0}" is a zero-length string.
66
ParameterIsInvalid=The parameter "{0}" is invalid.
7-
ServerReturnsUnknownError=The server returns an unknown error.
7+
ServerReturnsUnknownError=The server returns an unknown error.
8+
FileNotExist=ObjectKeyInvalid=The parameter "{0}" is invalid, The file in this path does not exist.

src/main/resources/common_zh_CN.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ConnectionError=\u7f51\u7edc\u8fde\u63a5\u9519\u8bef\uff0c\u8be6\u7ec6\u4fe1\u606f\uff1a{0}EncodingFailed=\u7f16\u7801\u5931\u8d25\uff1a {0}FailedToParseResponse=\u8fd4\u56de\u7ed3\u679c\u65e0\u6548\uff0c\u65e0\u6cd5\u89e3\u6790\u3002ParameterIsNull=\u53c2\u6570"{0}"\u4e3a\u7a7a\u6307\u9488\u3002ParameterStringIsEmpty=\u53c2\u6570"{0}"\u662f\u957f\u5ea6\u4e3a0\u7684\u5b57\u7b26\u4e32\u3002ParameterIsInvalid=\u53c2\u6570"{0}"\u65E0\u6548\u3002ServerReturnsUnknownError=\u670d\u52a1\u5668\u8fd4\u56de\u672a\u77e5\u9519\u8bef\u3002
1+
ConnectionError=\u7f51\u7edc\u8fde\u63a5\u9519\u8bef\uff0c\u8be6\u7ec6\u4fe1\u606f\uff1a{0}EncodingFailed=\u7f16\u7801\u5931\u8d25\uff1a {0}FailedToParseResponse=\u8fd4\u56de\u7ed3\u679c\u65e0\u6548\uff0c\u65e0\u6cd5\u89e3\u6790\u3002ParameterIsNull=\u53c2\u6570"{0}"\u4e3a\u7a7a\u6307\u9488\u3002ParameterStringIsEmpty=\u53c2\u6570"{0}"\u662f\u957f\u5ea6\u4e3a0\u7684\u5b57\u7b26\u4e32\u3002ParameterIsInvalid=\u53c2\u6570"{0}"\u65E0\u6548\u3002ServerReturnsUnknownError=\u670d\u52a1\u5668\u8fd4\u56de\u672a\u77e5\u9519\u8bef\u3002FileNotExist=\u53c2\u6570"{0}"\u65E0\u6548\uff0c\u6b64\u8def\u5f84\u4e2d\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u3002

src/test/java/com/aliyun/oss/integrationtests/UploadFileTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,24 @@ public void run() {
518518

519519
}
520520

521+
@Test
522+
public void testUploadFileNotExist() throws Throwable {
523+
String key = "obj-upload-file-not-exist";
524+
String filePath = "Files that do not exist";
525+
526+
try {
527+
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
528+
uploadFileRequest.setUploadFile(filePath);
529+
uploadFileRequest.setTaskNum(5);
530+
uploadFileRequest.setPartSize(100*1024);
531+
532+
ossClient.uploadFile(uploadFileRequest);
533+
Assert.fail("The file does not exist, it should have failed here");
534+
} catch (IllegalArgumentException e) {
535+
Assert.assertEquals("参数\"uploadFile\"无效,此路径中的文件不存在。", e.getMessage());
536+
}
537+
}
538+
521539
public class UploadFileProgressListener implements ProgressListener {
522540
private long bytesWritten = 0;
523541
private long totalBytes = -1;

0 commit comments

Comments
 (0)