<dependency> <groupId>com.huaweicloud</groupId> <artifactId>esdk-obs-java</artifactId> <version>3.23.9.1</version> </dependency>
public String getSignedUrl(String cid, JsonObject config) {
// 本次请求的桶的endpoint
String endPoint = config.getString("end_point", "https://obs.xxxx.myhuaweicloud.com");
// 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY_ID。
// 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
String ak = config.getString("access_key");
String sk = config.getString("secret_key");
String bucketName = config.getString("bucket_name");
String objectKey = config.getString("object_name");
// 创建ObsClient实例
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
// 替换您的过期时间,单位是秒
long expireSeconds = 300L;
// 替换成您对应的操作
TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.PUT, expireSeconds);
// 替换为请求本次操作访问的桶名和对象名
request.setBucketName(bucketName);
request.setExpires(expireSeconds);
request.setObjectKey(objectKey);
request.setMethod(HttpMethodEnum.PUT);
Map<String,String> head = new HashMap<>();
head.put("x-obs-acl", "public-read");
request.setHeaders(head);
obsClient.setBucketAcl(bucketName, AccessControlList.REST_CANNED_PUBLIC_READ);
TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
// 成功返回预签名URL,如下打印URL信息
System.out.println(response.getSignedUrl());
return response.getSignedUrl();
}