import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosException;
import com.volcengine.tos.model.object.GetObjectV2Input;
import com.volcengine.tos.model.object.GetObjectV2Output;
import com.volcengine.tos.model.object.ObjectMetaRequestOptions;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class GetObjectWithIfConditionExample extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
String endpoint = "your endpoint";
String region = "your region";
String accessKey = "your access key";
String secretKey = "your secret key";
String securityToken = "your security token";
String bucketName = "your bucket name";
String objectKey = "your object key";
String filePath = "your file path";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey, securityToken);
Thread tosThread = new Thread(new Runnable() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("2022-09-13 14:36:01");
ObjectMetaRequestOptions options = new ObjectMetaRequestOptions().setIfModifiedSince(date);
GetObjectV2Input input = new GetObjectV2Input().setBucket(bucketName).setKey(objectKey).setOptions(options);
try(FileOutputStream fileOutputStream = new FileOutputStream(filePath);
GetObjectV2Output output = tos.getObject(input)) {
byte[] buffer = new byte[1024];
while ((length = output.getContent().read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, length);
Log.i("getObject", "getObject succeed, object's metadata is " + output.getGetObjectBasicOutput());
} catch (IOException e) {
Log.e("IOException", "write data to file failed");
} catch (TosException e) {
if (e.getStatusCode() == 304) {
Log.e("TosException", "server return 304 not modified");
} else if (e.getStatusCode() == 412) {
Log.e("TosException", "server return 412 precondition failed.");
} else if (e.getStatusCode() == 404) {
Log.e("TosException", "the object you want to download is not found");
Log.e("TosException", "getObject failed");
} catch (ParseException e) {
Log.e("ParseException", "getObject failed");