import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.JsonNode;
import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.internal.util.TosUtils;
import com.volcengine.tos.model.object.GetObjectV2Input;
import com.volcengine.tos.model.object.GetObjectV2Output;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ImageInfoExample {
public static void main(String[] args) {
String endpoint = "your endpoint";
String region = "your region";
String accessKey = System.getenv("TOS_ACCESS_KEY");
String secretKey = System.getenv("TOS_SECRET_KEY");
String bucketName = "bucket-example";
String objectKey = "image.png";
String style = "image/info";
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
GetObjectV2Input input = new GetObjectV2Input().setBucket(bucketName).setKey(objectKey).setProcess(style);
try (ByteArrayOutputStream stream = new ByteArrayOutputStream();
GetObjectV2Output output = tos.getObject(input)) {
byte[] buffer = new byte[1024];
while ((length = output.getContent().read(buffer)) != -1) {
stream.write(buffer, 0, length);
String respBody = stream.toString("UTF-8");
System.out.println("Get image info succeed, image info is " + respBody);
JsonNode jsonNode = TosUtils.getJsonMapper().readTree(respBody);
String height = jsonNode.get("ImageHeight").get("value").asText();
String width = jsonNode.get("ImageWidth").get("value").asText();
String fileSize = jsonNode.get("FileSize").get("value").asText();
String format = jsonNode.get("Format").get("value").asText();
System.out.println("Image info height:" + height + ", width:" + width + ", size:" + fileSize + ", format:" + format);
} catch (JacksonException e) {
System.out.println("parse response data failed");
} catch (IOException e) {
System.out.println("read response data failed");
} catch (TosClientException e) {
System.out.println("get image info failed");
System.out.println("Message: " + e.getMessage());
if (e.getCause() != null) {
e.getCause().printStackTrace();
} catch (TosServerException e) {
System.out.println("get image info failed");
System.out.println("StatusCode: " + e.getStatusCode());
System.out.println("Code: " + e.getCode());
System.out.println("Message: " + e.getMessage());
System.out.println("RequestID: " + e.getRequestID());
System.out.println("get image info failed");
System.out.println("unexpected exception, message: " + t.getMessage());