m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Azure Storage ServiceでBlobの読み書きをJavaで行う

PHPではなく

Java

pom.xml

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage -->
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-storage</artifactId>
            <version>5.0.0</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        
    </dependencies>

実装

一覧出力
import java.net.URISyntaxException;
import java.security.InvalidKeyException;

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.ListBlobItem;

public class Sample01 {
	
	// 接続文字列
	public static final String storageConnectionString =
		    "DefaultEndpointsProtocol=http;" +
		    "AccountName=自分のアカウント名;" +
		    "AccountKey=自分のアクセスキー";
	
	/**
	 * 指定されたコンテナのblob一覧を取得するサンプル
	 * @param args
	 * @throws URISyntaxException 
	 * @throws InvalidKeyException 
	 * @throws StorageException 
	 */
	public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException {
		
		// ストレージアカウントオブジェクトを取得
		CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
		
		// Blobクライアントオブジェクトを取得
		CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
		
		// blobの指定したtest01コンテナを取得
		CloudBlobContainer container = blobClient.getContainerReference("test01");
		
		// blob情報を出力する
		for (ListBlobItem blobItem : container.listBlobs()) {
	        System.out.println(blobItem.getUri());
	    }
	}

}
読み込み
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;

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;

public class Sample02 {
	
	// 接続文字列
	public static final String storageConnectionString =
		    "DefaultEndpointsProtocol=http;" +
		    "AccountName=自分のアカウント名;" +
		    "AccountKey=自分のアクセスキー";
	
	/**
	 * 指定されたコンテナのblobを読み込むサンプル
	 * @param args
	 * @throws URISyntaxException 
	 * @throws InvalidKeyException 
	 * @throws StorageException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException, IOException {
		
		// ストレージアカウントオブジェクトを取得
		CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
		
		// Blobクライアントオブジェクトを取得
		CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
		
		// blobの指定したtest01コンテナを取得
		CloudBlobContainer container = blobClient.getContainerReference("test01");
		
		// blobデータを読み込む
		CloudBlockBlob blob = container.getBlockBlobReference("testdata_20170716233116.txt");
		InputStream input = blob.openInputStream();
		InputStreamReader inr = new InputStreamReader(input, "UTF-8");
		
		// 読み込んだ値を出力する
		String utf8str = org.apache.commons.io.IOUtils.toString(inr);
		System.out.println(utf8str);

	}
}
書き込み
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;

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

public class Sample03 {
	
	// 接続文字列
	public static final String storageConnectionString =
		    "DefaultEndpointsProtocol=http;" +
		    "AccountName=自分のアカウント名;" +
		    "AccountKey=自分のアクセスキー";
	
	/**
	 * 指定されたコンテナのblobを書き込むサンプル
	 * @param args
	 * @throws URISyntaxException 
	 * @throws InvalidKeyException 
	 * @throws StorageException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws InvalidKeyException, URISyntaxException, StorageException, IOException {
		
		// ストレージアカウントオブジェクトを取得
		CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
		
		// Blobクライアントオブジェクトを取得
		CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
		
		// blobの指定したtest01コンテナを取得
		CloudBlobContainer container = blobClient.getContainerReference("test01");
		
		// blobデータへ書き込む準備
		CloudBlockBlob blob = container.getBlockBlobReference("testdata_20170725000000.txt");
		BlobOutputStream output = blob.openOutputStream();
		
		// データを書き込む
		output.write("aaaaaaa".getBytes());
		output.close();
	}
}

所感

今回というかファイルをローカルに扱うのは準備が面倒な感じがしたのでテキストデータを読み込んだり、書き込んだりする方法を記載。
PHPの場合はそのまま読み込んでも良いけど、Javaの場合は型が厳密なのでそこそこ手段が別れているような感じです。