Spring Batch/Azure 存储帐户 blob 资源 [container"foo", blob ='bar' ] 无法解析为绝对文件路径

标签 spring azure spring-batch azure-storage azure-blob-storage

我正在尝试使用 Spring 写入 Azure 存储。

我在 Bean 内配置资源,而不是从类中 Autowiring 它。

BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connectionString).endpoint(endpoint).buildClient();

这是 Azure 存储容器内的 blob 名称:

String resourceString = "azure-blob://foo/bar.csv"

设置 Azure 存储模式解析器:

AzureStorageResourcePatternResolver storageResourcePatternResolver = new AzureStorageResourcePatternResolver(client);

然后获取资源:

Resource resource = storageResourcePatternResolver.getResource(resourceString);

这是我收到的错误:

java.lang.UnsupportedOperationException: Azure storage account blob resource [container='foo', blob='bar.csv'] cannot be resolved to absolute file path
at com.azure.spring.autoconfigure.storage.resource.BlobStorageResource.getFile(BlobStorageResource.java:83) ~[azure-spring-boot-3.4.0.jar:na]
at org.springframework.batch.item.support.AbstractFileItemWriter.getOutputState(AbstractFileItemWriter.java:367) ~[spring-batch-infrastructure-4.3.2.jar:4.3.2]
at org.springframework.batch.item.support.AbstractFileItemWriter.open(AbstractFileItemWriter.java:308) ~[spring-batch-infrastructure-4.3.2.jar:4.3.2]
at org.springframework.batch.item.support.AbstractFileItemWriter$$FastClassBySpringCGLIB$$f2d35c3.invoke(<generated>) ~[spring-batch-infrastructure-4.3.2.jar:4.3.2]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.5.jar:5.3.5]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:137) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:124) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692) ~[spring-aop-5.3.5.jar:5.3.5]
at org.springframework.batch.item.file.FlatFileItemWriter$$EnhancerBySpringCGLIB$$5ce6a900.open(<generated>) ~[spring-batch-infrastructure-4.3.2.jar:4.3.2]
at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:104) ~[spring-batch-infrastructure-4.3.2.jar:4.3.2]
at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:311) ~[spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:205) ~[spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:138) [spring-batch-core-4.3.2.jar:4.3.2]
at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:135) [spring-batch-core-4.3.2.jar:4.3.2]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_221]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_221]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]2021-05-25 09:24:36.168  INFO 4916 --- [ taskExecutor-3] o.s.batch.core.step.AbstractStep         : Step: [slaveStep:partition155] executed in 347ms2021-05-25 09:24:36.240 ERROR 4916 --- [ taskExecutor-1] o.s.batch.core.step.AbstractStep         : Encountered an error executing step slaveStep in job job1

非常感谢任何帮助。

最佳答案

searchLocation 应以 azure-blob://azure-file:// 开头。您评论中的“blob”不正确。

azure-blob://foo/bar.csv 表示“foo” 容器中的“bar.csv” blob。请检查您的存储空间,确保该 blob 存在。

例如,我的 blob URL 为 https://pamelastorage123.blob.core.windows.net/pamelac/test.txt,因此 azure-blob://pamelac/test .txt 是正确的。

StorageExampleApplication.java:

@SpringBootApplication
public class StorageExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(StorageExampleApplication.class, args);
        testRescource();
    }

    public static void testRescource(){
        String searchLocation = "azure-blob://<container-name>/<blob-name>";
        String connectionString = "<Connection string>";
        String endpoint = "https://<account-name>.blob.core.windows.net";

        BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connectionString).endpoint(endpoint).buildClient();
        AzureStorageResourcePatternResolver storageResourcePatternResolver = new AzureStorageResourcePatternResolver(client);

        Resource resource = storageResourcePatternResolver.getResource(searchLocation);
        System.out.println(resource.getFilename());
    }

}

application.properties:

azure.storage.account-name=[storage-account-name]
azure.storage.account-key=[storage-account-access-key]
azure.storage.blob-endpoint=[storage-blob-endpoint-URL]

结果:

enter image description here

关于Spring Batch/Azure 存储帐户 blob 资源 [container"foo", blob ='bar' ] 无法解析为绝对文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67689536/

相关文章:

php - 在 Azure 上运行 ASP.Net Core 和 PHP

database - Azure 数据工厂 - Dynamics 365 复制数据复制 GUID 值而不显示值

java - Spring Batch 和 Spring 集成。无法配置JobListener

spring - 如何在Groovy中使用<mvc:resource/>

Spring 表达式语言不能与 spring aop 一起使用

Azure 管理库,证书被拒绝

spring-batch - 如何使用决策器终止 Spring Batch Split Flow 中的 Step

java - Spring Batch Partitioning 在 itemReader 中注入(inject) stepExecutionContext 参数

java - 准备好bean通过jdbc连接MySQL了吗?

java - Spring 按名称 Autowiring