How to create temporary URL for Swift object storage using REST API?

南楼画角 提交于 2019-12-13 19:54:04

问题


Swift Object storage allow you to create a temporary URL for any resource with an expiry date. This can be achieved with swift CLI command line. To make use of this functionality in an web application, I need to achieve the creation of temporary URL using API call, So that I can make a rest CALL and get the temp URL which can later be embedded in HTML and resource downloaded by the we browser directly.

From the documentation I dont see any API mentioned for this ? Do anyone know how i can get it from Java using API call.

Thanks Manoj


回答1:


There is no direct API available to generate temporary URL for Swift objects. Instead it has to generated from client side with the help of X-Account-Meta-Temp-URL-Key secret key as per described in this document

Here is the python version of code to generate it. Refer this to re-implement it in Java and then it can be embedded anywhere.

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)

Here is an another reference, which is a customization done to Openstack Horizon to provide an UI feature to generate swift objects temp URL.




回答2:


For other people looking for the answer in java, Below is the code snippet to get the hmac in 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()));
        }

The above code is taken from https://gist.github.com/ishikawa/88599

Use the hmac to create the temporary URL as per the below code

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

Thanks



来源:https://stackoverflow.com/questions/37048060/how-to-create-temporary-url-for-swift-object-storage-using-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!