1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AzureのストレージにJavaでアップロードとダウンロードメモ

Last updated at Posted at 2017-09-01

はじめに

AzureのストレージにJavaでアップロードと、
ダウンロードするメモです。

詳細は調査して、追記していく予定です。

jarのインストール(Maven)

pom.xml
    <dependency>
	    <groupId>com.microsoft.azure</groupId>
	    <artifactId>azure-storage</artifactId>
	    <version>5.5.0</version>
	</dependency>

認証定数の定義

java
	public static final String storageConnectionString = "DefaultEndpointsProtocol=http;" +
			"AccountName=[AccountName];" +
			"AccountKey=[AcountKey]";

アップロード

java
    // アップロード処理
    private static void upload() throws Exception{
    	CloudBlobContainer container = auth();

    	// アップロード処理
    	File file = new File("[アップロードファイルパス]");
    	FileInputStream fis = new FileInputStream(file);
    	CloudBlockBlob blockBlob = container.getBlockBlobReference(file.getName());
    	blockBlob.upload(fis, file.length());

    	fis.close();
    }

ダウンロード

java
    // ダウンロード処理
    private static void download() throws Exception{
    	CloudBlobContainer container = auth();

    	// ダウンロード処理
    	String uploadName = "[ダウンロードファイル名]";
    	CloudBlockBlob blockBlob = container.getBlockBlobReference(uploadName);

    	String downloadPath = "[ダウンロードファイルパス]";
    	FileOutputStream fos = new FileOutputStream(new File(downloadPath));
    	blockBlob.download(fos);

    	fos.close();
    }

proxy環境で実行する場合

このまま実行するとタイムアウトになりますので、
jvmにproxyを設定する必要があります。
下記のパラメータを指定して、実行しましょう。

オプション
-Dhttp.proxyHost=[proxyHost] -Dhttp.proxyPort=[proxyPort]
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?