无法使用JClouds将文件从Microsoft Windows Azure VM上载到Azure BLOB

时间:2014-03-28 07:51:33

标签: java azure azure-storage blobstore jclouds

我正在尝试使用Jclouds将文件上传到Azure BLOB。运行服务器的环境是Microsoft Windows Azure VM,它在专用网络上运行。它通过防火墙打开端口80和443。以下代码用于上载文件。如果我从我的PC执行它,相同的代码正在运行:

@SuppressWarnings("nls")
public boolean upload(final String path) 
{
    final File file = new File(path);

    FileInputStream fis = null;
    BlobStoreContext context =null;
    try
    {
        fis = new FileInputStream(file);

        context = ContextBuilder.newBuilder("azureblob").credentials(this.store, this.key).buildView(BlobStoreContext.class);

        final BlobStore store = context.getBlobStore();

        Payload payload = new InputStreamPayload(fis);
        payload.getContentMetadata().setContentLength(fis.getChannel().size());

        final Blob blob = store.blobBuilder(file.getName()).payload(payload).build();

        context.getBlobStore().putBlob("js-uc-osprey", blob);
        return true;
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    finally
    {
        if(context!=null)
        context.close();            
    }

    return false;
}

我得到以下异常:

com.google.inject.CreationException: Guice creation errors:

1) org.jclouds.rest.RestContext<org.jclouds.azureblob.AzureBlobClient, A> cannot be used as a key; It is not fully specified.

1 error
        at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
        at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154)
        at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
        at com.google.inject.Guice.createInjector(Guice.java:95)
        at org.jclouds.ContextBuilder.buildInjector(ContextBuilder.java:321)
        at org.jclouds.ContextBuilder.buildInjector(ContextBuilder.java:261)
        at org.jclouds.ContextBuilder.buildView(ContextBuilder.java:521)
        at org.jclouds.ContextBuilder.buildView(ContextBuilder.java:501)
        at com.ct.jsix.storage.service.impl.StorageServiceImpl.upload(StorageServiceImpl.java:147)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180)
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:193)
        at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:102)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
        at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.serviceRequest(JettyHTTPDestination.java:355)
        at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:319)
        at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:72)
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1040)
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:976)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
        at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
        at org.eclipse.jetty.server.Server.handle(Server.java:363)
        at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:483)
        at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:931)
        at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:992)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:856)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:744)

是否要在Azure VM上进行任何配置才能使其正常工作?请帮忙。

1 个答案:

答案 0 :(得分:1)

您是否使用Java 7u51运行jclouds 1.6.x或1.7.0?如果是这样,您可能遇到https://issues.apache.org/jira/browse/JCLOUDS-427。您可以通过降级到Java 7u45或升级到jclouds 1.7.1来解决这个问题。

此外,您应该更喜欢ByteSource Payload而不是FileInputStream,因为前者允许jclouds重试逻辑工作。您可以使用Files.asByteSource(File)创建一个。