TOS 默认不保存处理后的图片。您可以使用代码将处理后的图片保存至指定的存储桶。本文介绍如何通过 TOS Java SDK 将处理后的图片保存至指定存储桶。
以下代码展示如何将图片高度固定为 100px,图片格式转换为 JPG 格式,然后将处理后的图片命名为 temp.jpg,并保存至 bucket-example 存储桶。
import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.core.type.TypeReference; 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; import java.nio.charset.StandardCharsets; import java.util.Base64; public class ImageSaveAsExample { static class SaveAsResult { String bucket; String fileSize; String object; String status; @Override public String toString() { return "SaveAsResult{" + "bucket='" + bucket + '\'' + ", fileSize='" + fileSize + '\'' + ", object='" + object + '\'' + ", status='" + status + '\'' + '}'; } } 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/resize,h_100/format,jpg"; //将图片高度固定为 100px ,并转为 JPG 格式 String saveBucket = "bucket-example"; String saveObject = "temp.jpg"; TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey); try { GetObjectV2Input input = new GetObjectV2Input().setBucket(bucketName).setKey(objectKey).setProcess(style) .setSaveBucket(Base64.getUrlEncoder().encodeToString(saveBucket.getBytes(StandardCharsets.UTF_8))) .setSaveObject(Base64.getUrlEncoder().encodeToString(saveObject.getBytes(StandardCharsets.UTF_8))); try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); GetObjectV2Output output = tos.getObject(input)) { byte[] buffer = new byte[1024]; int length; while ((length = output.getContent().read(buffer)) != -1) { stream.write(buffer, 0, length); } String respBody = stream.toString("UTF-8"); System.out.println("Image inspect success, inspect info is " + respBody); SaveAsResult result = TosUtils.getJsonMapper().readValue(respBody, new TypeReference<SaveAsResult>() { }); System.out.println(result.toString()); } catch (JacksonException e) { System.out.println("parse response data failed"); e.printStackTrace(); } catch (IOException e) { System.out.println("read response data failed"); e.printStackTrace(); } } catch (TosClientException e) { // 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送。 System.out.println("Image saveAs failed"); System.out.println("Message: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } } catch (TosServerException e) { // 操作失败,捕获服务端异常,可以获取到从服务端返回的详细错误信息。 System.out.println("Image saveAs 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()); } catch (Throwable t) { // 作为兜底捕获其他异常,一般不会执行到这里。 System.out.println("Image saveAs failed"); System.out.println("unexpected exception, message: " + t.getMessage()); } } }
关于图片处理持久化的详细介绍,请参见图片处理持久化。