From 05db93c48c69b1fc5db8bb2fce4c37014e92e450 Mon Sep 17 00:00:00 2001
From: doum <doum>
Date: 星期二, 19 八月 2025 10:01:30 +0800
Subject: [PATCH] 代码初始化
---
server/src/main/java/com/doumee/core/utils/azure/AzureBlobUtil.java | 146 +++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 129 insertions(+), 17 deletions(-)
diff --git a/server/src/main/java/com/doumee/core/utils/azure/AzureBlobUtil.java b/server/src/main/java/com/doumee/core/utils/azure/AzureBlobUtil.java
index 4f1c383..566a07e 100644
--- a/server/src/main/java/com/doumee/core/utils/azure/AzureBlobUtil.java
+++ b/server/src/main/java/com/doumee/core/utils/azure/AzureBlobUtil.java
@@ -1,17 +1,29 @@
package com.doumee.core.utils.azure;
+import com.alibaba.fastjson.JSONObject;
import com.azure.core.http.rest.Response;
-import com.azure.storage.blob.BlobClient;
-import com.azure.storage.blob.BlobContainerClient;
-import com.azure.storage.blob.BlobServiceClient;
-import com.azure.storage.blob.BlobServiceClientBuilder;
-import com.azure.storage.blob.models.BlobRequestConditions;
-import com.azure.storage.blob.models.BlockBlobItem;
+import com.azure.core.http.rest.RestProxy;
+import com.azure.identity.DefaultAzureCredentialBuilder;
+import com.azure.storage.blob.*;
+import com.azure.storage.blob.models.*;
import com.azure.storage.blob.options.BlobParallelUploadOptions;
+import com.azure.storage.blob.options.BlobUploadFromFileOptions;
+import com.azure.storage.blob.sas.BlobContainerSasPermission;
+import com.azure.storage.blob.specialized.BlobInputStream;
+import com.azure.storage.common.StorageSharedKeyCredential;
+import io.netty.handler.logging.ByteBufFormat;
+import io.netty.handler.logging.LogLevel;
+import reactor.util.context.ContextView;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
+import javax.servlet.ServletOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.nio.file.Path;
+import java.time.OffsetDateTime;
@Data
@Slf4j
@@ -28,55 +40,155 @@
public AzureBlobUtil(String accountName, String accountKey, String endpoint, String container){
try {
+
this.accountKey = accountKey;
this.accountName = accountName;
this.endpoint = endpoint;
this.container = container;
- this.connectionString = "DefaultEndpointsProtocol=https;AccountName=" + this.accountName + ";AccountKey=" + this.accountKey + ";EndpointSuffix=" + endpoint;
- this.blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
- this.blobContainerClient = this.blobServiceClient.getBlobContainerClient(this.container);
+// this.connectionString = "DefaultEndpointsProtocol=https;AccountName=" + this.accountName + ";AccountKey=" + this.accountKey + ";EndpointSuffix=" + endpoint;
+// this.blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
+// this.blobServiceClient = new BlobServiceClientBuilder()
+// .endpoint(endpoint)
+// .sasToken(accountKey)
+// .buildClient();
+ initClient();
}catch (Exception e) {
e.printStackTrace();
+ log.error("鍒濆鍖朆LOB瀹㈡埛绔紓甯�0=========="+e.getMessage());
}
}
public void initClient( ) {
+ if(this.blobServiceClient!=null){
+ log.error("鍒濆鍖朆LOB瀹㈡埛绔凡瀹屾垚锛屾棤闇�閲嶅鎺堟潈==========");
+ return;
+ }
try {
- this.blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
- this.blobContainerClient = this.blobServiceClient.getBlobContainerClient(this.container);
+ StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
+ // Azure SDK client builders accept the credential as a parameter
+ this.blobServiceClient = new BlobServiceClientBuilder()
+ .endpoint(endpoint)
+ .credential(credential)
+ .buildClient();
+ this.blobContainerClient = this.blobServiceClient.createBlobContainerIfNotExists(this.container);
+ this.blobContainerClient.setAccessPolicy(PublicAccessType.CONTAINER, null);
+// this.blobContainerClient = this.blobServiceClient.getBlobContainerClient(this.container);
+// String endpointString = String.format("https://%s.blob.core.windows.net/%s",
+// accountName, container);
+//
+// this.blobContainerClient = new BlobContainerClientBuilder()
+// .endpoint(endpointString)
+// .credential(new DefaultAzureCredentialBuilder().build())
+// .buildClient();
}catch (Exception e) {
e.printStackTrace();
+ log.error("鍒濆鍖朆LOB瀹㈡埛绔紓甯�1=========="+e.getMessage());
}
}
- public void uploadFile(String fileName, InputStream data) {
+
+ public void downloadBlobToStream(String blob, ServletOutputStream outputStream) {
+ BlobClient blobClient =this.getBlobContainerClient().getBlobClient(blob);
+ blobClient.downloadStream(outputStream);
+ }
+ public BlobInputStream readBlobFromStream(String blob) {
+ BlobClient blobClient =this.getBlobContainerClient().getBlobClient(blob);
+ // Opening a blob input stream allows you to read from a blob through a normal
+ // stream interface
+ try (BlobInputStream blobStream = blobClient.openInputStream()) {
+ blobStream.read();
+ return blobStream ;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+ public BlobInputStream readBlobFromStream(BlobClient blobClient) {
+ // Opening a blob input stream allows you to read from a blob through a normal
+ // stream interface
+ try (BlobInputStream blobStream = blobClient.openInputStream()) {
+ blobStream.read();
+ return blobStream;
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+ public boolean upload(String fileName, InputStream data) {
try {
if(getBlobContainerClient() == null){
initClient();
}
BlobClient client = this.blobContainerClient.getBlobClient(fileName);
client.upload(data, data.available(), true);
+ return true;
}catch (Exception e) {
e.printStackTrace();
}
+
+ return false;
}
- public boolean uploadFileWithResponse(String fileName, InputStream inputStream) {
+
+ public void uploadBlockBlobWithTransferOptions(BlobContainerClient blobContainerClient, Path filePath) {
+ String fileName = filePath.getFileName().toString();
+ BlobClient blobClient = blobContainerClient.getBlobClient(fileName);
+
+ ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
+ .setBlockSizeLong((long) (4 * 1024 * 1024)) // 4 MiB block size
+ .setMaxConcurrency(2)
+ .setMaxSingleUploadSizeLong((long) 8 * 1024 * 1024); // 8 MiB max size for single request upload
+
+ BlobUploadFromFileOptions options = new BlobUploadFromFileOptions(filePath.toString());
+ options.setParallelTransferOptions(parallelTransferOptions);
+
try {
+ Response<BlockBlobItem> blockBlob = blobClient.uploadFromFileWithResponse(options, null, null);
+ } catch (UncheckedIOException ex) {
+ System.err.printf("Failed to upload from file: %s%n", ex.getMessage());
+ }
+ }
+ /*public boolean uploadFileWithResponse(String fileName, InputStream inputStream) {
+ try {
+ log.error("BLOB涓婁紶寮�鍙戯細........"+fileName);
if(getBlobContainerClient() == null){
+ log.error("BLOB涓婁紶寮�鍙戯細........鍒濆鍖栧鎴风");
initClient();
}
BlobParallelUploadOptions options = new BlobParallelUploadOptions(inputStream, inputStream.available());
options.setRequestConditions(new BlobRequestConditions().setIfNoneMatch("*"));
Response<BlockBlobItem> rsp = this.blobContainerClient.getBlobClient(fileName).uploadWithResponse(options, null, null);
- if(rsp.getStatusCode()==201) {
- log.info("涓婁紶鎴愬姛锛�........"+fileName);
+ if(rsp!=null && rsp.getStatusCode()==201) {
+ log.info("BLOB涓婁紶鎴愬姛锛�........"+fileName);
+ log.error("BLOB涓婁紶鎴愬姛锛�........"+fileName);
return true;
}
+ log.error("BLOB涓婁紶澶辫触锛�........"+ JSONObject.toJSONString(rsp));
}catch (Exception e) {
e.printStackTrace();
- log.info("涓婁紶澶辫触锛�........"+e.getMessage());
+ log.error("BLOB涓婁紶澶辫触锛�........"+e.getMessage());
+ }
+ return false;
+ }*/
+ public boolean uploadFileWithResponseAndSize(String fileName, InputStream inputStream,Integer size) {
+ try {
+ log.error("BLOB涓婁紶寮�鍙戯細........"+fileName+"===============size:"+size);
+ if(getBlobContainerClient() == null){
+ log.error("BLOB涓婁紶寮�鍙戯細........鍒濆鍖栧鎴风");
+ initClient();
+ }
+ BlobParallelUploadOptions options = new BlobParallelUploadOptions(inputStream,(size!=null && size>0)?size: inputStream.available());
+ options.setRequestConditions(new BlobRequestConditions().setIfNoneMatch("*"));
+ Response<BlockBlobItem> rsp = this.blobContainerClient.getBlobClient(fileName).uploadWithResponse(options, null, null);
+ if(rsp!=null && rsp.getStatusCode()==201) {
+ log.info("BLOB涓婁紶鎴愬姛锛�........"+fileName);
+ log.error("BLOB涓婁紶鎴愬姛锛�........"+fileName);
+ return true;
+ }
+ log.error("BLOB涓婁紶澶辫触锛�........"+ JSONObject.toJSONString(rsp));
+ }catch (Exception e) {
+ e.printStackTrace();
+ log.error("BLOB涓婁紶澶辫触锛�........"+e.getMessage());
}
return false;
}
-
}
\ No newline at end of file
--
Gitblit v1.9.3