Skip to content

Commit 61e0ffd

Browse files
authored
Merge pull request #69 from aliyun/get-bucket-stat
get bucket stat
2 parents 36b2360 + 4a36fc4 commit 61e0ffd

31 files changed

+566
-57
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>com.aliyun.oss</groupId>
1313
<artifactId>aliyun-sdk-oss</artifactId>
14-
<version>2.4.1</version>
14+
<version>2.5.0-SNAPSHOT</version>
1515
<packaging>jar</packaging>
1616
<name>Aliyun OSS SDK for Java</name>
1717
<description>The Aliyun OSS SDK for Java used for accessing Aliyun Object Storage Service</description>

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.aliyun.oss.model.BucketProcess;
4040
import com.aliyun.oss.model.BucketReferer;
4141
import com.aliyun.oss.model.BucketReplicationProgress;
42+
import com.aliyun.oss.model.BucketStat;
4243
import com.aliyun.oss.model.BucketWebsiteResult;
4344
import com.aliyun.oss.model.CannedAccessControlList;
4445
import com.aliyun.oss.model.CnameConfiguration;
@@ -1398,6 +1399,26 @@ public BucketInfo getBucketInfo(String bucketName)
13981399
public BucketInfo getBucketInfo(GenericRequest genericRequest)
13991400
throws OSSException, ClientException;
14001401

