Skip to content

Instantly share code, notes, and snippets.

@dinowang
Last active April 2, 2021 22:05
Show Gist options
  • Save dinowang/c44a46409cc794e91dd05d99f179aa9a to your computer and use it in GitHub Desktop.
Save dinowang/c44a46409cc794e91dd05d99f179aa9a to your computer and use it in GitHub Desktop.
Java program upload file to Azure Blob Storage with progress indicator
package com.cloudriches.sample;
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.CloudBlockBlob;
import java.io.File;
import java.io.FileInputStream;
public class Main implements WatchingInputStream.ProgressListener {
public static void main(String[] args) throws Exception {
Main main = new Main();
main.upload();
}
public void upload() throws Exception
{
CloudStorageAccount account = CloudStorageAccount.parse("DefaultEndpointsProtocol=https;AccountName= ....");
CloudBlobClient client = account.createCloudBlobClient();
CloudBlobContainer container = client.getContainerReference("files");
CloudBlockBlob blob = container.getBlockBlobReference("template_62269_xQ0qqGGG71BB3y99n11x.zip");
File sourceFile = new File("/Users/dino/Downloads/template_62269_xQ0qqGGG71BB3y99n11x.zip");
FileInputStream inputStream = new FileInputStream(sourceFile);
WatchingInputStream watchingInputStream = new WatchingInputStream(inputStream, this);
blob.upload(watchingInputStream, sourceFile.length());
}
@Override
public void onAdvance(long at, long length) {
double percentage = (double)at / (double)length;
System.out.println(percentage);
}
}
package com.cloudriches.sample;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
// from https://gist.github.com/alterakey/1454764
public class WatchingInputStream extends FilterInputStream {
public interface ProgressListener {
void onAdvance(long at, long length);
}
private int marked = 0;
private long position = 0;
private ProgressListener listener;
public WatchingInputStream(InputStream in, ProgressListener listener) {
super(in);
this.listener = listener;
}
@Override
public int read(byte[] buffer, int offset, int count) throws IOException {
int advanced = super.read(buffer, offset, count);
this.position += advanced;
this.report();
return advanced;
}
@Override
public synchronized void reset() throws IOException {
super.reset();
this.position = this.marked;
}
@Override
public synchronized void mark(int readlimit) {
super.mark(readlimit);
this.marked = readlimit;
}
@Override
public long skip(long byteCount) throws IOException {
long advanced = super.skip(byteCount);
this.position += advanced;
this.report();
return advanced;
}
private void report() {
if (this.listener == null)
return;
try {
this.listener.onAdvance(this.position, this.position + this.in.available());
}
catch (IOException e) {
this.listener.onAdvance(this.position, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment