jiangping
2025-06-17 64fa2c33cd645e86d4e2a8c34c7881ea4aa678cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.doumee.core.utils.azure;
 
import com.alibaba.fastjson.JSONObject;
import com.azure.core.http.rest.Response;
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
public class AzureBlobUtil {
 
    public    String connectionString;
    public    BlobServiceClient blobServiceClient;
    public     String accountName;
    public   String accountKey;
    public    String endpoint;
    public   String container;
 
    public   BlobContainerClient blobContainerClient ;
 
    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.blobServiceClient = new BlobServiceClientBuilder()
//                    .endpoint(endpoint)
//                    .sasToken(accountKey)
//                    .buildClient();
           initClient();
        }catch (Exception e) {
            e.printStackTrace();
            log.error("初始化BLOB客户端异常0=========="+e.getMessage());
        }
 
    }
 
    public   void initClient( )   {
        if(this.blobServiceClient!=null){
            log.error("初始化BLOB客户端已完成,无需重复授权==========");
            return;
        }
        try {
            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("初始化BLOB客户端异常1=========="+e.getMessage());
        }
    }
 
    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 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!=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;
    }*/
    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;
    }
}