Skip to content

Commit 98d5626

Browse files
authored
Merge pull request #90 from aliyun/process-object
process object
2 parents a71fd65 + a3dc918 commit 98d5626

24 files changed

+437
-348
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The recommended way to use the Aliyun OSS SDK for Java in your project is to con
2424
<dependency>
2525
<groupId>com.aliyun.oss</groupId>
2626
<artifactId>aliyun-sdk-oss</artifactId>
27-
<version>2.6.1</version>
27+
<version>2.7.0</version>
2828
</dependency>
2929
```
3030

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.6.1</version>
14+
<version>2.7.0</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/ClientConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class ClientConfiguration {
6161
private int maxErrorRetry = DEFAULT_MAX_RETRIES;
6262
private int connectionRequestTimeout = DEFAULT_CONNECTION_REQUEST_TIMEOUT;
6363
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
64-
private int socketTimeout = DEFAULT_CONNECTION_TIMEOUT;
64+
private int socketTimeout = DEFAULT_SOCKET_TIMEOUT;
6565
private int maxConnections = DEFAULT_MAX_CONNECTIONS;
6666
private long connectionTTL = DEFAULT_CONNECTION_TTL;
6767
private boolean useReaper = DEFAULT_USE_REAPER;

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,24 @@ public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsReque
595595
public boolean doesObjectExist(String bucketName, String key)
596596
throws OSSException, ClientException;
597597

598+
/**
599+
* 判断指定的{@link OSSObject}是否存在。
600+
* @param genericRequest
601+
* 请求参数{@link GenericRequest}实例。
602+
* @return
603+
* 如果存在返回True,不存在则返回False。
604+
*/
605+
public boolean doesObjectExist(GenericRequest genericRequest)
606+
throws OSSException, ClientException;
607+
598608
/**
599609
* 判断指定的{@link OSSObject}是否存在。
600610
* @param headObjectRequest
601611
* 请求参数{@link HeadObjectRequest}实例。
602612
* @return
603613
* 如果存在返回True,不存在则返回False。
604614
*/
615+
@Deprecated
605616
public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
606617
throws OSSException, ClientException;
607618

@@ -1649,7 +1660,26 @@ public OSSSymlink getSymlink(String bucketName, String symlink)
16491660
public OSSSymlink getSymlink(GenericRequest genericRequest)
16501661
throws OSSException, ClientException;
16511662

1652-
// UDF
1663+
/**
1664+
* 对指定的Object进行处理。
1665+
* <p>
1666+
* 处理结果{@link GenericResult}实例,使用完之后需要手动关闭释放请求连接,
1667+
* 请调用getResponse().getContent().close()关闭。
1668+
* </p>
1669+
* @param processObjectRequest 请求,包括指定的处理方式process。
1670+
* @return 处理结果{@link GenericResult}实例。使用完之后需要手动关闭释放请求连接。
1671+
* @throws OSSException OSS Server异常信息。
1672+
* @throws ClientException OSS Client异常信息。
1673+
*/
1674+
public GenericResult processObject(ProcessObjectRequest processObjectRequest)
1675+
throws OSSException, ClientException;
1676+
1677+
/**
1678+
* 创建UDF。
1679+
* @param createUdfRequest 请求。
1680+
* @throws OSSException OSS Server异常信息。
1681+
* @throws ClientException OSS Client异常信息。
1682+
*/
16531683
public void createUdf(CreateUdfRequest createUdfRequest)
16541684
throws OSSException, ClientException;
16551685

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -604,26 +604,28 @@ public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsReque
604604
return objectOperation.deleteObjects(deleteObjectsRequest);
605605
}
606606

607-
private void headObject(HeadObjectRequest headObjectRequest)
607+
@Override
608+
public boolean doesObjectExist(String bucketName, String key)
608609
throws OSSException, ClientException {
609-
objectOperation.headObject(headObjectRequest);
610+
return doesObjectExist(new GenericRequest(bucketName, key));
610611
}
611612

613+
@Deprecated
612614
@Override
613-
public boolean doesObjectExist(String bucketName, String key)
615+
public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
614616
throws OSSException, ClientException {
615-
return doesObjectExist(new HeadObjectRequest(bucketName, key));
617+
return doesObjectExist(new GenericRequest(headObjectRequest.getBucketName(), headObjectRequest.getKey()));
616618
}
617619

618620
@Override
619-
public boolean doesObjectExist(HeadObjectRequest headObjectRequest)
621+
public boolean doesObjectExist(GenericRequest genericRequest)
620622
throws OSSException, ClientException {
621623
try {
622-
headObject(headObjectRequest);
624+
getSimplifiedObjectMeta(genericRequest);
623625
return true;
624626
} catch (OSSException e) {
625-
if (e.getErrorCode() == OSSErrorCode.NO_SUCH_BUCKET
626-
|| e.getErrorCode() == OSSErrorCode.NO_SUCH_KEY) {
627+
if (e.getErrorCode().equals(OSSErrorCode.NO_SUCH_BUCKET)
628+
|| e.getErrorCode().equals(OSSErrorCode.NO_SUCH_KEY)) {
627629
return false;
628630
}
629631
throw e;
@@ -1373,6 +1375,12 @@ public OSSSymlink getSymlink(GenericRequest genericRequest)
13731375
return objectOperation.getSymlink(genericRequest);
13741376
}
13751377

1378+
@Override
1379+
public GenericResult processObject(ProcessObjectRequest processObjectRequest)
1380+
throws OSSException, ClientException {
1381+
return this.objectOperation.processObject(processObjectRequest);
1382+
}
1383+
13761384
@Override
13771385
public void createUdf(CreateUdfRequest createUdfRequest)
13781386
throws OSSException, ClientException {

src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.aliyun.oss.ClientException;
3131
import com.aliyun.oss.common.comm.io.FixedLengthInputStream;
3232
import com.aliyun.oss.common.utils.DateUtil;
33+
import com.aliyun.oss.internal.RequestParameters;
3334
import com.aliyun.oss.model.AddBucketReplicationRequest.ReplicationAction;
3435
import com.aliyun.oss.model.BucketReferer;
3536
import com.aliyun.oss.model.CompleteMultipartUploadRequest;
@@ -46,6 +47,7 @@
4647
import com.aliyun.oss.model.LifecycleRule.StorageTransition;
4748
import com.aliyun.oss.model.LiveChannelTarget;
4849
import com.aliyun.oss.model.PartETag;
50+
import com.aliyun.oss.model.ProcessObjectRequest;
4951
import com.aliyun.oss.model.PutBucketImageRequest;
5052
import com.aliyun.oss.model.PutImageStyleRequest;
5153
import com.aliyun.oss.model.ResizeUdfApplicationRequest;
@@ -94,7 +96,8 @@ public final class RequestMarshallers {
9496
public static final CreateUdfApplicationRequestMarshaller createUdfApplicationRequestMarshaller = new CreateUdfApplicationRequestMarshaller();
9597
public static final UpgradeUdfApplicationRequestMarshaller upgradeUdfApplicationRequestMarshaller = new UpgradeUdfApplicationRequestMarshaller();
9698
public static final ResizeUdfApplicationRequestMarshaller resizeUdfApplicationRequestMarshaller = new ResizeUdfApplicationRequestMarshaller();
97-
99+
public static final ProcessObjectRequestMarshaller processObjectRequestMarshaller = new ProcessObjectRequestMarshaller();
100+
98101
public interface RequestMarshaller<R> extends Marshaller<FixedLengthInputStream, R> {
99102

100103
}
@@ -726,7 +729,6 @@ public byte[] marshall(CreateUdfApplicationRequest request) {
726729
xmlBody.append("<InstanceNum>" + config.getInstanceNum() + "</InstanceNum>");
727730
xmlBody.append("<Flavor>");
728731
xmlBody.append("<InstanceType>" + config.getFlavor().getInstanceType() + "</InstanceType>");
729-
xmlBody.append("<IoOptimized>" + config.getFlavor().getIoOptimized() + "</IoOptimized>");
730732
xmlBody.append("</Flavor>");
731733
xmlBody.append("</CreateUDFApplicationConfiguration>");
732734

@@ -784,6 +786,26 @@ public byte[] marshall(ResizeUdfApplicationRequest request) {
784786

785787
}
786788

789+
public static final class ProcessObjectRequestMarshaller implements RequestMarshaller2<ProcessObjectRequest> {
790+
791+
@Override
792+
public byte[] marshall(ProcessObjectRequest request) {
793+
StringBuffer processBody = new StringBuffer();
794+
795+
processBody.append(RequestParameters.SUBRESOURCE_PROCESS);
796+
processBody.append("=" + request.getProcess());
797+
798+
byte[] rawData = null;
799+
try {
800+
rawData = processBody.toString().getBytes(DEFAULT_CHARSET_NAME);
801+
} catch (UnsupportedEncodingException e) {
802+
throw new ClientException("Unsupported encoding " + e.getMessage(), e);
803+
}
804+
return rawData;
805+
}
806+
807+
}
808+
787809
private static enum EscapedChar {
788810
// "\r"
789811
RETURN("&#x000D;"),

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,38 @@ public void loadMimetypes(InputStream is) throws IOException {
8888
}
8989
}
9090
}
91-
91+
9292
public String getMimetype(String fileName) {
93+
String mimeType = getMimetypeByExt(fileName);
94+
if (mimeType != null) {
95+
return mimeType;
96+
}
97+
return DEFAULT_MIMETYPE;
98+
}
99+
100+
public String getMimetype(File file) {
101+
return getMimetype(file.getName());
102+
}
103+
104+
public String getMimetype(File file, String key) {
105+
return getMimetype(file.getName(), key);
106+
}
107+
108+
public String getMimetype(String primaryObject, String secondaryObject) {
109+
String mimeType = getMimetypeByExt(primaryObject);
110+
if (mimeType != null) {
111+
return mimeType;
112+
}
113+
114+
mimeType = getMimetypeByExt(secondaryObject);
115+
if (mimeType != null) {
116+
return mimeType;
117+
}
118+
119+
return DEFAULT_MIMETYPE;
120+
}
121+
122+
private String getMimetypeByExt(String fileName) {
93123
int lastPeriodIndex = fileName.lastIndexOf(".");
94124
if (lastPeriodIndex > 0 && lastPeriodIndex + 1 < fileName.length()) {
95125
String ext = fileName.substring(lastPeriodIndex + 1).toLowerCase();
@@ -98,11 +128,6 @@ public String getMimetype(String fileName) {
98128
return mimetype;
99129
}
100130
}
101-
return DEFAULT_MIMETYPE;
131+
return null;
102132
}
103-
104-
public String getMimetype(File file) {
105-
return getMimetype(file.getName());
106-
}
107-
108133
}

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
import java.util.concurrent.Future;
4949
import java.util.concurrent.TimeUnit;
5050

51+
import com.aliyun.oss.event.ProgressEventType;
52+
import com.aliyun.oss.event.ProgressListener;
53+
import com.aliyun.oss.event.ProgressPublisher;
5154
import com.aliyun.oss.model.DownloadFileRequest;
5255
import com.aliyun.oss.model.DownloadFileResult;
5356
import com.aliyun.oss.model.GenericRequest;
@@ -335,14 +338,22 @@ private DownloadFileResult downloadFileWithCheckpoint(DownloadFileRequest downlo
335338
prepare(downloadCheckPoint, downloadFileRequest);
336339
}
337340

341+
// 进度条开始下载数据
342+
ProgressListener listener = downloadFileRequest.getProgressListener();
343+
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
344+
338345
// 并发下载分片
339346
DownloadResult downloadResult = download(downloadCheckPoint, downloadFileRequest);
340347
for (PartResult partResult : downloadResult.getPartResults()) {
341348
if (partResult.isFailed()) {
349+
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
342350
throw partResult.getException();
343351
}
344352
}
345353

354+
// 进度条下载数据完成
355+
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
356+
346357
// 重命名临时文件
347358
renameTo(downloadFileRequest.getTempDownloadFile(), downloadFileRequest.getDownloadFile());
348359

@@ -389,10 +400,23 @@ private DownloadResult download(DownloadCheckPoint downloadCheckPoint, DownloadF
389400
ExecutorService service = Executors.newFixedThreadPool(downloadFileRequest.getTaskNum());
390401
ArrayList<Future<PartResult>> futures = new ArrayList<Future<PartResult>>();
391402
List<Task> tasks = new ArrayList<Task>();
392-
403+
ProgressListener listener = downloadFileRequest.getProgressListener();
404+
405+
// 计算待下载的数据量
406+
long contentLength = 0;
407+
for (int i = 0; i < downloadCheckPoint.downloadParts.size(); i++) {
408+
if (!downloadCheckPoint.downloadParts.get(i).isCompleted) {
409+
long partSize = downloadCheckPoint.downloadParts.get(i).end - downloadCheckPoint.downloadParts.get(i).start + 1;
410+
contentLength += partSize;
411+
}
412+
}
413+
ProgressPublisher.publishResponseContentLength(listener, contentLength);
414+
downloadFileRequest.setProgressListener(null);
415+
416+
// 下载数据分片
393417
for (int i = 0; i < downloadCheckPoint.downloadParts.size(); i++) {
394418
if (!downloadCheckPoint.downloadParts.get(i).isCompleted) {
395-
Task task = new Task(i, "download-" + i, downloadCheckPoint, i, downloadFileRequest, objectOperation);
419+
Task task = new Task(i, "download-" + i, downloadCheckPoint, i, downloadFileRequest, objectOperation, listener);
396420
futures.add(service.submit(task));
397421
tasks.add(task);
398422
} else {
@@ -402,42 +426,48 @@ private DownloadResult download(DownloadCheckPoint downloadCheckPoint, DownloadF
402426
}
403427
service.shutdown();
404428

429+
// 等待分片下载完成
405430
service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
406-
407431
for (Future<PartResult> future : futures) {
408432
try {
409433
PartResult tr = future.get();
410434
taskResults.add(tr);
411435
} catch (ExecutionException e) {
436+
downloadFileRequest.setProgressListener(listener);
412437
throw e.getCause();
413438
}
414439
}
415440

441+
// 对PartResult按照分片序号排序
416442
Collections.sort(taskResults, new Comparator<PartResult>() {
417443
@Override
418444
public int compare(PartResult p1, PartResult p2) {
419445
return p1.getNumber() - p2.getNumber();
420446
}
421447
});
422448

449+
// 设置返回结果
423450
downloadResult.setPartResults(taskResults);
424451
if (tasks.size() > 0) {
425452
downloadResult.setObjectMetadata(tasks.get(0).GetobjectMetadata());
426453
}
454+
downloadFileRequest.setProgressListener(listener);
427455

428456
return downloadResult;
429457
}
430458

431459
static class Task implements Callable<PartResult> {
432460

433461
public Task(int id, String name, DownloadCheckPoint downloadCheckPoint, int partIndex,
434-
DownloadFileRequest downloadFileRequest, OSSObjectOperation objectOperation) {
462+
DownloadFileRequest downloadFileRequest, OSSObjectOperation objectOperation,
463+
ProgressListener progressListener) {
435464
this.id = id;
436465
this.name = name;
437466
this.downloadCheckPoint = downloadCheckPoint;
438467
this.partIndex = partIndex;
439468
this.downloadFileRequest = downloadFileRequest;
440469
this.objectOperation = objectOperation;
470+
this.progressListener = progressListener;
441471
}
442472

443473
@Override
@@ -476,6 +506,8 @@ public PartResult call() throws Exception {
476506
if (downloadFileRequest.isEnableCheckpoint()) {
477507
downloadCheckPoint.dump(downloadFileRequest.getCheckpointFile());
478508
}
509+
ProgressPublisher.publishResponseBytesTransferred(progressListener,
510+
(downloadPart.end - downloadPart.start + 1));
479511
} catch (Exception e) {
480512
tr.setFailed(true);
481513
tr.setException(e);
@@ -504,6 +536,7 @@ public ObjectMetadata GetobjectMetadata () {
504536
private DownloadFileRequest downloadFileRequest;
505537
private OSSObjectOperation objectOperation;
506538
private ObjectMetadata objectMetadata;
539+
private ProgressListener progressListener;
507540
}
508541

509542
private ArrayList<DownloadPart> splitFile(long objectSize, long partSize) {

0 commit comments

Comments
 (0)