- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 http 触发的 azure 函数(用 Java 编写),我想在其中访问 Blob 存储。该代码在 maven 下编译,但是当我在本地运行它并从 CURL 发送帖子时,由于缺少 com.microsoft.azure.storage.CloudStorageAccount 导致的 ClassNotFound 异常,运行时崩溃。 azure-storage(版本 6.0.0)在 POM 文件中列为依赖项。相关的 .jar 文件应该放在哪里以便函数可以看到它们?
任何有关 Java azure 函数的见解将不胜感激。
最佳答案
根据您的需求,我建议您按照这个official tutorial创建、运行和部署您的 java azure 函数
。
函数类:
package com.fabrikam.functions;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.ExecutionContext;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
/**
* Hello function with HTTP Trigger.
*/
public class Function {
// Configure the connection-string with your values
public static final String storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName=***;" +
"AccountKey=***";
@FunctionName("hello")
public String hello(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) String req,
ExecutionContext context) {
try {
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container.
// The container name must be lower case
CloudBlobContainer container = blobClient.getContainerReference(req);
// Create the container if it does not exist.
container.createIfNotExists();
return String.format("Hello, I get container name : %s!", container.getName());
} catch (Exception e) {
// Output the stack trace.
e.printStackTrace();
return "Access Error!";
}
}
}
Pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fabrikam.functions</groupId>
<artifactId>fabrikam-functions</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Azure Java Functions</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<functionAppName>fabrikam-functions-20171017112209094</functionAppName>
</properties>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-java-core</artifactId>
<version>1.0.0-beta-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>6.0.0</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>0.1.4</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<configuration>
<resourceGroup>java-functions-group</resourceGroup>
<appName>${functionAppName}</appName>
<region>westus2</region>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>beta</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/azure-functions/${functionAppName}
</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>host.json</include>
<include>local.settings.json</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后使用命令mvn clean package
将你的maven项目打包成jar包。
使用命令mvn azure-functions:run
在本地运行您的azure函数。
更新答案:
我运行了我的 azure 函数并重现了与您所说的相同的异常。
java.lang.NoClassDefFoundError: com/microsoft/azure/storage/CloudStorageAccount
Exception:
Stack: java.lang.reflect.InvocationTargetException
[10/25/2017 2:48:44 AM] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[10/25/2017 2:48:44 AM] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[10/25/2017 2:48:44 AM] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[10/25/2017 2:48:44 AM] at java.lang.reflect.Method.invoke(Method.java:498)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:19)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:34)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:40)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.handler.MessageHandler.handle(MessageHandler.java:41)
[10/25/2017 2:48:44 AM] at com.microsoft.azure.webjobs.script.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:84)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1386)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
[10/25/2017 2:48:44 AM] at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
[10/25/2017 2:48:44 AM] Caused by: java.lang.NoClassDefFoundError: com/microsoft/azure/storage/CloudStorageAccount
[10/25/2017 2:48:44 AM] at com.fabrikam.functions.Function.hello(Function.java:26)
[10/25/2017 2:48:44 AM] ... 16 more
[10/25/2017 2:48:44 AM] Caused by: java.lang.ClassNotFoundException: com.microsoft.azure.storage.CloudStorageAccount
[10/25/2017 2:48:44 AM] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[10/25/2017 2:48:44 AM] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[10/25/2017 2:48:44 AM] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[10/25/2017 2:48:44 AM] ... 17 more
[10/25/2017 2:48:44 AM] .
[10/25/2017 2:48:44 AM] Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '3450abda-99a0-4d75-add2-a7bc48a0cb51'
[10/25/2017 2:48:44 AM] System.Private.CoreLib: Exception while executing function: Functions.hello. System.Private.CoreLib: Result:
经过一番研究,发现是因为打包的jar没有依赖的jar包
造成的。
因此,我将如下配置片段添加到我的 pom.xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Your main class path</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
然后请使用命令mvn-clean-package
,您将看到生成的两个jar文件。
一是不包含依赖jar包
,二是包含依赖jar包
。
将fabrikam-functions-1.0-SNAPSHOT-jar-with-dependencies
jar移动到路径:${project.basedir}/target/azure-functions/${function-应用程序名称}/
对我来说,它看起来像E:\TestAzureFunction\fabrikam-functions\target\azure-functions\fabrikam-functions-20171017112209094
。
不要忘记将 jar 重命名为 fabrikam-functions-1.0-SNAPSHOT
。
最后,我成功运行了azure函数,并通过url获取输出结果:http://localhost:7071/api/mongo
。
另外,你可以引用这个github doc有关 azure function maven 插件
的更多配置详细信息。
希望对您有帮助。
关于java - 我如何使用Java中的azure函数的blob存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841386/
我正在尝试从 Azure 容器中删除 blob。我能够连接到它并列出此问题中代码后面的所有 blob:Upload and Delete Azure Storage Blob using azure-
我正在尝试从 Azure 容器中删除 blob。我能够连接到它并列出此问题中代码后面的所有 blob:Upload and Delete Azure Storage Blob using azure-
运行我的 azure 函数(用于读取 azure blob 存储)后出现错误。 错误是 ID 0dad768d-36d4-4c1a-85ae-2a5122533b3c fail: Func
运行我的 azure 函数(用于读取 azure blob 存储)后出现错误。 错误是 ID 0dad768d-36d4-4c1a-85ae-2a5122533b3c fail: Func
我正在使用 C# 控制台应用程序 (.NET Core 3.1) 从 Azure Blob 存储读取大量图像文件并生成这些图像的缩略图。新图像将保存回 Azure,并将 Blob ID 存储在我们的数
我没有在网上看到任何有关如何获取位于 BlobContainerClient 内特定目录内的所有 blob 的示例。 以前,我使用的是 Microsoft.Azure.Storage 软件包,但这些软
我正在使用 C# 控制台应用程序 (.NET Core 3.1) 从 Azure Blob 存储读取大量图像文件并生成这些图像的缩略图。新图像将保存回 Azure,并将 Blob ID 存储在我们的数
我没有在网上看到任何有关如何获取位于 BlobContainerClient 内特定目录内的所有 blob 的示例。 以前,我使用的是 Microsoft.Azure.Storage 软件包,但这些软
我正在编写一些代码,允许用户使用麦克风录制自己的声音,然后将录音上传到 Azure Blob 存储。 为了录制音频,我使用类似于下面的代码 let recordedBlobs = []; this.m
当前使用:https://github.com/Azure/azure-sdk-for-go 概述:我当前正在从 azure blob 存储中下载一个 blob,解析该 blob,然后将转录的 blo
正在观看 this video about how to design Tinder ,在 06:50 提出了关于文件与 BLOBS 的观点。 我想知道大二进制文件和 BLOB(二进制大对象)之间有什
目前我有 hibernate JPA HSQLDB 来自动创建我的数据库表。 如何告诉 JPA 或 Hibernate 将字符串保存为 clob/blob 字段?即一个很长的字符串。到目前为止我找不
我有一个一维 NumPy 数组,其中包含一些“坏”值。我想剔除它们。 每个坏值的邻居只是“顽皮”,但我也想剔除它们。 对不良值的可靠测试是询问: arr<0.1 但是,(我能想到的)对于顽皮值的唯一可
查看有关获取 Blob 和获取 Blob 属性的 MSDN 文档。两个请求看起来相同 "https://myaccount.blob.core.windows.net/mycontainer/mybl
我有 2 个 Blob 存储,一个在 eastus,一个在 canadaeast,我想将一个 .vhd 从 eastus 复制到 canadaeast。我去了 eastus,在我想要复制的 blob
所以场景如下: 我有多个 Web 服务实例,用于将 blob 数据写入 Azure 存储。我需要能够根据收到的时间将 blob 分组到容器(或虚拟目录)中。偶尔(最坏的情况是每天)旧的 blob 会被
在 Azure Blobstorage 中,我有 100 个 Blob,但我只想列出前 10 个 Blob。我该怎么做? 我写的{maxResults:1}没有任何效果,它仍然列出了我所有的 Blob
我们当前的代码使用 Azure SDK 1.8,为了生成共享访问签名,它将首先调用 CloudBlobContainer.GetBlobReference(),然后调用 CloudBlob.GetSh
我有大量文件存储在公共(public) Azure blob 容器中,所有这些文件都通过我的 ASP.NET MVC Web 应用程序中的 HTML 直接引用。例如,blob 存储中一个图像的路径如下
我有一个 NodeJS 后端,它使用 Microsoft 的官方 Blob 存储库 (@azure/storage-blob) 来管理我的 Blob 存储: https://www.npmjs.com
我是一名优秀的程序员,十分优秀!