Skip to content

Commit

Permalink
get_object: Support close explicitly & try-with-resources (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
owarai authored Apr 29, 2024
1 parent d0c0d44 commit 3a1ccb5
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 44 deletions.
34 changes: 15 additions & 19 deletions docs/example/get_object.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,25 @@ getObject(bucket, "folder/test.mp3", "/Users/chengww/Desktop/test.mp3");
* @param localKeptPath the object will be kept in this path.
*/
private void getObject(Bucket bucket, String objectKey, String localKeptPath) {
try {
Bucket.GetObjectInput input = new Bucket.GetObjectInput();
Bucket.GetObjectOutput output = bucket.getObject(objectKey, input);
Bucket.GetObjectInput input = new Bucket.GetObjectInput();
// Use try-with-resource to close resources, otherwise it will cause connection leaks.
// Using try-with-finally or explicitly calling output's close() method also works.
try (Bucket.GetObjectOutput output = bucket.getObject(objectKey, input)) {
InputStream inputStream = output.getBodyInputStream();
if (output.getStatueCode() == 200) {
if (inputStream != null) {
FileOutputStream fos = new FileOutputStream(localKeptPath);
int len;
byte[] bytes = new byte[4096];
while ((len = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.flush();
fos.close();
System.out.println("Get object success.");
System.out.println("ObjectKey = " + objectKey);
System.out.println("LocalKeptPath = " + localKeptPath);
} else {
System.out.println("Get object status code == 200, but inputStream is null, skipped.");
FileOutputStream fos = new FileOutputStream(localKeptPath);
int len;
byte[] bytes = new byte[4096];
while ((len = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.flush();
fos.close();
System.out.println("Get object success.");
System.out.println("ObjectKey = " + objectKey);
System.out.println("LocalKeptPath = " + localKeptPath);
} else {
System.out.println("Get object status code == 200, but inputStream is null, skipped.");
// Failed
System.out.println("Failed to get object.");
System.out.println("StatueCode = " + output.getStatueCode());
Expand All @@ -57,10 +55,8 @@ getObject(bucket, "folder/test.mp3", "/Users/chengww/Desktop/test.mp3");
System.out.println("Code = " + output.getCode());
System.out.println("Url = " + output.getUrl());
}
if (inputStream != null) inputStream.close();
} catch (QSException | IOException e) {
e.printStackTrace();
}
}

```
2 changes: 1 addition & 1 deletion docs/example/get_object_by_segment.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ then you can get object multi:
}
outa.flush();

output.getBodyInputStream().close();
output.close();

}

Expand Down
2 changes: 1 addition & 1 deletion docs/example/get_object_by_segment_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Bucket bucket = new Bucket(env, zoneKey, bucketName);
}
outa.flush();

output.getBodyInputStream().close();
output.close();

}

Expand Down
33 changes: 15 additions & 18 deletions docs/example/get_object_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,25 @@ getObject(bucket, "folder/test.mp3", "/Users/chengww/Desktop/test.mp3");
* 对象将会被保存到该目录。
*/
private void getObject(Bucket bucket, String objectKey, String localKeptPath) {
try {
Bucket.GetObjectInput input = new Bucket.GetObjectInput();
Bucket.GetObjectOutput output = bucket.getObject(objectKey, input);
Bucket.GetObjectInput input = new Bucket.GetObjectInput();
// 使用 try-with-resource 来关闭资源, 否则会造成连接泄漏.
// 使用 try-with-finally or 显式调用 output 的 close() 方法也可以.
try (Bucket.GetObjectOutput output = bucket.getObject(objectKey, input)) {
InputStream inputStream = output.getBodyInputStream();
if (output.getStatueCode() == 200) {
if (inputStream != null) {
FileOutputStream fos = new FileOutputStream(localKeptPath);
int len;
byte[] bytes = new byte[4096];
while ((len = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.flush();
fos.close();
System.out.println("Get object success.");
System.out.println("ObjectKey = " + objectKey);
System.out.println("LocalKeptPath = " + localKeptPath);
} else {
System.out.println("Get object status code == 200, but inputStream is null, skipped.");
FileOutputStream fos = new FileOutputStream(localKeptPath);
int len;
byte[] bytes = new byte[4096];
while ((len = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.flush();
fos.close();
System.out.println("Get object success.");
System.out.println("ObjectKey = " + objectKey);
System.out.println("LocalKeptPath = " + localKeptPath);
} else {
System.out.println("Get object status code == 200, but inputStream is null, skipped.");
// Failed
System.out.println("Failed to get object.");
System.out.println("StatueCode = " + output.getStatueCode());
Expand All @@ -68,7 +66,6 @@ getObject(bucket, "folder/test.mp3", "/Users/chengww/Desktop/test.mp3");
System.out.println("Code = " + output.getCode());
System.out.println("Url = " + output.getUrl());
}
if (inputStream != null) inputStream.close();
} catch (QSException | IOException e) {
e.printStackTrace();
}
Expand Down
2 changes: 1 addition & 1 deletion docs/example/put_object_and_set_default_download_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ if (output2.getBodyInputStream() != null && output2.getStatueCode() == 200) {
out.write(buffer, 0, bytesRead);
}
out.close();
output2.getBodyInputStream().close();
output2.close();
```

You can get the object url for downloading.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if (output2.getBodyInputStream() != null && output2.getStatueCode() == 200) {
out.write(buffer, 0, bytesRead);
}
out.close();
output2.getBodyInputStream().close();
output2.close();
```

或获取下载链接,直接浏览器打开即可自动下载并处理文件名称
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/qingstor/sdk/service/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import com.qingstor.sdk.service.Types.*;
import com.qingstor.sdk.utils.QSParamInvokeUtil;
import com.qingstor.sdk.utils.QSStringUtil;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -7125,7 +7127,7 @@ public String validateParam() {
* field ResponseExpires Specified the Expires response header <br>
* field VersionID Object version id <br>
*/
public static class GetObjectOutput extends OutputModel {
public static class GetObjectOutput extends OutputModel implements Closeable {

/** The response body */

Expand Down Expand Up @@ -7270,6 +7272,13 @@ public void setBodyInputStream(InputStream bodyInputStream) {
this.bodyInputStream = bodyInputStream;
}

@Override
public void close() throws IOException {
if (bodyInputStream != null) {
bodyInputStream.close();
}
}

/**
* The Cache-Control general-header field is used to specify directives for caching
* mechanisms in both requests and responses.
Expand Down Expand Up @@ -8158,7 +8167,7 @@ public String validateParam() {
* field ResponseContentType Specified the Content-Type response header <br>
* field ResponseExpires Specified the Expires response header <br>
*/
public static class ImageProcessOutput extends OutputModel {
public static class ImageProcessOutput extends OutputModel implements Closeable {

/** The response body */
private InputStream bodyInputStream;
Expand All @@ -8182,6 +8191,13 @@ public void setBodyInputStream(InputStream bodyInputStream) {
this.bodyInputStream = bodyInputStream;
}

@Override
public void close() throws IOException {
if (bodyInputStream != null) {
bodyInputStream.close();
}
}

/** Object content length */
private Long contentLength;

Expand Down
1 change: 1 addition & 0 deletions src/test/java/integration/cucumber/ImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ public void image_process_status_code_is(int statusCode) throws Throwable {
// Write code here that turns the phrase above into concrete actions
System.out.println("image_process_message:" + imageProcessOutput.getMessage());
TestUtil.assertEqual(imageProcessOutput.getStatueCode(), statusCode);
imageProcessOutput.close();
}
}
10 changes: 9 additions & 1 deletion template/shared.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@
* field {{$property.Name| camelCase}} {{replace $property.Description "<bucket-name>/<object-key>" "'bucket-name'/'object-key'" -1}} <br>
{{- end}}
*/
public static class {{$opID}}Output extends OutputModel{
public static class {{$opID}}Output extends OutputModel {{if eq $opID "GetObject" "ImageProcess"}} implements Closeable {{- end}} {
{{range $_, $response := $operation.Responses}}
{{if eq $response.Body.Type "string"}}
{{if $response.Body.Description -}}
Expand Down Expand Up @@ -961,6 +961,14 @@
public void setBodyInputStream(InputStream bodyInputStream) {
this.bodyInputStream=bodyInputStream;
}

@Override
public void close() throws IOException {
if (bodyInputStream != null) {
bodyInputStream.close();
}
}

{{end}}

{{if $response.Elements.Properties | len}}
Expand Down
2 changes: 2 additions & 0 deletions template/sub_services.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

package com.qingstor.sdk.service;

import java.io.Closeable;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down

0 comments on commit 3a1ccb5

Please sign in to comment.