| | |
| | | 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 |
| | |
| | | |
| | | 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("初始化BLOB客户端异常0=========="+e.getMessage()); |
| | | } |
| | | |
| | | } |
| | | |
| | | public void initClient( ) { |
| | | if(this.blobServiceClient!=null){ |
| | | log.error("初始化BLOB客户端已完成,无需重复授权=========="); |
| | | 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("初始化BLOB客户端异常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; |
| | | } |
| | | |
| | | } |