Azure Storage 云存储使用

Azure About 3,658 words

注意

普通上传:最大4000MB

分片上传:一块Blob中:未提交的Block最多100000片,已提交的Block最多50000片,Blob最大为:大约190.7TiB4000 MiB x 50000片)

构建认证信息

public StorageSharedKeyCredential getCredential(String accountName, String accountKey) {
    return new StorageSharedKeyCredential(accountName, accountKey);
}

构建直接上传客户端

public BlobClient getBlobClient(String endpoint, String containerName, String blobName, String accountName, String accountKey) {
    StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
    return new BlobClientBuilder()
        .endpoint(endpoint)
        .containerName(containerName)
        .blobName(blobName)
        .credential(credential)
        .buildClient();
}

直接上传

public void uploadFile(InputStream is) {
    BlobClient client = getBlobClient(xxx);
    client.upload(BinaryData.fromStream(is), true);
}

下载文件

public void downloadToFile(String blobName, String pathName) {
    BlobClient client = getBlobClient(xxx);
    client.downloadToFile(pathName, true);
}

签名(防盗链)

public String signUrl(String objectName, String process, Duration expireTime) {
    BlobClient client = getBlobClient(xxx);
    OffsetDateTime expiryTime = OffsetDateTime.now().plus(expireTime);
    BlobContainerSasPermission permission = new BlobContainerSasPermission().setReadPermission(true);
    BlobServiceSasSignatureValues blobServiceSasSignatureValues = new BlobServiceSasSignatureValues(expiryTime, permission);
    return client.getBlobUrl() + "?" + client.generateSas(blobServiceSasSignatureValues);
}

分片上传

构建分片上传客户端

public BlockBlobClient getBlockBlobClient(String endpoint, String containerName, String blobName, String accountName, String accountKey) {
    StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
    return new SpecializedBlobClientBuilder()
        .endpoint(endpoint)
        .containerName(containerName)
        .blobName(blobName)
        .credential(credential)
        .buildBlockBlobClient();
}

上传分片

public void uploadPart(int partNumber, String blobName, InputStream is) {
    try {
        BlockBlobClient blockBlobClient = getBlockBlobClient(xxx);
        String md5 = DigestUtils.md5DigestAsHex(String.valueOf(partNumber).getBytes());
        String blockId = Base64.getEncoder().encodeToString(md5.getBytes());
        BinaryData data = BinaryData.fromBytes(is.readAllBytes());
        blockBlobClient.stageBlock(blockId, data);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

合并分片

public void completeMultipartUpload(String blobName) {
    BlockBlobClient blockBlobClient = getBlockBlobClient(xxx);
    BlockList blockList = blockBlobClient.listBlocks(BlockListType.UNCOMMITTED);
    List<Block> uncommittedBlocks = blockList.getUncommittedBlocks();
    if (CollectionUtils.isEmpty(uncommittedBlocks)) {
        throw new RuntimeException("empty uncommitted blocks");
    }
    List<String> blockIds = new ArrayList<>();
    for (int i = 1; i <= uncommittedBlocks.size(); i++) {
        String md5 = DigestUtils.md5DigestAsHex(String.valueOf(i).getBytes());
        String blockId = Base64.getEncoder().encodeToString(md5.getBytes());
        blockIds.add(blockId);
    }
    blockBlobClient.commitBlockList(blockIds, true);
}

终止上传

public void abortMultipartUpload(String blobName) {
    BlockBlobClient blockBlobClient = getBlockBlobClient(xxx);
    boolean result = blockBlobClient.deleteIfExists();
    log.info("abort multipart upload, delete if exists#{}", result);
}

HTTP 文档

https://learn.microsoft.com/en-us/rest/api/storageservices

Java 文档

https://learn.microsoft.com/en-us/java/api/overview/azure/storage

Views: 942 · Posted: 2023-03-30

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh