项目中使用了微软云提供的服务,包括虚机、存储、虚拟网络、存储账号等等。官网上有1元体验的服务,于是申请了一个账号进行了测试。本系列文章主要用于记录/学习和使用过程中遇到的一些问题和解决步骤,作为后续的技术储备和问题备忘。
目前,非官方总结的文档数量和覆盖度还是不够高,所以目前还是只能依赖官网的文档进行使用和开发。好在相对还是比较完整的,尽管有些地方存在说明不够详细的情况。可以参考的文档主要有这两个,一个是官网的中文文档,另一个是中国区内,由世纪互联提供的文档。
微软中国:/zh-cn/azure/storage/storage-java-how-to-use-blob-storage
世纪互联:/zh-cn/#pivot=services
暂时主要记录Blob使用相关的部分,其他的稍后进行补充。
1、存储账号创建
/zh-cn/azure/storage/storage-create-storage-account
2、java应用程序访问:
/zh-cn/azure/storage/storage-java-how-to-use-blob-storage
其中,关于存储账号连接,关键代码如下:
设置 Azure 存储连接字符串
Azure 存储客户端使用存储连接字符串来存储用于访问数据管理服务的终结点和凭据。 在客户端应用程序中运行时,必须提供以下格式的存储连接字符串,并对 AccountName 和 AccountKey 值使用Azure 门户中列出的存储帐户的名称和存储帐户的主访问密钥。 下面的示例演示如何声明一个静态字段以保存连接字符串。
// Define the connection-string with your valuespublic static final String storageConnectionString ="DefaultEndpointsProtocol=http;" +"AccountName=your_storage_account;" +"AccountKey=your_storage_account_key";
查看方式:微软云门户->左侧列表中,点存储账号->选择要使用的存储账号->点击访问密钥,如下图所示,右边的2个默认密钥就是AccountKey;AccountName就是存储账号名称,对应下图中的blobstoretest,另外,上面代码中的storageConnectionString,是指key右侧的连接字符串。需要把对应的信息都替换到上述代码中的对应内容。
所用jar包已经上传至csdn资源,/download/liuhuoxingkong/9942484。
完整java工程代码也已经导入,按照上述文章说明,替换对应的连接串,账户和密钥即可。/download/liuhuoxingkong/9965588
之前上传的工程有误。。没有带上简单的示例代码==
重新上传了工程,主要代码如下:
package com.my.test.dataarchive;import com.microsoft.azure.storage.CloudStorageAccount;import com.microsoft.azure.storage.blob.CloudBlobClient;import com.microsoft.azure.storage.blob.CloudBlobContainer;import com.microsoft.azure.storage.blob.ListBlobItem;public class GPSDataArchive {public static final String storageConnectionString = "DefaultEndpointsProtocol=https;"+"AccountName=blobstoretest;"+"AccountKey=yourkey";public static void main(String[] args) {try{// Retrieve storage account from connection-string.CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);// Create the blob client.CloudBlobClient blobClient = storageAccount.createCloudBlobClient();// Retrieve reference to a previously created container.CloudBlobContainer container = blobClient.getContainerReference("historygps");//1. 列举container中的blob对象for (ListBlobItem blobItem : container.listBlobs()) {System.out.println(blobItem.getUri());}//2. 下载blob// for (ListBlobItem blobItem : container.listBlobs()) {// // If the item is a blob, not a virtual directory.// if (blobItem instanceof CloudBlob) {// // Download the item and save it to a file with the same name.// CloudBlob blob = (CloudBlob) blobItem;// blob.download(new FileOutputStream("D:\\tools\\azure\\download\\" + blob.getName()));// }// }//3. 删除blob// Retrieve reference to a blob named "myimage.jpg".// CloudBlockBlob blob = container.getBlockBlobReference("image1.jpg");// // Delete the blob.// boolean delete_flag = blob.deleteIfExists();// System.out.println("delete flag="+delete_flag);//4. 删除container// boolean con_delete_flag = container.deleteIfExists();// System.out.println("container delete flag="+con_delete_flag);//5. 上传blob// final String filePath = "D:\\tools\\azure\\download\\image1.jpg";// CloudBlockBlob blob = container.getBlockBlobReference("image2.jpg");// File source = new File(filePath);// blob.upload(new FileInputStream(source), source.length());}catch (Exception e){// Output the stack trace.e.printStackTrace();}}}