当前位置: 移动技术网 > IT编程>开发语言>Java > Springboot进行Azure Blob Storage的开发

Springboot进行Azure Blob Storage的开发

2020年07月17日  | 移动技术网IT编程  | 我要评论

java语言的开发
1、引入pom文件

		<dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-storage</artifactId>
            <version>8.4.0</version>
        </dependency>

2、demo

package com.example.blob.storage;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;
import com.microsoft.azure.storage.blob.ListBlobItem;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.text.MessageFormat;


/**
 * @author 
 * @version 1.0
 * @date 2020-07-14 16:16
 */
public class AzureDemo {
    private static String ACCOUNT_NAME = "docblobsit01";
    private static String ACCOUNT_KEY = "xxx";
    private static String END_POINT = "core.chinacloudapi.cn";
    private static String PROTOCOL = "https";
    private static String format = "DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}";

    private static CloudStorageAccount storageAccount = null;
    private static CloudBlobClient blobClient = null;
    private static CloudBlobContainer container = null;

    public static void main(String[] args) {
        initAzure("test");
        listBlobs(null);
       // uploadFile();
    }

    public static void listBlobs(String perfix) {
        /**
         * 第一个参数, container中blob的前缀, 可以是文件夹的前缀, 也可以是blob的前缀
         * 第二个参数, 是否展开文件夹中的文件, 如container中无文件夹, 则会列出所有blob
         */
        Iterable<ListBlobItem> blobItems = container.listBlobs(null, true);
        for (ListBlobItem blobItem : blobItems) {
            String uri = blobItem.getUri().toString();
            System.out.println(uri);
        }
    }

    public static void uploadFile(File file) {
        try {
            // 构建目标BlockBlob对象
            CloudBlockBlob blob = container.getBlockBlobReference("20191012/aaa.txt");
            // 将本地文件上传到Azure Container
            blob.uploadFromFile(file.getPath());
            // 获得获得属性
            blob.downloadAttributes();
            // 获得上传后的文件大小
            long blobSize = blob.getProperties().getLength();
            // 获得本地文件大小
            long localSize = file.length();
            // 校验
            if (blobSize != localSize) {
                System.out.println("校验失败...上传失败");
                // 删除blob
                blob.deleteIfExists();
            } else {
                System.out.println("上传成功");
            }
        } catch (URISyntaxException | StorageException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadFile(String blobPath, String targetPath) {
        String finalPath = targetPath.concat(blobPath);
        try {
            // 传入要blob的path
            CloudBlockBlob blob = container.getBlockBlobReference(blobPath);
            // 传入目标path
            blob.downloadToFile(finalPath);
        } catch (URISyntaxException | StorageException | IOException e) {
            e.printStackTrace();
        }
    }


    public static void initAzure(String containerName) {
        try {
            // 获得StorageAccount对象
            storageAccount = CloudStorageAccount.parse(MessageFormat.format(format, PROTOCOL, ACCOUNT_NAME, ACCOUNT_KEY, END_POINT));
            // 由StorageAccount对象创建BlobClient
            blobClient = storageAccount.createCloudBlobClient();
            // 根据传入的containerName, 获得container实例
            container = blobClient.getContainerReference(containerName);
        } catch (URISyntaxException | InvalidKeyException | StorageException e) {
            e.printStackTrace();
        }
    }
}


container name的名字是test
在这里插入图片描述
执行之后,可以看到下面的运行结果
在这里插入图片描述

欲关注更多微软云存储Azure Blob Storage的相关知识,请扫描下方二维码进行关注:
在这里插入图片描述

本文地址:https://blog.csdn.net/weixin_41709748/article/details/107341760

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网