2016-05-05 75 views
1

Swift Object存儲允許您爲任何具有到期日期的資源創建臨時URL。這可以通過swift CLI命令行來實現。爲了在Web應用程序中使用此功能,我需要使用API​​調用來創建臨時URL,以便我可以休息CALL並獲取稍後可以嵌入到HTML中的臨時URL以及我們下載的資源直接瀏覽器。如何使用REST API爲Swift對象存儲創建臨時URL?

從文檔我沒有看到任何API提到這個?有誰知道我可以如何使用API​​調用從Java獲得它。

感謝 馬諾

回答

2

有可用來生成斯威夫特對象的臨時URL沒有直接的API。相反,它已經從客戶端生成與X-帳戶-元溫度-URL-重點密鑰的幫助下按在this document

這裏描述的代碼的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) 

這裏是一個another reference,這是做了OpenStack的地平線提供的UI功能生成快捷對象的臨時URL定製。

+0

後的C將上述轉化爲java,這對我來說工作得很好。非常感謝。 –

1

對於其他人尋找Java中的答案,下面是代碼片段,以獲得HMAC在Java

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())); 
     } 

上面的代碼是從https://gist.github.com/ishikawa/88599

使用所採取的HMAC創建臨時URL按下面的代碼

Long expires = (System.currentTimeMillis()/1000)+ <expiry in seconds>; 
String tempURL=""+baseURL+path+"?temp_url_sig="+hmac+"& temp_url_expires="+expires; 

感謝

相關問題