1402+
/**
1403+
* 获取{@link Bucket}的存储信息。
1404+
* @param bucketName 指定Bucket名称。
1405+
* @return Bucket信息。
1406+
* @throws OSSException OSS Server异常信息。
1407+
* @throws ClientException OSS Client异常信息。
1408+
*/
1409+
public BucketStat getBucketStat(String bucketName)
1410+
throws OSSException, ClientException;
1411+
1412+
/**
1413+
* 获取{@link Bucket}的存储信息。
1414+
* @param genericRequest 请求信息。
1415+
* @return Bucket信息。
1416+
* @throws OSSException OSS Server异常信息。
1417+
* @throws ClientException OSS Client异常信息。
1418+
*/
1419+
public BucketStat getBucketStat(GenericRequest genericRequest)
1420+
throws OSSException, ClientException;
1421+
14011422
/**
14021423
* 设置{@link Bucket}的容量。
14031424
* @param bucketName 指定Bucket名称。
@@ -1661,7 +1682,7 @@ public String generateRtmpUri(GenerateRtmpUriRequest generatePushflowUrlRequest)
16611682
throws OSSException, ClientException;
16621683

16631684
/**
1664-
1685+
* 创建符号链接。
16651686
* @param bucketName Bucket名称。
16661687
* @param symlink 符号链接。
16671688
* @param target 目标文件。

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.aliyun.oss.model.BucketProcess;
8787
import com.aliyun.oss.model.BucketReferer;
8888
import com.aliyun.oss.model.BucketReplicationProgress;
89+
import com.aliyun.oss.model.BucketStat;
8990
import com.aliyun.oss.model.BucketWebsiteResult;
9091
import com.aliyun.oss.model.CannedAccessControlList;
9192
import com.aliyun.oss.model.CnameConfiguration;
@@ -1271,6 +1272,18 @@ public BucketInfo getBucketInfo(GenericRequest genericRequest)
12711272
return this.bucketOperation.getBucketInfo(genericRequest);
12721273
}
12731274

1275+
@Override
1276+
public BucketStat getBucketStat(String bucketName)
1277+
throws OSSException, ClientException {
1278+
return this.getBucketStat(new GenericRequest(bucketName));
1279+
}
1280+
1281+
@Override
1282+
public BucketStat getBucketStat(GenericRequest genericRequest)
1283+
throws OSSException, ClientException {
1284+
return this.bucketOperation.getBucketStat(genericRequest);
1285+
}
1286+
12741287
@Override
12751288
public void setBucketStorageCapacity(String bucketName, UserQos userQos) throws OSSException,
12761289
ClientException {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import static com.aliyun.oss.internal.ResponseParsers.getBucketReplicationLocationResponseParser;
6565
import static com.aliyun.oss.internal.ResponseParsers.getBucketCnameResponseParser;
6666
import static com.aliyun.oss.internal.ResponseParsers.getBucketInfoResponseParser;
67+
import static com.aliyun.oss.internal.ResponseParsers.getBucketStatResponseParser;
6768
import static com.aliyun.oss.internal.ResponseParsers.getBucketQosResponseParser;
6869
import static com.aliyun.oss.internal.ResponseParsers.listBucketResponseParser;
6970
import static com.aliyun.oss.internal.ResponseParsers.listObjectsReponseParser;
@@ -95,6 +96,7 @@
9596
import com.aliyun.oss.model.BucketProcess;
9697
import com.aliyun.oss.model.BucketReferer;
9798
import com.aliyun.oss.model.BucketReplicationProgress;
99+
import com.aliyun.oss.model.BucketStat;
98100
import com.aliyun.oss.model.BucketWebsiteResult;
99101
import com.aliyun.oss.model.CannedAccessControlList;
100102
import com.aliyun.oss.model.CnameConfiguration;
@@ -1211,6 +1213,29 @@ public BucketInfo getBucketInfo(GenericRequest genericRequest)
12111213
return doOperation(request, getBucketInfoResponseParser, bucketName, null, true);
12121214
}
12131215

1216+
public BucketStat getBucketStat(GenericRequest genericRequest)
1217+
throws OSSException, ClientException {
1218+
1219+
assertParameterNotNull(genericRequest, "genericRequest");
1220+
1221+
String bucketName = genericRequest.getBucketName();
1222+
assertParameterNotNull(bucketName, "bucketName");
1223+
ensureBucketNameValid(bucketName);
1224+
1225+
Map<String, String> params = new HashMap<String, String>();
1226+
params.put(RequestParameters.SUBRESOURCE_STAT, null);
1227+
1228+
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient())
1229+
.setEndpoint(getEndpoint())
1230+
.setMethod(HttpMethod.GET)
1231+
.setBucket(bucketName)
1232+
.setParameters(params)
1233+
.setOriginalRequest(genericRequest)
1234+
.build();
1235+
1236+
return doOperation(request, getBucketStatResponseParser, bucketName, null, true);
1237+
}
1238+
12141239
public void setBucketStorageCapacity(SetBucketStorageCapacityRequest setBucketStorageCapacityRequest)
12151240
throws OSSException, ClientException {
12161241

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

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727

2828
import java.io.File;
2929
import java.io.FileInputStream;
30+
import java.io.FileNotFoundException;
3031
import java.io.FileOutputStream;
3132
import java.io.IOException;
3233
import java.io.InputStream;
3334
import java.io.ObjectInputStream;
3435
import java.io.ObjectOutputStream;
36+
import java.io.OutputStream;
3537
import java.io.RandomAccessFile;
3638
import java.io.Serializable;
3739
import java.util.ArrayList;
@@ -341,6 +343,9 @@ private DownloadFileResult downloadFileWithCheckpoint(DownloadFileRequest downlo
341343
}
342344
}
343345

346+
// 重命名临时文件
347+
renameTo(downloadFileRequest.getTempDownloadFile(), downloadFileRequest.getDownloadFile());
348+
344349
// 开启了断点下载,成功上传后删除checkpoint文件
345350
if (downloadFileRequest.isEnableCheckpoint()) {
346351
remove(downloadFileRequest.getCheckpointFile());
@@ -360,7 +365,7 @@ private void prepare(DownloadCheckPoint downloadCheckPoint, DownloadFileRequest
360365
downloadCheckPoint.downloadParts = splitFile(downloadCheckPoint.objectStat.size,
361366
downloadFileRequest.getPartSize());
362367

363-
createFixedFile(downloadFileRequest.getDownloadFile(), downloadCheckPoint.objectStat.size);
368+
createFixedFile(downloadFileRequest.getTempDownloadFile(), downloadCheckPoint.objectStat.size);
364369
}
365370

366371
public static void createFixedFile(String filePath, long length) throws IOException {
@@ -445,7 +450,7 @@ public PartResult call() throws Exception {
445450
DownloadPart downloadPart = downloadCheckPoint.downloadParts.get(partIndex);
446451
tr = new PartResult(partIndex + 1, downloadPart.start, downloadPart.end);
447452

448-
output = new RandomAccessFile(downloadFileRequest.getDownloadFile(), "rw");
453+
output = new RandomAccessFile(downloadFileRequest.getTempDownloadFile(), "rw");
449454
output.seek(downloadPart.start);
450455

451456
GetObjectRequest getObjectRequest = new GetObjectRequest(downloadFileRequest.getBucketName(),
@@ -539,5 +544,59 @@ private boolean remove(String filePath) {
539544
return flag;
540545
}
541546

547+
private static void renameTo(String srcFilePath, String destFilePath) throws IOException {
548+
File srcfile =new File(srcFilePath);
549+
File destfile =new File(destFilePath);
550+
moveFile(srcfile, destfile);
551+
}
552+
553+
private static void moveFile(final File srcFile, final File destFile) throws IOException {
554+
if (srcFile == null) {
555+
throw new NullPointerException("Source must not be null");
556+
}
557+
if (destFile == null) {
558+
throw new NullPointerException("Destination must not be null");
559+
}
560+
if (!srcFile.exists()) {
561+
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
562+
}
563+
if (srcFile.isDirectory()) {
564+
throw new IOException("Source '" + srcFile + "' is a directory");
565+
}
566+
if (destFile.isDirectory()) {
567+
throw new IOException("Destination '" + destFile + "' is a directory");
568+
}
569+
if (destFile.exists()) {
570+
if (!destFile.delete()) {
571+
throw new IOException("Failed to delete original file '" + srcFile + "'");
572+
}
573+
}
574+
575+
final boolean rename = srcFile.renameTo(destFile);
576+
if (!rename) {
577+
copyFile(srcFile, destFile);
578+
if (!srcFile.delete()) {
579+
throw new IOException("Failed to delete original file '" + srcFile + "' after copy to '" + destFile + "'");
580+
}
581+
}
582+
}
583+
584+
private static void copyFile(File source, File dest) throws IOException {
585+
InputStream is = null;
586+
OutputStream os = null;
587+
try {
588+
is = new FileInputStream(source);
589+
os = new FileOutputStream(dest);
590+
byte[] buffer = new byte[4096];
591+
int length;
592+
while ((length = is.read(buffer)) > 0) {
593+
os.write(buffer, 0, length);
594+
}
595+
} finally {
596+
is.close();
597+
os.close();
598+
}
599+
}
600+
542601
private OSSObjectOperation objectOperation;
543602
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import static com.aliyun.oss.internal.OSSConstants.DEFAULT_FILE_SIZE_LIMIT;
4141
import static com.aliyun.oss.internal.OSSConstants.DEFAULT_CHARSET_NAME;
4242
import static com.aliyun.oss.internal.ResponseParsers.completeMultipartUploadResponseParser;
43-
import static com.aliyun.oss.internal.ResponseParsers.completeMultipartUploadCallbackResponseParser;
43+
import static com.aliyun.oss.internal.ResponseParsers.completeMultipartUploadProcessResponseParser;
4444
import static com.aliyun.oss.internal.ResponseParsers.initiateMultipartUploadResponseParser;
4545
import static com.aliyun.oss.internal.ResponseParsers.listMultipartUploadsResponseParser;
4646
import static com.aliyun.oss.internal.ResponseParsers.listPartsResponseParser;
@@ -181,10 +181,10 @@ public int compare(PartETag p1, PartETag p2) {
181181
reponseHandlers.add(new OSSCallbackErrorResponseHandler());
182182

183183
CompleteMultipartUploadResult result = null;
184-
if (completeMultipartUploadRequest.getCallback() == null) {
184+
if (!isNeedReturnResponse(completeMultipartUploadRequest)) {
185185
result = doOperation(request, completeMultipartUploadResponseParser, bucketName, key, true);
186186
} else {
187-
result = doOperation(request, completeMultipartUploadCallbackResponseParser, bucketName, key, true, null, reponseHandlers);
187+
result = doOperation(request, completeMultipartUploadProcessResponseParser, bucketName, key, true, null, reponseHandlers);
188188
}
189189

190190
result.setClientCRC(calcObjectCRCFromParts(completeMultipartUploadRequest.getPartETags()));
@@ -571,4 +571,11 @@ private static Long calcObjectCRCFromParts(List<PartETag> partETags) {
571571
return new Long(crc);
572572
}
573573

574+
private static boolean isNeedReturnResponse(CompleteMultipartUploadRequest completeMultipartUploadRequest) {
575+
if (completeMultipartUploadRequest.getCallback() != null ||
576+
completeMultipartUploadRequest.getProcess() != null) {
577+
return true;
578+
}
579+
return false;
580+
}
574581
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import static com.aliyun.oss.internal.ResponseParsers.getObjectAclResponseParser;
5656
import static com.aliyun.oss.internal.ResponseParsers.getObjectMetadataResponseParser;
5757
import static com.aliyun.oss.internal.ResponseParsers.putObjectReponseParser;
58-
import static com.aliyun.oss.internal.ResponseParsers.putObjectCallbackReponseParser;
58+
import static com.aliyun.oss.internal.ResponseParsers.putObjectProcessReponseParser;
5959
import static com.aliyun.oss.internal.ResponseParsers.getSimplifiedObjectMetaResponseParser;
6060
import static com.aliyun.oss.internal.ResponseParsers.getSymbolicLinkResponseParser;
6161

@@ -141,10 +141,10 @@ public PutObjectResult putObject(PutObjectRequest putObjectRequest)
141141

142142
PutObjectResult result = null;
143143

144-
if (putObjectRequest.getCallback() == null) {
144+
if (!isNeedReturnResponse(putObjectRequest)) {
145145
result = writeObjectInternal(WriteMode.OVERWRITE, putObjectRequest, putObjectReponseParser);
146146
} else {
147-
result = writeObjectInternal(WriteMode.OVERWRITE, putObjectRequest, putObjectCallbackReponseParser);
147+
result = writeObjectInternal(WriteMode.OVERWRITE, putObjectRequest, putObjectProcessReponseParser);
148148
}
149149

150150
if (isCrcCheckEnabled()) {
@@ -180,7 +180,7 @@ public PutObjectResult putObject(URL signedUrl, InputStream requestContent, long
180180
if (requestHeaders.get(OSSHeaders.OSS_HEADER_CALLBACK) == null) {
181181
result = doOperation(request, putObjectReponseParser, null, null, true);
182182
} else {
183-
result = doOperation(request, putObjectCallbackReponseParser, null, null, true);
183+
result = doOperation(request, putObjectProcessReponseParser, null, null, true);
184184
}
185185

186186
if (isCrcCheckEnabled()) {
@@ -899,4 +899,12 @@ private static void populateWriteObjectParams(WriteMode mode, PutObjectRequest o
899899
params.put(RequestParameters.POSITION, String.valueOf(appendObjectRequest.getPosition()));
900900
}
901901
}
902+
903+
private static boolean isNeedReturnResponse(PutObjectRequest putObjectRequest) {
904+
if (putObjectRequest.getCallback() != null || putObjectRequest.getProcess() != null) {
905+
return true;
906+
}
907+
return false;
908+
}
909+
902910
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, St
152152
return parser.parse(response);
153153
} catch (ResponseParseException rpe) {
154154
OSSException oe = ExceptionFactory.createInvalidResponseException(
155-
response.getRequestId(), response.getErrorResponseAsString(), rpe);
156-
logException("Unable to parse response error: ", oe);
155+
response.getRequestId(), rpe.getMessage(), rpe);
156+
logException("Unable to parse response error: ", rpe);
157157
throw oe;
158158
}
159159
}

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void ensureBucketNameValid(String bucketName) {
8282
*/
8383
public static boolean validateObjectKey(String key) {
8484

85-
if (key == null) {
85+
if (key == null || key.length() == 0) {
8686
return false;
8787
}
8888

@@ -222,22 +222,26 @@ public static void populateRequestMetadata(Map<String, String> headers, ObjectMe
222222
Map<String, Object> rawMetadata = metadata.getRawMetadata();
223223
if (rawMetadata != null) {
224224
for (Entry<String, Object> entry : rawMetadata.entrySet()) {
225-
String key = entry.getKey();
226-
String value = entry.getValue().toString();
227-
if (key != null) key = key.trim();
228-
if (value != null) value = value.trim();
229-
headers.put(key, value);
225+
if (entry.getKey() != null && entry.getValue() != null) {
226+
String key = entry.getKey();
227+
String value = entry.getValue().toString();
228+
if (key != null) key = key.trim();
229+
if (value != null) value = value.trim();
230+
headers.put(key, value);
231+
}
230232
}
231233
}
232234

233235
Map<String, String> userMetadata = metadata.getUserMetadata();
234236
if (userMetadata != null) {
235237
for (Entry<String, String> entry : userMetadata.entrySet()) {
236-
String key = entry.getKey();
237-
String value = entry.getValue();
238-
if (key != null) key = key.trim();
239-
if (value != null) value = value.trim();
240-
headers.put(OSSHeaders.OSS_USER_METADATA_PREFIX + key, value);
238+
if (entry.getKey() != null && entry.getValue() != null) {
239+
String key = entry.getKey();
240+
String value = entry.getValue();
241+
if (key != null) key = key.trim();
242+
if (value != null) value = value.trim();
243+
headers.put(OSSHeaders.OSS_USER_METADATA_PREFIX + key, value);
244+
}
241245
}
242246
}
243247
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public final class RequestParameters {
5050
public static final String SUBRESOURCE_PROCESS_CONF = "processConfiguration";
5151
public static final String SUBRESOURCE_PROCESS = "x-oss-process";
5252
public static final String SUBRESOURCE_SYMLINK = "symlink";
53+
public static final String SUBRESOURCE_STAT = "stat";
5354

5455
public static final String PREFIX = "prefix";
5556
public static final String DELIMITER = "delimiter";

0 commit comments

Comments
 (0)