操作场景
通过图片处理接口无法保存处理后的图片至ZOS,仅生成临时图片用于返回,因此ZOS提供了图片处理持久化的功能,图片处理持久化可以指定处理后的图片的保存位置。
具体参数可参考图片处理参数,通过持久化处理后的图片,可以更高效的管理访问桶内图片资源。
持久化请求可参考SDK文档中图片处理Post请求处理图片部分。
约束与限制
-
权限要求
进行图片转存操作时,要求账号具有源Bucket的GetObject和PutObject权限,以及目标Bucket的PutBucket和目标Object的PutObject权限。
-
存储位置
原图所在bucket与持久化图片转存的bucket可以相同也可以不同,但必须是同账号下同地域的桶。
示例
对图片依次进行缩放、裁剪操作,最后将处理后的图片持久化至dstBucket桶中。如要进行其他操作,可参考图片处理参数,并将imageop修改为对应操作字符串。
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.Protocol;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ProcessObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import java.io.*;
import java.util.Base64;
public class postPicProcess {
public static String ACCESS_KEY = "<your-access-key>";
public static String SECRET_KEY = "<your-secret-key>";
public static String END_POINT = "<your-endpoint>";
public static String srcBucket = "<your-srcBucket>";
public static String dstBucket = "<your-dstBucket>";
public static String srcKey = "<your-srcKey>";
public static String dstKey = "<your-dstKey>";
public static void main(String[] args) {
// 声明s3及桶名,这里换成自己的就好
AmazonS3 s3Client;
try{
// 当使用HTTPS协议且采用自签名认证时,需关闭证书检查
// System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
// 使用凭据和配置建立连接
AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
ClientConfiguration awsClientConfig = new ClientConfiguration();
// 使用V2签名时,采用"S3SignerType"
awsClientConfig.setSignerOverride("S3SignerType");
// 使用V4签名时,采用"AWSS3V4SignerType"
// awsClientConfig.setSignerOverride("AWSS3V4SignerType");
// 连接默认使用HTTPS协议,使用HTTP协议连接时需要显式指定
awsClientConfig.setProtocol(Protocol.HTTP);
s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withClientConfiguration(awsClientConfig)
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(END_POINT, ""))
.disableChunkedEncoding()
.build();
ProcessObjectRequest request = new ProcessObjectRequest();
request.setBucketName(dstBucket);
request.setKey(dstKey);
request.setProcessSource(srcBucket + "/" + srcKey);
String imageop = "image/resize,p_80/crop,w_500,h_500,x_10,y_10"
request.setZosProcess(imageop);
s3Client.ProcessObject(request);
System.out.print("=====request success=====\n");
}catch (Exception e) {
System.out.print("=====request fail=====\n");
System.out.print(e.getMessage());
}
}
}
代码执行方式:
javac watermarks.java
java watermarks