简单概念
Blob Storage
Azure Blob Storage是用来存放文本,图片,视频等非架构华数据从存储服务, 我们可以在任何地方通过http/https协议访问Blob Storage
Azure Blob Storage的存储结构
Azure Storage Account
Account是用来管理Azure Storage的一个命名空间,主要用来控制存储数据的访问权限和计费. Account控制Blob, Queue, File和Table的访问权限.
要想使用Blob Storage, 必须要有一个Storage Account账户
Container
Container中包含一组资源(Blob), 所有的Blob都必须存在于Container中, 概念可以类比window系统的C/D/E/F盘(我是这么理解的, 如有错,轻喷)
一个Storage Account可以控制不同Container的访问权限. 可以包含无数个Container, 每个Container中也可以包含无数个Blob
注意: Container的名字是小写
Blob
一个Blob代表一个文件, Blob划分为三种类型
- Block Blob: 主要存储静态文件, 如图片, 视频, 文档.
- Append Blob: 与Block Blob类似, 优化了append操作, 主要应用场景就是日志存储.
- Page Blob: 针对频繁的读写操作做了优化, 如Azure上虚拟机的磁盘使用的就是Page Blob.
操作
Maven依赖
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-data-lake-store-sdk</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>11.0.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>8.3.0</version>
</dependency>
创建Container对象
public static void initAzure(String containerName) {
String ACCOUNT_NAME = <ACCOUNT NAME>;
String ACCOUNT_KEY = <ACCOUNT KEY>;
String END_POINT = <END POINT>;
String PROTOCOL = "https";
String format = "DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2};EndpointSuffix={3}";
CloudStorageAccount storageAccount = null;
CloudBlobClient blobClient = null;
CloudBlobContainer container = null;
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();
}
}
获取Blob列表
public static void listBlobs() {
/**
* 第一个参数, container中blob的前缀, 可以是文件夹的前缀, 也可以是blob的前缀
* 第二个参数, 是否展开文件夹中的文件
* false: 如Container中只有blob就全部列出; 如有文件夹,则只列出文件夹名
* true: 列出所有blob
*/
Iterable<ListBlobItem> blobItems = container.listBlobs(null,false);
for (ListBlobItem blobItem : blobItems) {
String uri = blobItem.getUri().toString();
System.out.println(uri);
}
}
下载Blob
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 uploadFile(File file) {
try {
// 构建目标BlockBlob对象
CloudBlockBlob blob = container.getBlockBlobReference("20191012/a");
// 将本地文件上传到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();
}
}
测试
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 jiale.he
* @date 2019/10/12
*/
public class AzureDemo {
private static String ACCOUNT_NAME = "***";
private static String ACCOUNT_KEY = "***";
private static String END_POINT = "***";
private static String PROTOCOL = "***";
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("***");
listBlobs(null);
}
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();
}
}
}
结果: