-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectoryUpload.java
68 lines (55 loc) · 3.1 KB
/
DirectoryUpload.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package DirectoryUpload;
import java.net.URL;
import java.util.Random;
import java.util.stream.Stream;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.Paths;
import com.microsoft.azure.storage.blob.*;
import com.microsoft.rest.v2.RestException;
import io.reactivex.*;
public class DirectoryUpload {
public static void main(String[] args) throws java.lang.Exception{
// Retrieve the credentials and initialize SharedKeyCredentials from the system env variables
String accountName = System.getenv("AZURE_STORAGE_ACCOUNT");
String accountKey = System.getenv("AZURE_STORAGE_ACCESS_KEY");
SharedKeyCredentials creds = new SharedKeyCredentials(accountName, accountKey);
// Directory to upload
Path filePath = Paths.get("C:\\path\\to\\directory");
// Create a ServiceURL to call the Blob service. We will also use this to construct the ContainerURL
// Alternatively you can create the BlockBlobURL object directly
// We are using a default pipeline here, you can learn more about it at https://github.com/Azure/azure-storage-java/wiki/Azure-Storage-Java-V10-Overview
final ServiceURL serviceURL = new ServiceURL(new URL("http://" + accountName + ".blob.core.windows.net"), StorageURL.createPipeline(creds, new PipelineOptions()));
// Let's create a container using a blocking call to Azure Storage
// If container exists, we'll catch and continue
Random rand = new Random();
ContainerURL containerURL = serviceURL.createContainerURL("quickstart" + rand.nextInt(1000));
System.out.println("Creating a container at " + containerURL.toString());
containerURL.create(null, null).blockingGet();
// Walk the directory and filter for .xml files
Stream<Path> walk = Files.walk(filePath).filter(p -> p.toString().endsWith(".xml"));
// .filter(Files::isRegularFile) to upload all files ignoring directories
// Upload files found asynchronously into Blob storage in 20 concurrent operations
Observable.fromIterable(() -> walk.iterator()).flatMap(path -> {
BlockBlobURL blobURL = containerURL.createBlockBlobURL(path.getFileName().toString());
FileChannel fc = FileChannel.open(path);
return TransferManager.uploadFileToBlockBlob(
fc, blobURL,
BlockBlobURL.MAX_PUT_BLOCK_BYTES, null).toObservable()
.doOnError(throwable -> {
if (throwable instanceof RestException) {
System.out.println("Failed to upload " + path + " with error:" + ((RestException) throwable).response().statusCode());
} else {
System.out.println(throwable.getMessage());
}
})
.doAfterTerminate(() -> {
System.out.println("Upload of " + path + " completed");
fc.close();
});
}, 20) // Max concurrency of 20
.subscribe();
// To block the main via user input
System.in.read();
}
}