Skip to content

Commit

Permalink
java sdk 添加URL签名 demon
Browse files Browse the repository at this point in the history
  • Loading branch information
huan.xie committed Mar 25, 2024
1 parent b43743d commit ef3a04a
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions collections/_java-sdk/签名URL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: '在URL中包含签名'
sidebar:
nav: java-sdk
---

SDK 提供了公共URL签名接口`getPublicURL`和私有URL签名接口`getPrivateURL`

`getPublicURL``getPrivateURL`方法请求的 US3 API 为 `getDownloadUrlFromPublicBucket``getDownloadUrlFromPrivateBucket`,具体详见[签名URL API文档](https://docs.ucloud.cn/ufile/api/authorization-url)

### 代码示例

<div class="copyable" markdown="1">

{% highlight go linenos %}
public static void getPublicURL(String keyName, String bucketName) {
try {
/*
生成公共下载URL
*/
String public_url = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getDownloadUrlFromPublicBucket(keyName, bucketName)
.createUrl();
JLog.D(TAG, String.format("[PublicUrl] = %s", public_url));
} catch (UfileParamException e) {
e.printStackTrace();
} catch (UfileAuthorizationException e) {
e.printStackTrace();
} catch (UfileSignatureException e) {
e.printStackTrace();
} catch (UfileClientException e) {
e.printStackTrace();
}
}

public static void getPrivateURL(String keyName, String bucketName, int expiresDuration) {
try {
/*
生成私有下载URL
*/
// expiresDuration 私有下载路径的有效时间,即:生成的下载URL会在 创建时刻的时间戳 + expiresDuration秒后的时刻过期
String private_url = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.getDownloadUrlFromPrivateBucket(keyName, bucketName, expiresDuration)
.createUrl();
JLog.D(TAG, String.format("[PrivateUrl] = %s", private_url));
} catch (UfileParamException e) {
e.printStackTrace();
} catch (UfileAuthorizationException e) {
e.printStackTrace();
} catch (UfileSignatureException e) {
e.printStackTrace();
} catch (UfileClientException e) {
e.printStackTrace();
}
}
{% endhighlight %}
</div>

### 错误码

| HTTP 状态码 | RetCode | ErrMsg | 描述 |
| ----------- | ------- | ---------------------- | ----------------------------------- |
| 400 | -148653 | bucket not exists | 存储空间不存在 |
| 400 | -15036 | check md5 failed | MD5校验失败 |
| 401 | -148643 | no authorization found | 上传凭证错误 |
| 403 | -148643 | invalid signature | API公私钥错误或者KeyName包含%、#、? |
| 404 | -148654 | file not exist | 源文件不存在 |

0 comments on commit ef3a04a

Please sign in to comment.