Ibm cloud 如何使用RESTAPI为Swift对象存储创建临时URL?
Swift对象存储允许您为任何具有到期日期的资源创建临时URL。这可以通过swift CLI命令行实现。为了在web应用程序中使用此功能,我需要使用API调用实现临时URL的创建,以便我可以进行rest调用并获取临时URL,该临时URL稍后可以嵌入到HTML中,并由we浏览器直接下载资源 从文档中,我没有看到任何关于这个的API?有人知道我如何使用API调用从Java获得它吗 谢谢Ibm cloud 如何使用RESTAPI为Swift对象存储创建临时URL?,ibm-cloud,openstack,openstack-swift,Ibm Cloud,Openstack,Openstack Swift,Swift对象存储允许您为任何具有到期日期的资源创建临时URL。这可以通过swift CLI命令行实现。为了在web应用程序中使用此功能,我需要使用API调用实现临时URL的创建,以便我可以进行rest调用并获取临时URL,该临时URL稍后可以嵌入到HTML中,并由we浏览器直接下载资源 从文档中,我没有看到任何关于这个的API?有人知道我如何使用API调用从Java获得它吗 谢谢 Manoj没有可用于生成Swift对象临时URL的直接API。相反,它必须在X-Account-Meta-Temp
Manoj没有可用于生成Swift对象临时URL的直接API。相反,它必须在X-Account-Meta-Temp-URL-Key密钥的帮助下从客户端生成,如中所述 下面是生成它的python版本代码。参考本文,在Java中重新实现它,然后它就可以嵌入到任何地方
import hmac
from hashlib import sha1
from time import time
method = 'GET'
duration_in_seconds = 60*60*24
expires = int(time() + duration_in_seconds)
path = '/v1/AUTH_a422b2-91f3-2f46-74b7-d7c9e8958f5d30/container/object'
key = 'mykey'
hmac_body = '%s\n%s\n%s' % (method, expires, path)
sig = hmac.new(key, hmac_body, sha1).hexdigest()
s = 'https://{host}/{path}?temp_url_sig={sig}&temp_url_expires={expires}'
url = s.format(host='swift-cluster.example.com', path=path, sig=sig, expires=expires)
这里是一个,它是对Openstack Horizon进行的自定义,以提供生成swift对象临时URL的UI功能。没有直接API可用于生成swift对象的临时URL。相反,它必须在X-Account-Meta-Temp-URL-Key密钥的帮助下从客户端生成,如中所述 下面是生成它的python版本代码。参考本文,在Java中重新实现它,然后它就可以嵌入到任何地方
import hmac
from hashlib import sha1
from time import time
method = 'GET'
duration_in_seconds = 60*60*24
expires = int(time() + duration_in_seconds)
path = '/v1/AUTH_a422b2-91f3-2f46-74b7-d7c9e8958f5d30/container/object'
key = 'mykey'
hmac_body = '%s\n%s\n%s' % (method, expires, path)
sig = hmac.new(key, hmac_body, sha1).hexdigest()
s = 'https://{host}/{path}?temp_url_sig={sig}&temp_url_expires={expires}'
url = s.format(host='swift-cluster.example.com', path=path, sig=sig, expires=expires)
这里是一个,它是对Openstack Horizon的定制,提供了一个UI功能来生成swift对象临时URL。对于其他用java寻找答案的人,下面是用java获取hmac的代码片段
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String calculateRFC2104HMAC(String data, String key)
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
}
以上代码取自
使用hmac按照以下代码创建临时URL
Long expires = (System.currentTimeMillis()/1000)+ <expiry in seconds>;
String tempURL=""+baseURL+path+"?temp_url_sig="+hmac+"& temp_url_expires="+expires;
Long expires=(System.currentTimeMillis()/1000)+;
字符串tempURL=“+baseURL+path+”?temp_url_sig=“+hmac+”&temp_url_expires=“+expires;
感谢其他用java寻找答案的人,下面是用java获取hmac的代码片段
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String calculateRFC2104HMAC(String data, String key)
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return toHexString(mac.doFinal(data.getBytes()));
}
以上代码取自
使用hmac按照以下代码创建临时URL
Long expires = (System.currentTimeMillis()/1000)+ <expiry in seconds>;
String tempURL=""+baseURL+path+"?temp_url_sig="+hmac+"& temp_url_expires="+expires;
Long expires=(System.currentTimeMillis()/1000)+;
字符串tempURL=“+baseURL+path+”?temp_url_sig=“+hmac+”&temp_url_expires=“+expires;
谢谢在将上述内容转换为java之后,这项工作对我来说非常好。非常感谢。在将上述内容转换为java之后,这项工作对我来说非常好。谢谢。