You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
预签名机制
Post 表单预签名(Java SDK)
复制全文
下载 pdf
Post 表单预签名(Java SDK)
本文介绍 Post 表单预签名的示例代码。​
示例代码​
​
import com.volcengine.tos.TOSV2;​
import com.volcengine.tos.TOSV2ClientBuilder;​
import com.volcengine.tos.TosClientException;​
import com.volcengine.tos.model.object.PreSignedPostSignatureInput;​
import com.volcengine.tos.model.object.PreSignedPostSignatureOutput;​
​
public class PreSignedPostSignatureExample {​
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 = "example_dir/example_object.txt";​
// 预签名 URL 过期时间,单位为秒,设置3600秒即1小时后过期​
long expires = 3600;​
// 设置上传的condition,表单中必须携带x-tos-acl为private,key必须以objectKey开头​
List<PostSignatureCondition> conditions = new ArrayList<>(2);​
conditions.add(new PostSignatureCondition("x-tos-acl", "private"));​
conditions.add(new PostSignatureCondition("key", objectKey, "starts-with"));​
​
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);​
​
try{​
PreSignedPostSignatureInput input = new PreSignedPostSignatureInput().setBucket(bucketName)​
.setExpires(expires).setConditions(conditions);​
PreSignedPostSignatureOutput output = tos.preSignedPostSignature(input);​
System.out.println("preSignedPostSignature succeed, the signature is " + output.getSignature());​
System.out.println("the credential is " + output.getCredential());​
System.out.println("the policy is " + output.getPolicy());​
System.out.println("the date is " + output.getDate());​
System.out.println("the algorithm is " + output.getAlgorithm());​
System.out.println("the originPolicy is " + output.getOriginPolicy());​
} catch (TosClientException e) {​
// 操作失败,捕获客户端异常,一般情况是请求参数错误,此时请求并未发送​
System.out.println("preSignedPostSignature failed");​
System.out.println("Message: " + e.getMessage());​
if (e.getCause() != null) {​
e.getCause().printStackTrace();​
}​
} catch (Throwable t) {​
// 作为兜底捕获其他异常,一般不会执行到这里​
System.out.println("preSignedPostSignature failed");​
System.out.println("unexpected exception, message: " + t.getMessage());​
}​
}​
}​
​
相关文档​
关于 POST 表单预签名的详细信息,请参见基于浏览器上传的表单中包含签名。​
​
最近更新时间:2025.12.09 18:37:47
这个页面对您有帮助吗?
有用
有用
无用
无用