使用BlobStore为对象生成临时URI

时间:2014-05-26 02:31:53

标签: java openstack openstack-swift jclouds

我正在尝试使用jClouds BlobStore功能为存储在OpenStack Swift容器中的对象实现一代临时GET URI。

但是我收到了这些错误:

java.lang.IllegalStateException: Suppliers.memoizeWithExpiration(get().getTemporaryUrlKey() using {annotationParser={caller=SwiftApi.accountApiInRegion[RegionOne]}}, 60000000000, NANOS) returned a null temporaryUrlKey!
    at com.google.common.base.Preconditions.checkState(Preconditions.java:177)
    at org.jclouds.openstack.swift.v1.TemporaryUrlSigner.hmacSHA1(TemporaryUrlSigner.java:63)
    at org.jclouds.openstack.swift.v1.TemporaryUrlSigner.sign(TemporaryUrlSigner.java:57)
    at org.jclouds.openstack.swift.v1.blobstore.RegionScopedTemporaryUrlBlobSigner.sign(RegionScopedTemporaryUrlBlobSigner.java:100)
    at org.jclouds.openstack.swift.v1.blobstore.RegionScopedTemporaryUrlBlobSigner.signGetBlob(RegionScopedTemporaryUrlBlobSigner.java:73)

以下是代码示例:

public class BlobStoreObjectSignerService implements ObjectSignerService {

    @Autowired
    private BlobRequestSigner blobRequestSigner;

    @Value("${blobStore.private.container}")
    String privateContainerName;

    /**
    * Generates signed request With GET Method to access allocated object.
    *   
    * @param objectName         name of the stored file
    * @param principalName      name of the principal (user) which is putted the file
    * @param temporary          defines if file should be available for a limited time
    * @param timeToLive         time in seconds file will be available for fetching
    */
    public HttpRequest generateGetRequestForObject( String objectName, 
                                                    String principalName, 
                                                    boolean temporary, 
                                                    long timeToLive ) {
        if (temporary) {
            return this.generateTemporaryGetRequestForObject(objectName,  principalName, timeToLive);
        } else {
            return this.generatePermanentGetRequestForObject(objectName,  principalName);
        }
    }

    public HttpRequest generatePermanentGetRequestForObject( String objectName, 
                                                             String principalName ) {
        return this.generatePermanentGetRequestForObject( privateContainerName,
                                                          String.format( "/%s/%s", principalName, objectName ) );
    }

    public HttpRequest generateTemporaryGetRequestForObject( String objectName, 
                                                             String principalName,
                                                             long timeToLive ) {
        return this.generateTemporaryGetRequestForObject( privateContainerName,
                                                          String.format( "/%s/%s", principalName, objectName ),
                                                          timeToLive );
    }

    public HttpRequest generatePermanentGetRequestForObject(String containerName, String objectName) {
        return this.blobRequestSigner.signGetBlob(containerName, objectName);
    }

    public HttpRequest generateTemporaryGetRequestForObject(String containerName, String objectName, long timeToLive) {
        return this.blobRequestSigner.signGetBlob(containerName, objectName, timeToLive);
    }

}

对于公共容器,我仍然可以通过获取标题/元数据来获取对象链接,它有点横向,但我仍然需要签名的URI来发布存储在私有容器中的对象。

使用BlobStore将文件放入容器中工作得很好。所有推送通常都是由OpenStack Dashboard发布的。

我只在dev环境中使用Java 7和DevStack为Swift配置。 jClouds版本是1.7.2。

1 个答案:

答案 0 :(得分:2)

根据例外情况,您似乎没有在帐户上设置临时URL密钥。您可以通过AccountApi将密钥更新为新值:

SwiftApi swiftApi = ContextBuilder.newBuilder(PROVIDER)
           .credentials("{username}", "{apiKey}")
           .buildApi(SwiftApi.class);

// Access the accountApi
AccountApi accountApi = swiftApi.getAccountApiForRegion("{regionId");
accountApi.updateTemporaryUrlKey("{newKey}");

// Access the key
String tempUrlKey;
if (accountApi.getTemporaryUrlKey().isPresent()) {
   tempUrlKey = accountApi.get().getTemporaryUrlKey().get();
}

希望有所帮助!