我有使用 Spring Boot 项目 爪哇8 摇篮4.8 我在其中添加了 azure-identity 和 azure-storage-blob 的依赖项。
这是ReportUploader文件
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
@Component
public class ReportUploader {
private final BlobServiceClient blobServiceClient;
public ReportUploader(@Value("${azure.storageAccountName}") String storageAccountName) {
this.blobServiceClient = new BlobServiceClientBuilder()
.endpoint(String.format("https://%s.blob.core.windows.net", storageAccountName))
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
}
public void upload(File sourceFile, String destFileName, String bucketName) {
toAzureBlobStorage(sourceFile, destFileName, bucketName);
}
private void toAzureBlobStorage(File sourceFile, String destFileName, String bucketName) {
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(bucketName);
if (!containerClient.exists())
containerClient = blobServiceClient.createBlobContainer(bucketName);
containerClient.getBlobClient(destFileName).uploadFromFile(sourceFile.getAbsolutePath(), true);
}
}
但它给了我错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportUploader' defined in file a/b/c/xyz.class Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate abc: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: io/netty/handler/logging/ByteBufFormat
这是我的 gradle 文件
compile("com.fasterxml.jackson.core:jackson-databind:2.13.5")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.13.5")
compile("com.fasterxml.jackson.core:jackson-core:2.13.5")
compile("org.springframework.kafka:spring-kafka")
testCompile("org.springframework.kafka:spring-kafka-test")
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '3.10.2'
compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
compile group: 'com.fasterxml.uuid', name: 'java-uuid-generator', version: '3.1.0'
testCompile 'io.cucumber:cucumber-java:4.7.4'
testCompile 'io.cucumber:cucumber-junit:4.7.4'
compile group: 'io.cucumber', name: 'cucumber-spring', version: '4.8.0'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.307'
compile group: 'joda-time', name: 'joda-time', version: '2.3'
compile 'org.springframework.boot:spring-boot-starter-log4j2'
compile group: 'com.azure', name: 'azure-identity', version: '1.2.1'
compile group: 'com.azure', name: 'azure-storage-blob', version: '12.7.0'
谁能告诉我哪个版本与 Java 8 兼容?
我使用 DefaultCredentials 在 Spring Boot 项目中使用 Java 8 成功将 blob 上传到 Azure 存储。
代码:
UploadFile.java:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@RestController
@RequestMapping("/upload-file")
public class UploadFile {
private final ReportUploader reportUploader;
@Autowired
public UploadFile(ReportUploader reportUploader) {
this.reportUploader = reportUploader;
}
@PostMapping
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
try {
File tempFile = File.createTempFile("temp", null);
file.transferTo(tempFile);
String originalFileName = file.getOriginalFilename();
reportUploader.upload(tempFile, originalFileName, "<container_name>");
tempFile.delete();
System.out.println("Blob uploaded successfully: " + originalFileName);
return ResponseEntity.ok("File uploaded successfully");
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(500).body("Failed to upload the file");
}
}
}
ReportUploader.java:
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
@Component
public class ReportUploader {
private final BlobServiceClient blobServiceClient;
public ReportUploader(@Value("${azure.storageAccountName}") String storageAccountName) {
this.blobServiceClient = new BlobServiceClientBuilder()
.endpoint(String.format("https://%s.blob.core.windows.net", storageAccountName))
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
}
public void upload(File sourceFile, String destFileName, String bucketName) {
toAzureBlobStorage(sourceFile, destFileName, bucketName);
}
private void toAzureBlobStorage(File sourceFile, String destFileName, String bucketName) {
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(bucketName);
if (!containerClient.exists())
containerClient = blobServiceClient.createBlobContainer(bucketName);
containerClient.getBlobClient(destFileName).uploadFromFile(sourceFile.getAbsolutePath(), true);
}
}
build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '2.6.4'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.thinkconstructive'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '8'
}
repositories {
mavenCentral()
}
dependencies {
implementation group: 'com.azure', name: 'azure-identity', version: '1.2.1'
implementation group: 'com.azure', name: 'azure-storage-blob', version: '12.7.0'
implementation 'io.netty:netty-all:4.1.66.Final'
implementation 'io.socket:socket.io-client:1.0.0'
implementation 'com.corundumstudio.socketio:netty-socketio:1.7.16'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
application.properties:
azure.storageAccountName=<storageAccount_name>
邮递员输出:
我使用 Postman 成功将 blob 上传到 Azure 存储,如下所示。
本地输出:
Azure 门户:
blob 已成功上传到 Azure 存储,如下所示。